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
  • Conclusion

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

Install pytest
Go to command prompt & run ‘pip install pytest’



Figure 1

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)


Figure 2

You can also run the ‘pytest –version’ command to check the version


Figure 3

Pytest conventions
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
Figure 4

Setup Test Runner
In eclipse, perform following steps

Figure 5


Figure 6

Click ‘Apply and Close’

Write first Selenium test using pytest
Let us first create a new package (figures 7, 8, 9)


Figure 7


Figure 8


Figure 9

We will now create a python test inside this package. The prefix would be ‘test’


Figure 10

We will write a simple test case that will launch a chrome driver. To do that, we will first import webdriver

Figure 11

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


Figure 12

Run first Selenium test using pytest
Let us run this pytest (Run As 🡪 Python unit-test)


Figure 13

We get an exception, see below.


Figure 14

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’

Figure 15

Click ‘latest’

Click ‘ChromeDriver 2.42’ link or whatever latest version you see


Figure 16

The below window will come up.

Download ‘chromedriver_win32.zip’
Figure 17

Extract the exe

Figure 18

Place the exe file in desired location.

Mention the path of exe in the constructor


Figure 19

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


Figure 20

Add more functionality to selenium pytest
Line#5 below navigates to a website & line#6 maximizes the window


Figure 21

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


Figure 22

Selenium Python API documentation
You can go to the site https://selenium-python.readthedocs.io and refer the API documentation

Figure 23

For example, the below page will tell you everything related to navigating to a website.


Figure 24

The below page will answer most of your FAQs

Figure 25

Conclusion
In this tutorial we have studied about pytest fundamentals & also created our first selenium test. Thank you!