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

  1. Go to the Python site and hit download to the latest version available.
  2. 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.
  3. 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:
    python
    
    If a bunch of messages is displayed indicating that parameters are missing, then you are good to go, otherwise, a computer restart is mandatory.

Next up: Install the IDE

Install PyCharm

  1. Go to the Jetbrains site and hit download to the latest version available.
  2. Once the download is complete, proceed with the installation.

Next up: Create a new project

Starting a new project in PyCharm

  1. Open PyCharm
  2. Go to File -> New project...
  3. Enter your project name, and select a new virtual environment with pipenv and proceed to create.
  4. 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.

  1. 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
    
  2. 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
    
  3. 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
    
  4. 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

  1. In our project, create a new simple folder named tests, the name of this folder is requiered to be like that.
  2. Create a new python file named test_web.py and then follow the guide here to execute your first script.