Getting started with Pytest and Selenium
In this tutorial, we are going to set up a project from scratch in Windows, using the latest version released at the time of the post of:
- Python
- PyCharm
- Pytest
- Selenium
- Chrome
- ChromeDriver
Step-by-step guide:
Note: ChromeDriver is required to be downloaded and added to the PATH environment variable in windows. To do that, follow the next resource.
Install Python
- Go to the Python site and hit download to the latest version available.
- Once the download is complete, proceed with the installation. Consider including the executable in the Windows PATH location. That is in one of the steps of the installation wizard.
- A computer restart might be required. To check if Python was added to your PATH, open a Power Shell console and just type and hit enter:
If a bunch of messages is displayed indicating that parameters are missing, then you are good to go, otherwise, a computer restart is mandatory.python
Next up: Install the IDE
Install PyCharm
- Go to the Jetbrains site and hit download to the latest version available.
- Once the download is complete, proceed with the installation.
Next up: Create a new project
Starting a new project in PyCharm
- Open PyCharm
- Go to File -> New project...
- Enter your project name, and select a new virtual environment with pipenv and proceed to create.
- In PyCharm, we have the ability to use the console for everything we need from now on.
Installing dependencies in PyCharm
We need to install certain dependencies to be able to set up our project according to our needs.
- Installing pipenv
This is a package management tool that allows us to have a separate virtual environment for our project and install our dependencies. For more information about pipenv, please visit this video. In the terminal, execute the following command:
pip install pipenv
- Installing pytest
Pytest is a unit testing library, which will be used to handle and execute our test scripts. For more information Pytest, please visit this resource.
In the terminal, execute the following command:
pipenv install pytest --dev
- Installing selenium
There's not much introduction here, Selenium will provide the required bindings to do our web automation scripts.
In the terminal, execute the following command:
pipenv install selenium --dev
- Almost ready to create our first test script. But, first, please read this important resource on setting up Selenium + Pytest
Next up: Creating the first test script
Creating the first test script
- In our project, create a new simple folder named
tests
, the name of this folder is requiered to be like that. - Create a new python file named
test_web.py
and then follow the guide here to execute your first script.