In Previous section, we have discussed about basic functionalities of Python in Selenium and the implementation with the practical examples. Now we will discuss another important concept of Selenium Python which is called PyTest. In this section we will focus on the introduction to PyTest and later we will discus some more advance topics of PyTest.
Objective:
- What is Framework
- Introduction to PyTest Framework
- How to Install PyTest Framework
- Run Basic PyTest Framework
- Examples
What is Framework?
Before going to the deep on Introduction to PyTest, we need to understand what is Framework.
A Framework is the collection of module or package which help Programmer / Tester writing the automation script. In case of working in framework, we do not need any low level details to run the Python script such as Thread Management, Socket etc.
Framework makes programmer life easier by reusing the code or common functionalities. Also in Framework, code maintenance is less than traditional method. For example: in Selenium, there are some cases that same fields are used in multiple number of test case. If we will use traditional method then we need to locate the same element multiple number of times in different test cases. If some part of DOM is changed then we need to update our script in all test cases which is very complex and hectic.
In case of Framework, we can separate all elements in different files and we can use those in our test case so that we can define all element locator once and in a single place. If some part of DOM is changed then we can update this in that one place which is very easy to manage and easy to maintain our code clean.
Also in Framework, we can customize our output report in proper format like PDF, Screenshot, Email, HTML etc.
There are 5 types of automation testing framework in Python Selenium.
- Robot Framework
- PyTest
- Unittest / PyUnit
- Behave
- Lettus

What is PyTest Framework?
PyTest is a Framework in Selenium Python. Like Java we have TestNG framework, we have PyTest in Selenium Python. Unittest is by default available in Python library. But PyTest has many advance features in Selenium Python over Selenium java.
In other word, PyTest is a testing framework which allows testers / users to write the automated test case using Python Programming language. We can write test case for APIs, UI, databases, automate test case.
PyTest is a command-line tool which finds our tests we have written and run those tests and product the test result in a proper format.
How to install PyTest Framework:
PyTest is Open Source. That means you do not need to pay anything for it. With PyTest you can create automated test cases for UI and back-end also. In addition to UI and back-end, you can create automate script for Mobile application also.
Like TestNG, You can do parallel execution in PyTest. Parallel execution is very important concept where you need to run your script in multiple browsers and run test cases in parallel to execute more number of test cases in less time.
PyTest is not coming in Python by default. You need to install before using in your script. Run below command to install PyTest in Selenium Python.
pip install pytest


If you want to see what version of PyTest you are using then you can run below command and Enter. The version will be printed in terminal.
pytest–version

Run basic PyTest:
To write PyTest script, you need to import the pytest in your script as below.
import pytest
There are some rules to create the test case in PyTest. i.e.
- All test case name should start with “test” keyword or end with “test” keyword and your file name should also starts with test_.
Let’s create a simple test case to understand the PyTest script. In below example, we have created the 6 test method with method name starts with test_
importpytest def test_m1(): a=3 b=4 assert a+1 == b, "Test Failed" assert a == b, "Test Failed as a is not equal to b" def test_m2(): name = "Selenium" assert name.upper() == "SELENIUM" def test_m3(): assert True def test_m4(): assert False def test_m5(): assert 100 == 100 def test_m6(): assert "Pratik" == "PRATIK"
Output:


From the above output you can see that multiple block where the first block signifies that “test session starts”.

You can see here the output showing is F..F.F . F in red color signifies that test case failed and “.” (dot) in green color signifies that test case passed. This the conversion that PyTest follow. Here total 6 test case are executed out of it 3 test case got passed and 3 test case got failed.
After this block all failed test execution block will be displayed. In last “short test summary info” will be displayed. In “short test summary info” all failed test case description will be shown with reason.

If you will run the command py.test then all files listed in application will be executed.
To execute only a particular file then you need to go the directory and type file name with py.test as below .
py.test<package name or directory name >/<File name>
py.test PyTest_Session/test_demo2.py
To run only those test case which has the keyword only login then you need to use –k and –v in your command as below.
py.test -k login –v
Here –k is the identifier just like group or on the basic of some keyword.
-v is to show the output in more descriptive manner.
test_demo1.py:
import pytest def test_m1(): a=3 b=4 assert a+1 == b, "Test Failed" assert a == b, "Test Failed as a is not equal to b" def test_m2(): name = "Selenium" assert name.upper() == "SELENIUM" def test_m3(): assert True def test_m4(): assert False def test_m5(): assert 100 == 100 def test_m6(): assert "Pratik" == "PRATIK" def test_login(): assert "admin" == "admin"
test_demo2.py:
importpytest def test_m4(): assert False def test_m5(): assert 100 == 100 def test_m6(): assert "Pratik" == "PRATIK" def test_login(): assert "admin" == "admin"
Output:

What if we will exclude –v while executing above command. See below output to understand.

Here you can see if we exclude –v then file is running and output is showing but the result is not showing fully like each file status and from each file which test cases has run.
In this section we have understood about the Introduction to PyTest and writing the basic script in PyTest.
Summary:
- A Framework is the collection of module or package which help programmer / Tester writing your automation script.
- In case of Framework, we can separate all elements in different file and we can use those in our test case so that we can define all element locator once and in a single place.
- PyTest is a Framework in Selenium Python. Like Java we have TestNG framework in Selenium Java, we have PyTest in Selenium Python. Unit-test is by default available in Python library. But PyTest has the advance feature in Selenium Python.
- PyTest is a command-line tool which finds our tests we have written and run those tests and product the test result in a proper format.
- PyTest is Open Source.