Python Automated Testing
Before you can do any kind of integration testing with Python, let's first start with making sure Python is available on your system.
For Windows:- Download the latest python installer for Windows: https://www.python.org/downloads/windows/
- Run the installer and follow the setup wizard to install Python.
-
Run
python --version
to see which Python version is currently installed, make sure it is 2.5.X or above. - macOS, Ubuntu and most other Linux distros come with Python pre-installed.
Installation
With TestingBot you can easily run your automated tests with any Python testing framework, here's a simple example:
import unittest
import sys
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from testingbotclient import TestingBotClient
class TestTestingBotClient(unittest.TestCase):
def setUp(self):
desired_cap = { 'platform': 'Windows', 'browserName': 'chrome', 'version': 'latest-1' }
self.driver = webdriver.Remote(
command_executor='http://key:secret@hub.testingbot.com/wd/hub',
desired_capabilities=desired_cap)
def test_google_example(self):
self.driver.get("https://www.google.com")
if not "Google" in self.driver.title:
raise Exception("Unable to load google page!")
elem = self.driver.find_element_by_name("q")
elem.send_keys("TestingBot")
elem.submit()
def tearDown(self):
self.driver.quit()
status = sys.exc_info() == (None, None, None)
tb_client = TestingBotClient('key', 'secret')
tb_client.tests.update_test(self.driver.session_id, self._testMethodName, status)
if __name__ == '__main__':
unittest.main()
This test will start a Firefox browser on Windows in our cloud, go to Google, and search for TestingBot. It will then output the title.
Once the test has finished, the tb_client
library will send back the test's success state and name to TestingBot, so you have a nice overview of your tests by name and success state in our member dashboard.
Make sure to always stop your test (
driver.quit()
), otherwise it will continue running, leading to a timeout.
Configuring capabilities
To run your existing tests on TestingBot, your tests will need to be configured to use the TestingBot remote machines. If the test was running on your local machine or network, you can simply change your existing test like this:
Before: After:Specify Browsers & Devices
To let TestingBot know on which browser/platform/device you want to run your test on, you need to specify the browsername, version, OS and other optional options in the capabilities field.
Testing Internal Websites
We've built TestingBot Tunnel, to provide you with a secure way to run tests against your staged/internal webapps.
Please see our TestingBot Tunnel documentation for more information about this easy to use tunneling solution.
The example below shows how to easily run a Python test with our Tunnel:
1. Download our tunnel and start the tunnel:
2. Adjust your test: instead of pointing to 'hub.testingbot.com/wd/hub'
like the example above - change it to point to your tunnel's IP address.
Assuming you run the tunnel on the same machine you run your tests, change to 'localhost:4445/wd/hub'
. localhost is the machine running the tunnel, 4445 is the default port of the tunnel.
This way your test will go securely through the tunnel to TestingBot and back:
import unittest
import sys
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from testingbotclient import TestingBotClient
class TestTestingBotClient(unittest.TestCase):
def setUp(self):
desired_cap = {'platform': 'Windows', 'browserName': 'chrome', 'version': 'latest-1' }
self.driver = webdriver.Remote(
command_executor='http://key:secret@localhost:4445/wd/hub',
desired_capabilities=desired_cap)
def test_google_example(self):
self.driver.get("https://www.google.com")
if not "Google" in self.driver.title:
raise Exception("Unable to load google page!")
elem = self.driver.find_element_by_name("q")
elem.send_keys("TestingBot")
elem.submit()
def tearDown(self):
self.driver.quit()
status = sys.exc_info() == (None, None, None)
tb_client = TestingBotClient('key', 'secret')
tb_client.tests.update_test(self.driver.session_id, self._testMethodName, status)
if __name__ == '__main__':
unittest.main()
Run tests in Parallel
Parallel Testing means running the same test, or multiple tests, simultaneously. This greatly reduces your total testing time.
You can run the same tests on all different browser configurations or run different tests all on the same browser configuration.
TestingBot has a large grid of machines and browsers, which means you can use our service to do efficient parallel testing. It is one of the key features we provide to greatly cut down on your total testing time.
To run tests in parallel, we recommend using Nose and MultiProcessing, which makes it very easy to run multiple Python tests simultaneously:
Queuing
Every plan we provide comes with a limit of parallel tests.
If you exceed the number of parallel tests assigned to your account, TestingBot will queue the additional tests (for up to 6 minutes) and run the tests as soon as slots become available.
Mark tests as passed/failed
As TestingBot has no way to dermine whether your test passed or failed (it is determined by your business logic), we offer a way to send the test status back to TestingBot. This is useful if you want to see if a test succeeded or failed from the TestingBot member area.
You can use our Python API client to report back test results.
Other Python Testing Framework examples
-
PyTest
PyTest makes it easy to run Selenium tests with Python.
-
Behave
Behave is behaviour-driven development, Python style.
-
Lettuce
Lettuce is a Python BDD plugin based on Ruby's Cucumber, offering Gherkin stories.
-
PyUnit
PyUnit is the standard unit testing framework module for Python, described as a Python version of JUnit.