1. Run in command line mode
Execute all use cases in a certain directory and all use cases that meet the specification
Enter the corresponding directory and execute pytest directly; for example, you need to execute all use cases under testcases; you can enter the testcases directory; then execute pytest
Enter the upper-level directory of the corresponding directory, and execute the pytest directory name/ ; ; For example, you need to execute all the use cases under testcases; it can be in the study_project directory; and then execute pytest testcases/
Execute the use case under a certain py file
pytest script name.py
Run a function in the .py module
pytest test_mod.py::TestClass
Run a function in the .py module
pytest test_mod.py::test_func
In the run.py module, a certain test class
pytest test_mod.py::TestClass
Run the .py module, test a method in the class
pytest test_mod.py::TestClass::test_method
Various combinations
pytest test_mod1.py::TestClass::test_method test_mod2.py
mark run
pytest -m login run the use case marked as login
Two, the main method runs
Generally specify the path and parameters that need to be run in the root directory of the project
3. Through the global configuration file pytest.ini
Where should pytest.ini be placed? Just put it in the root directory of the project, don't mess around, don't mess with other names
Common configuration items
marks
Function: The @pytest.mark.webtest decorator is added to the test case. If the marks option is not added, warnings will be reported
Format: list list type
Writing:
# mark
markers =
smoke:冒烟用例
order
: 订单用例
product: 商品用例
Change test case collection rules
pytest default test case collection rules
- Filenames start with test_*.py files and *_test.py
- functions starting with test_
- A class starting with test cannot contain an __init__ method
- Methods in classes starting with test_
We can modify or add this use case collection rule; of course, it is recommended to add it to the original rule, as follows:
testpaths=testcases/test_study/
python_files =test_* *_test test*
python_classes =Test* test*
python_functions =test_* test*
Remarks: testpaths=testcases/test_study/ If pytest is executed in the root directory of the project, then the current execution directory will be used as the relative path, and the files under test_study under the testcases in the root directory will be executed; others will not be executed
For example:
output:
addopts
Function: The addopts parameter can change the default command line options. When we enter a bunch of instructions in cmd to execute the use case, we can use this parameter instead, saving the repetitive work of typing commands
# 例如命令行参数,失败重跑两次,一共运行两次,生成测试报告
addopts = -v --reruns=
1
--count=
2
--html=reports.html --self-contained-html -n=auto
log_cli
Function: console real-time output log
Format: log_cli=true or false (default), or log_cli=1 or 0