CAPABILITY GENERATOR

LambdaTestAutomation Testing ● How to perform Unit Testing in Python on LambdaTest Selenium Grid

How to perform Unit Testing in Python on LambdaTest Selenium Grid


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.

Prerequisites To Perform Unit testing In Python With LambdaTest Selenium Grid


  1. Global Dependencies
    • Make sure that you have the latest Python build installed. Download and install Python from here.
    • To install python with homebrew use the command below:
    • Make sure you have pip installed for package installation.

  2. LambdaTest Authentication Credential
  3. 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.

  4. Setting Up Project
    • The recommended way to run your tests would be in virtualenv. It will isolate the build from other setups you may have running and ensure that the tests run with the specified versions of the modules specified in the requirements.txt file.
    • Create a virtual environment in your project folder the environment name is arbitrary.
    • Activate the environment:
    • Install the required packages:

Writing Your First Unit Test In Python


For this demo purpose, we will be using this Github repository.

  1. Firstly, get the LambdaTest username and accesskey from the environment variables.

    Note: For getting these environment variables, you will need to import the “os” library of Python.
  2. Next we need to set the desired capability of the Selenium Grid, on which we want to run our test on LambdaTest. You can learn more about it from our Desired Capabilities guide. For this demo, we have used the below specifications:
  3. Since we have the LambdaTest Credentials and the Desired Capabilities, we can create the Selenium Remote WebDriver instance now. Use the below code to do the same.
  4. Now we are ready to write our test and launch it. The above steps can be considered as the setup() method.
  5. In the test, we will take an example of our sample ToDo app. We will try to check two checkboxes of the list and add another item in the list. The code to do is given below:
  6. Once the test is performed, we can assert if our test has passed as expected or failed. Here, we will check if the added item is actually present in the list or not. Below is the code that explains how to do that.

  7. Now we just need to write the teardown() method to close the driver, after the test is complete.

The complete code for this test is give below:

Running Your First Unit Test In Python On LambdaTest Selenium Grid


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! 🙂