Automated Testing Using PyTest In Python.

In this article, we will learn Automated testing using the PyTest framework in Python.

What is PyTest?

PyTest is a testing framework that allows users to write test codes using Python programming language. It helps you to write simple and scalable test cases for databases, APIs, or UI. PyTest is mainly used for writing tests for APIs. It helps to write tests from simple unit tests to complex functional tests.

Advantages of PyTest

  • Pytest can run multiple tests in parallel mode, which reduces the execution time of the test suite.
  • Pytest has its own way to detect the test file and test functions automatically, if not mentioned explicitly.
  • Pytest allows us to skip a group of tests with conditions or display error messages during execution.

To, start with the installation of Pytest using the following command.

pip install pytest

Features of PyTest

1) Test Creation

  • Creating tests in Pytest is simple as writing a “test” keyword in the starting of function or class name and also file name.
  • In our example, we can implement a test class under multiple test case functions.
  • First of all, create one test file for writing test cases. filename must start with the “test” keyword otherwise PyTest does not run test cases.
import pytest

class Test_api:
    def test_001(self):
        a = 10
        b = 20
        c = a + b
        assert c == 30, "FAILED"

=> run above code using the following command: pytest

=> In the above example, write a simple test case for test case pass or fill is displayed in the console, in our example test case is pass.

2) Fixtures

  • Fixtures are a special type of function that will run before the test function, module, or the whole session based on their configuration and return any intended values. They will help us to execute a piece of code.
  • Fixtures are a set of resources that have to be set up before and cleaned up once the Selenium test automation execution is completed.
  • A function is marked as a fixture using the following marker:  @pytest.fixture

3) Parameterization 

  • Parameterization is the process of taking values or objects defined within a function or a method and making them parameters to that function or method, in order to generalize the code.
  • You can pass arguments to fixtures with the params keyword argument in the fixture decorator, and you can also pass arguments to tests with the @pytest. mark. parametrize decorator for individual tests.

4) File 

  • You can put fixtures into individual test files, but to share fixtures among multiple test files, you need to use a file somewhere centrally located for all of the tests.
  • The fixtures can be shared by any test. You can put fixtures in individual test files if you want the fixture to only be used by tests in that file.

5) Report

  •  Prerequisite for generating HTML report. Install pytest-html package using the following command.
pip install pytest-html
  • Run below command for generating HTML report
pytest --html=report.html

Different commands were used for giving more details about our test case.

pytest -s => using -s you can see a print statement to added string or variable value.

pytest -v => Should we want to increase verbosity to our output, we can use the -v option.


=> You can also use selenium scrapping or automation through getting data and test case implementation.

To learn more about Selenium follow this blog Selenium-Scrapping.

To learn more about PyTest, check the official documentation.

I hope you’ll get impressed by the features of PyTest. So let’s start coding within the next blog. If you’ve got any questions regarding this blog, please let me know in the comments.

Submit a Comment

Your email address will not be published. Required fields are marked *


Select Categories