Tutorial 16 – Pytest Introduction
The first 15 tutorials were about the Python language. Starting this tutorial, we will study about Pytest. We will also write our first selenium test. So welcome to the sixteenth tutorial of Selenium-Python series!What you will Learn:
- What is Pytest
- Advantages of pytest
- Install pytest
- Pytest conventions
- Install selenium package
- Setup Test Runner
- Write first Selenium test using pytest
- Run first Selenium test using pytest
- Download chromedriver exe
- Add more functionality to selenium pytest
- Selenium Python API documentation
What is PytestPytest is a unit testing framework for python. It’s a testing framework which allows us to write test tets cases using python. The other competitors to pytest are: nose, ‘unit test’.
Advantages of pytest
There are few advantages of pytest over other tools:
1) Pytest is easy to use & has easy syntax
2) It can run tests in parallel
3) We can skip few of the tests. So if we have 100 test cases, we can skip 40 & run only the desired 40 tests
Go to command prompt & run ‘pip install pytest’
Now if you want to check whether pytest is installed in eclipse, run ‘pip list’ command, see below, we can see the pytest version as well (5.2.2)
You can also run the ‘pytest –version’ command to check the version
There are few pytest conventions:
1) Test case file name should start with a prefix ‘test’
2) Test case method name should start with a prefix ‘test’
Install selenium package
Setup Test Runner
In eclipse, perform following steps
Click ‘Apply and Close’
Write first Selenium test using pytest
Let us first create a new package (figures 7, 8, 9)
We will now create a python test inside this package. The prefix would be ‘test’
We will write a simple test case that will launch a chrome driver. To do that, we will first import webdriver
See line#3 below. We are creating a method & the method name starts with prefix ‘test’. In line#4 we are creating an object of chrome browser & it launches the chrome browser
Run first Selenium test using pytest
Let us run this pytest (Run As 🡪 Python unit-test)
We get an exception, see below.
To resolve this error, we have to tell pytest the path of chromedriver.exe file.
Download chromedriver exe
Go to https://docs.seleniumhq.org/download/ & under third party browsers you will find ‘Google Chrome Driver’, click ‘latest’
Click ‘ChromeDriver 2.42’ link or whatever latest version you see
The below window will come up.
Extract the exe
Place the exe file in desired location.
Mention the path of exe in the constructor
Now when you run as ‘Python unit test’, the empty chrome browser gets launched.
Notice the browser message “Chrome is being controlled by automated test software”. So we have successfully launched a browser in an automated fashion using python script!!
Add more functionality to selenium pytest
Line#5 below navigates to a website & line#6 maximizes the window
Run as pytest unit test, notice that google.com is launched!!! Notice the browser message “Chrome is being controlled by automated test software”. So we have successfully launched a browser in an automated fashion using python script!!
Selenium Python API documentation
You can go to the site https://selenium-python.readthedocs.io and refer the API documentation
For example, the below page will tell you everything related to navigating to a website.
The below page will answer most of your FAQs
In this tutorial we have studied about pytest fundamentals & also created our first selenium test. Thank you!