Ten minutes to take you to understand - the most complete lecture on pytest of the Python testing framework

pytest extra short

pytest is a very mature full-featured Python testing framework, which mainly has the following characteristics:

Simple and flexible, easy to use

Support parameterization

It can support simple unit tests and complex functional tests, and can also be used for automated testing such as selenium/appnium and interface automated testing (pytest+requests)

pytest has many third-party plug-ins, and can customize extensions, such as pytest-selenium (integrated selenium), pytest-html (perfect html test report generation), pytest-rerunfailures (failure case repeated execution), pytest-xdist (Multi-CPU distribution) etc.

Skip and xfail processing of test cases

Can be well integrated with jenkins

report framework----allure also supports pytest
 

1.pytest installation

1.1 Installation

pip install -U pytest

 

1.2 Verify installation

pytest --version # will show the currently installed version

 

1.3 pytest documentation

Official documentation:
https://docs.pytest.org/en/latest/contents.html

In the pytest framework, there are the following constraints:

All unit test file names need to meet the test_*.py format or *_test.py format.

In the unit test file, the test class starts with Test and cannot have an init method (note: when defining a class, it needs to start with T, otherwise pytest will not run the class)

In a unit test class, one or more functions beginning with test_ can be included.

At this time, when executing the pytest command, it will automatically search for test functions that meet the above constraints from the current directory and subdirectories to execute.

1.4 Pytest running mode

 

# file_name: test_abc.py
import pytest # 引入pytest包
def test_a(): # test开头的测试函数
print("------->test_a")
assert 1 # 断言成功
def test_b():
print("------->test_b")
assert 0 # 断言失败
if __name__ == '__main__':
pytest.main("-s test_abc.py") # 调用pytest的main函数执行测试

1. Test class main function mode

pytest.main("-s test_abc.py")

2. Command line mode

pytest file path / test file name

For example: pytest ./test_abc.py

1.5 Pytest Exit Code meaning list

  • Exit code 0 All use cases have been executed and passed
  • Exit code 1 All test cases have been executed, and there are Failed test cases
  • Exit code 2 The user interrupted the execution of the test
  • Exit code 3 An internal error occurred during test execution
  • Exit code 4 pytest command line usage error
  • Exit code 5 No available test case files were collected

1.6 How to get help information

View pytest version

pytest --version

Show available built-in function arguments

pytest --fixtures

View help information and configuration file options through the command line

pytest --help

1.7 Controlling Test Case Execution

1. After the Nth test case fails, end the test execution

pytest -x # 01st failure, stop the test

pytest --maxfail=2 # terminate the test if there are 2 failures

2. Specify the test module

pytest test_mod.py

3. Specify the test directory

pytest testing/

4. Filter execution by keyword expression

pytest -k "MyClass and not method"

This command will match the use case where the file name, class name, and method name match the expression. Here, this command will run
TestMyClass.test_something and will not execute TestMyClass.test_method_simple

5. Specify test cases by node id

nodeid is composed of module file name, delimiter, class name, method name, and parameters, examples are as follows:

Run the specified use case in the module

pytest test_mod.py::test_func

Run the specified method in the module

ytest test_mod.py::TestClass::test_method

6. Execution via tagged expressions

pytest -m slow

This command will execute all test cases decorated by the decorator @pytest.mark.slow

7. Execute tests through packages

pytest --pyargs pkg.testing

This command will automatically import the package pkg.testing, and use the directory where the package is located to execute the following use cases.

1.8 Multi-process running cases

When there are a lot of cases, the running time will also become very long. If you want to shorten the running time of the script, you can use multiple processes to run it.

Install pytest-xdist:

pip install -U pytest-xdist

Operating mode:

pytest test_se.py -n NUM

Among them, NUM fills in the number of concurrent processes.

1.9 Retry running cases

When doing interface testing, sometimes 503 or short-term network fluctuations may be encountered, causing the case to fail, and this is not the result we expected. At this time, it can be solved by retrying the cases.

Install pytest-rerunfailures:

pip install -U pytest-rerunfailures

Operating mode:

pytest test_se.py --rerunning NUM

NUM fills in the number of retries.

1.10 Display print content

When running the test script, in order to debug or print some content, we will add some print content to the code, but when running pytest, these content will not be displayed. If you bring -s, you can display it.

Operating mode:

pytest test_se.py -s

In addition, the multiple running modes of pytest can be superimposed and executed. For example, you want to run 4 processes at the same time, and you want to print out the print content. Can use:

pytest test_se.py -s -n 4

2. The setup and teardown functions of Pytest

1. setup and teardown are mainly divided into: module level, class level, function level, function level.

2. Exists inside the test class

Code example:

  • Function level setup()/teardown()

