First day of pytest study

Common operations of pytest

This article is used to record the process of learning pytest. I hope you can point out any mistakes in combination with learning in station B. If there is any infringement, please send a private letter!

General naming rules

1. Module name (py file) in test_ Begin or begin with_ End of test; For example: test_first.py or first_test.py
2. The class (class) in the py file starts with Test, for example: class Testfirst:
3. The function in the py file starts with test, for example: def testfirst:

Operation mode

There are three main ways to run pytest:
1. Run as main function

import pytest
def testfirst():
	print("hello world!")
class Testfirst:
	def test_second(self):
		print("hello pytest!")
		
if __name__ =="__main__":
	pytest.main()  
'''
main It can specify the operation file and operation
 For example:
	pytest.main(["-vs"]) #Test run all test cases. "- vs" is explained in detail below. It's not urgent
	pytest.main(["-vs","./test_first.py"]) #Test in the root directory of the test run_ first. py
	pytest.main(["-vs","./test_firsr.py::testfirst"])#Test only run test_ first. testfirst function in PY
'''

2. Run as named rows
Input operation in the terminal, and the parameters are the same in the main function mode

pytest -vs  #Run all
pytest -vs ./test_first.py #Run test_first.py
pytest -vs ./test_first.py::testfirst

3. Use pytest Ini run

[pytest]
addopts = -vs
test_path = ./
python_files = test_*.py
python_classes = Test
python_functions = test

pytest.ini files are written using ANSI encoding. pytest.ini can modify general rules. Finally, input at the terminal:

pytest

Detailed explanation of parameters

parameterExplain in detail
-sOutput debugging information
-vShow more detailed information
-vs-Combination of s and - v
-nMultithreaded test cases, for example: pytest -n 2 # means running test code with two threads
-xIt means that as long as one test case reports an error, it will stop running
-kSpecify the test case according to some strings of the test case, for example: pytest -k "first"
–rerunsUsed for failed tests of rerun, for example: pytest --reruns 2 # failed examples rerun 2 times
–maxfail NUMWhen the number of failed test cases reaches NUM, it will stop. For example, pytest --maxfai 2 # test cases will stop after two failures
-html ./report/report.htmlGenerate html test report and store it in/ report/report. Within html

Change test execution sequence

The pytest test is executed from top to bottom. If you want to change the program running order, add a line on the function
@pytest.mark.run(order = x) #x is the order of execution

import pytest

@pytest.mark.run(order = 2)
def test_first():
	print("it is first")
@pytest.mark.run(order =1)
def test_second():
	print("it is second")

if __name__ =="__main__":
	pytest.main(["-vs"])

To execute the above procedure, execute test first_ Second, execute test again_ first.

Skip use case

There are two main types:

  1. Unconditional skip: @ pytest mark. skip(reason = “…”)
  2. Conditional skip: @ pytest mark. Skipif (1 = = 1, reason = "...") #1 = = 1 is the condition, and reason is the reason string printed

Group execution

Function: execute only the selected routine code
Steps:
1. Modify pytest Ini is

[pytest]
addopts = -vs -m "smoke"
test_path = ./
python_files = test_*.py
python_classes = Test
python_functions = test
markers = smoke

2. Modify py program

import pytest

@pytest.mark.smoke
def test_first():
	print("it is first")

def test_second():
	print("it is second")

if __name__ =="__main__":
	pytest.main()

The result after execution is: only test is tested_ First function.

Keywords: Python pytest

Added by olanjouw on Sun, 16 Jan 2022 07:45:47 +0200