01 | Introduction
Pytest is a very mature full-featured Python testing framework, which mainly has the following characteristics:
-
Simple and flexible, easy to use, rich in documentation
-
Supports parameterization for fine-grained control over test cases
-
Supports simple unit testing and complex functional testing, and can also be used for UI and interface automation testing such as Selenium, Appium, and Requests
-
Supports many third-party plug-ins, and can customize extensions (Pytest plug-in download address)
-
Support test case skip and failure retry
-
Can be well integrated with CI tools, such as Jenkins
02 installation
pip install pytest
03 | Simple to use
import pytest
def test01():
print('第一条用例')
assert True
def test02():
print("第二条用例")
assert False
if __name__=="__main__":
# -s:显示用例中的输出
# -v:输出更详细的用例执行信息
# __file__:本文件
pytest.main(["-s", "-v", __file__])
The result of the operation is as follows:
04 | Initialization & Ending
4.1 Function level
Execute the initialization & end method every time a test case is executed
import pytest
class Test:
def setup(self):
print("初始化")
def teardown(self):
print("结束")
def test01(self):
print('第一条用例')
assert True
def test02(self):
print("第二条用例")
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 execute the initialization & end method once
import pytest
class Test:
def setup_class(self):
print("初始化")
def teardown_class(self):
print("结束")
def test01(self):
print('第一条用例')
assert True
def test02(self):
print("第二条用例")
assert False
if __name__=="__main__":
pytest.main(["-s",__file__])
Execution result graph:
05 | Common Plugins
5.1 Test report
Install the test report plugin
pip install pytest-html
Create a pytest.ini configuration file in the project directory, and add a test report storage path in the configuration file.
[pytest]
# 在当前目录下创建report目录存放测试报告
addopts = --html=./report/report.html
Execute the test, and the generated test report is as follows:
5.2 Retry on 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是一个整数,表示重试次数
# --reruns-delay n,n是一个整数,表示重试等待时间,单位是s
The retries of failed use cases can be seen in the test report
Single retry: add a decorator to a use case function, note that the global retry configuration needs to be commented out
# reruns指定重试次数,reruns_delay指定重试间隔
@pytest.mark.flaky(reruns=2, reruns_delay=1)
def test02(self):
print("第二条用例")
assert False
See the retry situation in the test report:
5.3 Multi-process concurrent execution
The emergence of pytest-xdist is to enable distributed execution of automated test cases, thereby saving automated testing time
install plugin
pip install pytest-xdist
Multi-CPU parallel execution
pytest -n 并行测试数量
5.4 Multiple Assertions
When using pytest to make assertion judgments, for the accuracy of use cases, multiple aspects are often asserted, such as:
-
Assertion 1: Assert the status of the http response
-
Assertion 2: Assert the code value returned by 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 an assertion fails and all fail. For example, if the result of assertion 1 is 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 it.
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("测试完成")
if __name__=="__main__":
pytest.main(["-s", "-v", __file__])
The execution result is as shown in the figure below. It can be seen that a total of four assertions have been executed, of which 2 failed assertions
5.5 Beautify the execution result
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 the execution is, and the pytest-sugar plugin can solve our pain points very well.
Install the pytest-sugar plugin
pip install pytest-sugar
Execute the use case file on the command line, the execution result is as shown in the figure below, and there will be a progress bar when the use case is executed:
If my blog is helpful to you, if you like my blog content, please "Like", "Comment", "Favorite" with one click!
The following are supporting learning materials. For friends who do [software testing], it should be the most comprehensive and complete preparation warehouse. This warehouse also accompanied me through the most difficult journey. I hope it can help you too!
Software testing interview applet
The software test question bank maxed out by millions of people! ! ! Who is who knows! ! ! The most comprehensive quiz mini program on the whole network, you can use your mobile phone to do the quizzes, on the subway or on the bus, roll it up!
The following interview question sections are covered:
1. Basic theory of software testing, 2. web, app, interface function testing, 3. network, 4. database, 5. linux
6. web, app, interface automation, 7. performance testing, 8. programming basics, 9. hr interview questions, 10. open test questions, 11. security testing, 12. computer basics
Information acquisition method: