pytest configuration file: pytest.ini

1. Start

The pytest configuration file can change the default running mode of pytest. It is a fixed file name pytest.ini.

The storage path of pytest.ini is the root path of the project.

2. Use addopts- to append default parameters

Every time we execute the pytest command on the command line, if the parameters are fixed, they can be configured in pytest.ini to reduce tedious operations.
When pytest is running, it will automatically read the parameters configured in pytest.ini.

For example:

[pytest]
addopts =
    --reruns=2
    -p no:faulthandler
    -s
    --cache-clear
    --capture=sys

Parameters can also be separated by spaces.

[pytest]
addopts = --reruns=2 -p no:faulthandler -s --cache-clear --capture=sys

3. The command line parameter priority is greater than the parameters of pytest.ini

When the parameters in the configuration file overlap with those in the command file, the parameter values ​​on the command line will override the parameter values ​​defined in the configuration file. ,

For example, in the figure below, the command line parameter value is 1, the pytest.ini parameter value is 2, and the actual execution parameter value is 1.

4. testpaths-specify test case search directory

testpaths is used to specify the search directory for test cases. You can specify one or more directories. Multiple directories need to be separated by spaces or newlines.

[pytest]
testpaths = ./test_case01 ./test_case02

[pytest]
testpaths = 
  ./test_case01 
  ./test_case02

5. python_files & python_classes & python_functions modify the rules of pytest’s default search case

pytest default use case matching rules:

  • Test modules must start with test_ or end with _test
  • The test class must start with Test and cannot have init ()
  • Test methods must start with test_

The pytest.ini configuration file can use python_files, python_calsses, and python_functions to modify the default use case matching rules.
  • Use python_files to customize the matching rules of the test module
  • Use python_classes to customize the matching rules of test classes
  • Use python_functions to customize the matching rules of test methods

Note: The above three configuration items support multiple matching rules. Multiple matching rules should be separated by spaces or newlines.

Next, we can add our own matching rules:

  • Add gitlink*.py the test module at the beginning
  • Add GitLink*a test class starting with
  • Add gitlink*a test method at the beginning
[pytest]
# 配置搜索的模块文件名称。匹配以test开头py结尾的文件
python_files = gitlink*.py

# 配置搜索的测试类名
python_classes = Gitlink*

# 配置搜索的测试方法名
python_functions = gitlink*

Command line input: pytest -vsThe running results are as follows:

6. markers-register mark mark

When we use @pytest.mark. mark name, if a custom mark is used, when the parameter -m=mark name is appended to the execution case, although it will not affect the test execution, an alarm prompt will appear after execution: PytestUnknownMarkWarning: Unknown pytest.mark.smoke.

We need to add the markers field in the pytest.ini configuration file to register custom markers.

[pytest]
# 注册标记名称
markers =
    smoke: 冒烟测试用例
    normal: 正常用例

After adding, you pytest --markerscan use to view the tag name we added.

Then when we use the command again, pytest -m=smokethere will be no warning~

Guess you like

Origin blog.csdn.net/FloraCHY/article/details/131823489