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: or
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__":
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 in the root directory of the test run_ first. py
	pytest.main(["-vs","./"])#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 ./ #Run
pytest -vs ./

3. Use pytest Ini run

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:


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 = x) #x is the order of execution

import pytest = 2)
def test_first():
	print("it is first") =1)
def test_second():
	print("it is second")

if __name__ =="__main__":

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
1. Modify pytest Ini is

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

2. Modify py program

import pytest

def test_first():
	print("it is first")

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

if __name__ =="__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