Automated testing – Pytest testing framework

01 | Introduction

Pytest is a very mature, full-featured Python testing framework with the following main features:

  1. Simple and flexible, easy to use, rich documentation

  2. Supports parameterization and allows fine-grained control of test cases

  3. Supports simple unit testing and complex functional testing, and can also be used for automated testing of UI and interfaces such as Selenium, Appium, and Requests.

  4. Supports many third-party plug-ins and can customize extensions (Pytest plug-in download address)

  5. Support skipping and failure retry of test cases

  6. Can be well combined with CI tools, such as Jenkins

02 Installation

pip install pytest

03 | Simple to use

import pytest


def test01():
    print('First use case')
    assert True
def test02():
    print("Second use case")
    assert False


if __name__=="__main__":
    # -s: Display the output in the use case
    # -v: Output more detailed use case execution information
    # __file__: this file
    pytest.main(["-s", "-v", __file__])

The running results are as follows:

04 | Initialization & amp;End

4.1 Function level

Execute the initialization & end method every time a test case is executed

import pytest
classTest:
    def setup(self):
        print("Initialization")
    def teardown(self):
        print("End")
    def test01(self):
        print('First use case')
        assert True
    def test02(self):
        print("Second use case")
        assert False
if __name__=="__main__":
    pytest.main(["-s",__file__])

Execution result graph:

4.2 Class Level

No matter how many use cases need to be executed in the class, only the initialization & end method is executed once

import pytest
classTest:
    def setup_class(self):
        print("Initialization")
    def teardown_class(self):
        print("End")
    def test01(self):
        print('First use case')
        assert True
    def test02(self):
        print("Second use case")
        assert False
if __name__=="__main__":
    pytest.main(["-s",__file__])

Execution result graph:

Now I have also found a lot of test friends and created a communication group to share technology, sharing a lot of technical documents and video tutorials we collected.
If you don’t want to experience the feeling of not being able to find resources when studying on your own, having no one to answer your questions, and persisting for a few days before giving up.
You can join us to communicate. And there are many technical experts who have made certain achievements in automation, performance, security, test development, etc.
Share their experience, and also share many live lectures and technical salons
You can learn for free! Focus on it! Open source! ! !
QQ group number: 110685036

05 | Commonly used plug-ins

5.1 Test Report

Install the test report plug-in

pip install pytest-html

Create the pytest.ini configuration file in the project directory, and add the test report storage path in the configuration file.

[pytest]
#Create a report directory in the current directory to store test reports
addopts = --html=./report/report.html

Execute the test and the generated test report is as shown below:

5.2 Retry after failure

Install plugin

pip install pytest-rerunfailures

Global retry: Add retry parameters in the configuration file to control the retry and retry waiting time after all use cases fail.

[pytest]
addopts = --html=./report/report.html --reruns 3 --reruns-delay 2
# --reruns n, n is an integer indicating the number of retries
# --reruns-delay n, n is an integer, indicating the retry waiting time, the unit is s

The retry status of failed test cases can be seen in the test report

Single retry: Add a decorator to a use case function. Note that you need to comment out the global retry configuration.

# reruns specifies the number of retries, reruns_delay specifies the retry interval
@pytest.mark.flaky(reruns=2, reruns_delay=1)
    def test02(self):
        print("Second use case")
        assert False

Look at the retry status in the test report:

5.3 Concurrent execution of multiple processes

The emergence of pytest-xdist is to allow automated test cases to be executed in a distributed manner, thereby saving automated testing time

Install plugin

pip install pytest-xdist

Multiple CPU parallel execution

pytest -n number of parallel tests

5.4 Multiple assertions

When using pytest for assertion judgment, in order to ensure the accuracy of the use case, assertions are often made in multiple aspects, such as:

  • Assertion 1: Assert the status of the http response

  • Assertion 2: Assert the code value returned in the response

  • Assertion 3: Assert whether the data field in the json returned by the response meets expectations.

If you use native Python’s assert, you will encounter a situation where all assertions fail if one assertion fails. For example, if assertion 1 results in Failed, neither assertion 2 nor assertion 3 will be executed. We hope that assertion 2 and assertion 3 will continue to execute, so that we can obtain more assertion results to determine where there is a problem with the interface, and can better locate the problem. At this time, we can use the pytest-assume plug-in to achieve this.

Install plugin

pip install pytest-assume

Use Cases

import pytest


def test_add2():
    pytest.assume(1 + 4 == 5)
    pytest.assume(1 + 3 == 3)
    pytest.assume(2 + 5 == 7)
    pytest.assume(2 + 5 == 9)
    print("Test completed")

if __name__=="__main__":
    pytest.main(["-s", "-v", __file__])

The execution results are as shown below. It can be seen that a total of four assertions were executed, of which 2 failed assertions.

5.5 Beautify execution results

When we conduct automated testing, there are often hundreds or thousands of use cases, and the execution time is tens of minutes or hours. Sometimes, when we debug so many use cases, we don’t know how far they have been executed, and the pytest-sugar plug-in can solve our pain points very well.

Install pytest-sugar plugin

pip install pytest-sugar

Execute the use case file in the command line. The execution result is as shown below. When the use case is executed, a progress bar will be displayed:

If my blog is helpful to you, if you like my blog content, please “like”, “comment” and “favorite” with three clicks!