Python Unittest with Selenium : Tutorial to run your first test on Lambdatest
In this topic, you will learn how to configure and run your automation testing scripts on LambdaTest Selenium cloud platform using Python framework UnitTest.
Objective
By the end of this topic, you will be able to:
- Set up an environment for testing your hosted web pages using UnitTest framework with Selenium.
- Understand and configure the core capabilities required for your Selenium test suite.
- Test your locally hosted pages on LambdaTest platform.
- Explore advanced features of LambdaTest.
Note: All the code samples in this documentation can be found in the LambdaTest's Repository on GitHub. You can either download or clone the repository to quickly run your tests.
Prerequisites To Perform Unit testing In Python with Selenium
Before you can start performing Python automation testing using UnitTest, you would need to:
- Install the latest Python build from the official website. We recommend using the latest version.
- Make sure pip is installed in your system. You can install pip from pip documentation.
- Download the latest Selenium Client and its WebDriver bindings from the official website. Latest versions of Selenium Client and WebDriver are ideal for running your automation script on LambdaTest Selenium cloud grid.
- Install virtualenv which is the recommended way to run your tests. 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.
pip install virtualenv
Installing Selenium Dependencies and Tutorial Repo
Step 1: Clone the LambdaTest’s Python-UnitTest-Selenium repository and navigate to the code directory as shown below:
git clone https://github.com/LambdaTest/Python-UnitTest-Selenium
cd Python-UnitTest-Selenium
Step 2: Create a virtual environment in your project folder the environment name is arbitrary.
virtualenv venv
Step 3: Activate the environment.
source venv/bin/activate
Step 4: Install the required packages from the cloned project directory:
pip install -r requirements.txt
Setting up Your Authentication
Make sure you have your LambdaTest credentials with you to run test automation scripts on LambdaTest Selenium Grid. You can obtain these credentials from the LambdaTest Automation Dashboard or through LambdaTest Profile.
Step 5: Set LambdaTest Username
and Access Key
in environment variables.
- For Linux/macOS:
export LT_USERNAME="YOUR_USERNAME" export LT_ACCESS_KEY="YOUR ACCESS KEY"
- For Windows:
set LT_USERNAME="YOUR_USERNAME" set LT_ACCESS_KEY="YOUR ACCESS KEY"