Unit testing is the type of software testing in which each component is considered an individual unit and tested in an isolated environment. It means that a unit or component will be tested for its features and performance independently, without combining it with any other unit. Python allows performing unit testing very easily with the help of the “unittest” library.
LambdaTest provides an online Selenium Automation Grid platform, enabling you to run your automation scripts on combinations of 2000+ different browsers and operating systems. LambdaTest also allows you to run your Python codes on its Selenium Grid. In this post, we will see how to perform unit testing in Python on LambdaTest online cloud-based Selenium Grid.
All the code samples in this documentation can be found in the Unittest LambdaTest Repository on GitHub. You can either download or clone the repository to quickly run your tests.
1 |
$ brew install python |
Make sure to set your LambdaTest username, and access key as the environment variable. We will be importing these in our code. You can retrieve your username and access key from your LambdaTest automation dashboard by clicking on the key icon near the help button.
1 |
pip install virtualenv |
1 |
virtualenv venv |
1 |
source venv/bin/activate |
1 |
pip install -r requirements.txt |
1 2 |
username = os.environ.get("LT_USERNAME") access_key = os.environ.get("LT_ACCESS_KEY") |
1 |
import os |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
desired_caps = { # Name of the build under which the test will run "build": 'LambdaTest Python unittest demo', # Name of the test to run "name": 'Py-unittest', # Desired operating system "platform": 'Windows 10', # Desired browser "browserName": 'firefox', # Desired version of browser "version": '73' } |
1 2 3 |
self.driver = webdriver.Remote( command_executor = "http://{}:{}@hub.lambdatest.com:80/wd/hub".format(username, access_key), desired_capabilities = desired_caps) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# try: driver = self.driver # Url driver.get("https://lambdatest.github.io/sample-todo-app/") # Click on check box check_box_one = driver.find_element_by_name("li1") check_box_one.click() # Click on check box check_box_two = driver.find_element_by_name("li2") check_box_two.click() # Enter item in textfield textfield = driver.find_element_by_id("sampletodotext") textfield.send_keys("Yey, Let's add it to list") # Click on add button add_button = driver.find_element_by_id("addbutton") add_button.click() # Verified added item added_item = driver.find_element_by_xpath("//span[@class='done-false']").text print (added_item) |
1 2 3 4 5 6 |
#Assertion if "Yey, Let's add it to list" in added_item: driver.execute_script("lambda-status=passed") else: driver.execute_script("lambda-status=failed") |
1 |
self.driver.quit() |
The complete code for this test is give below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
import os import unittest import sys from selenium import webdriver username = os.environ.get("LT_USERNAME") access_key = os.environ.get("LT_ACCESS_KEY") class FirstSampleTest(unittest.TestCase): # setUp runs before each test case def setUp(self): desired_caps = { "build": 'LambdaTest Python unittest demo', "name": 'Py-unittest', "platform": 'Windows 10', "browserName": 'firefox', "version": '73' } self.driver = webdriver.Remote( command_executor="http://{}:{}@hub.lambdatest.com:80/wd/hub".format(username, access_key), desired_capabilities= desired_caps) # tearDown runs after each test case def tearDown(self): self.driver.quit() # test file def test_unit_user_should_able_to_add_item(self): # try: driver = self.driver # Url driver.get("https://lambdatest.github.io/sample-todo-app/") # Click on check box check_box_one = driver.find_element_by_name("li1") check_box_one.click() # Click on check box check_box_two = driver.find_element_by_name("li2") check_box_two.click() # Enter item in textfield textfield = driver.find_element_by_id("sampletodotext") textfield.send_keys("Yey, Let's add it to list") # Click on add button add_button = driver.find_element_by_id("addbutton") add_button.click() # Verified added item added_item = driver.find_element_by_xpath("//span[@class='done-false']").text print (added_item) # Assertion - check the pass fail condition if "Yey, Let's add it to list" in added_item: driver.execute_script("lambda-status=passed") else: driver.execute_script("lambda-status=failed") if __name__ == "__main__": unittest.main() |
Upon executing the above code, the unit test will run and you can find the details of it on your automation dashboard of LambdaTest.
Debug your executed tests, verify results, see execution video, mark bugs, and much more using the most popular cross browser testing platform – LambdaTest.
For any query or doubt, please feel free to contact us via 24×7 chat support or you can also drop a mail to support@lambdatest.com.
Happy testing!