Run at the beginning and end of the test method, that is: running a test function will run setup and teardown once

import pytest
class Test_ABC:
# 函数级开始
def setup(self):
print("------->setup_method")
# 函数级结束
def teardown(self):
print("------->teardown_method")
def test_a(self):
print("------->test_a")
assert 1
def test_b(self):
print("------->test_b")
if __name__ == '__main__':
pytest.main("-s test_abc.py")

Results of the:

test_abc.py

------->setup_method # first setup()

------->test_a

.

------->teardown_method # first teardown()

------->setup_method # second setup()

------->test_b

.

------->teardown_method # second teardown()

2.2. Class level

Run at the beginning and end of the test class, that is: only run setup_class and teardown_class once in a test, regardless of how many test functions there are in the test class.

Code example:
 

import pytest
class Test_ABC:
# 测试类级开始
def setup_class(self):
print("------->setup_class")
# 测试类级结束
def teardown_class(self):
print("------->teardown_class")
def test_a(self):
print("------->test_a")
assert 1
def test_b(self):
print("------->test_b")
if __name__ == '__main__':
pytest.main("-s test_abc.py")

Results of the:

test_abc.py
------->setup_class # 第一次 setup_class()
------->test_a
.
------->test_b
F
------->teardown_class # 第一次 teardown_class()

3. Pytest configuration file

The configuration file of pytest is usually placed in the test directory, named pytest.ini, and the configuration in the configuration file will be used when the command line is run.

#配置pytest命令行运行参数
[pytest]
addopts = -s ... # 空格分隔,可添加多个命令行参数 -所有参数均为插件包的参数配置测试搜索的路径
testpaths = ./scripts # 当前目录下的scripts文件夹 -可自定义
#配置测试搜索的文件名称
python_files = test*.py
#当前目录下的scripts文件夹下,以test开头,以.py结尾的所有文件 -可自定义
配置测试搜索的测试类名
python_classes = Test_*
 
#当前目录下的scripts文件夹下,以test开头,以.py结尾的所有文件中,以Test开头的类 -可自定义
配置测试搜索的测试函数名
 
python_functions = test_*
 
#当前目录下的scripts文件夹下,以test开头,以.py结尾的所有文件中,以Test开头的类内,以test_开头的方法 -可自定义

4 Pytest common plugins

Plugin List URL:
https://plugincompat.herokuapp.com

Contains many plug-in packages, you can choose to use according to the needs of the work.

4.1 Preconditions:

1. File path:

- Test_App
- - test_abc.py
- - pytest.ini

2. pyetst.ini configuration file content:

[pytest]
# 命令行参数
addopts = -s
# 搜索文件名
python_files = test_*.py
# 搜索的类名
python_classes = Test_*
#搜索的函数名
python_functions = test_*

4.2 Pytest test report

pytest-HTML is a plugin for pytest to generate HTML reports of test results. Compatible with Python 2.7, 3.6

Installation method: pip install pytest-html

pip install pytest-html

Generate a test report in xml/html format through the command line, and store it in the path specified by the user. Plugin name: pytest-html

How to use: Command line format: pytest --html=user path/report.html

Example:

import pytest
class Test_ABC:
def setup_class(self):
print("------->setup_class")
def teardown_class(self):
print("------->teardown_class")
def test_a(self):
print("------->test_a")
assert 1
def test_b(self):
print("------->test_b")
assert 0 # 断言失败```
运行方式:
1.修改Test_App/pytest.ini文件,添加报告参数,即:addopts = -s --html=./report.html
# -s:输出程序运行信息
# --html=./report.html 在当前目录下生成report.html文件
️ 若要生成xml文件,可将--html=./report.html 改成 --html=./report.xml
2.命令行进入Test_App目录
3.执行命令: pytest
执行结果:
1.在当前目录会生成assets文件夹和report.html文件

6. Summary

pytest is a full-featured Python testing tool that helps you write better programs. It is similar to the unittest testing framework that comes with Python, but pytest is more concise and efficient to use, and is compatible with the unittest framework. pytest supports simple unit testing and complex functional testing. It can combine requests to implement interface testing, combine selenium and appium to realize automated functional testing, and use pytest combined with Allure2 to integrate into Jenkins to achieve continuous integration.

Finally, I would like to thank everyone who has read my article carefully. Reciprocity is always necessary. Although it is not a very valuable thing, you can take it away if you need it:

These materials should be the most comprehensive and complete preparation warehouse for [software testing] friends. This warehouse has also accompanied tens of thousands of test engineers through the most difficult journey, and I hope it can help you! Partners can click the small card below to receive 

Guess you like

Origin blog.csdn.net/OKCRoss/article/details/131055884