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
parameter | Explain in detail |
---|---|
-s | Output debugging information |
-v | Show more detailed information |
-vs | -Combination of s and - v |
-n | Multithreaded test cases, for example: pytest -n 2 # means running test code with two threads |
-x | It means that as long as one test case reports an error, it will stop running |
-k | Specify the test case according to some strings of the test case, for example: pytest -k "first" |
–reruns | Used for failed tests of rerun, for example: pytest --reruns 2 # failed examples rerun 2 times |
–maxfail NUM | When 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.html | Generate 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:
- Unconditional skip: @ pytest mark. skip(reason = “…”)
- 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.