1 Create a file
In the root directory of the project, create a namedJust the file. pytest will automatically load the file when the test is executed and read the configuration information therein.
2 Common parameters and configuration methods
The following are common parameters and their configuration methods in files:
-
markers
: Define the test mark. For example, you can define aslow
Tags to indicate slow tests:
[pytest] markers = slow: mark test as slow
-
addopts
: Specify the default pytest option. For example, you can specify-rA
Options to display test results:
[pytest] addopts = -rA
-
testpaths
: Specify the test file path. For example, you can specifytests
The directory is the test file path:
[pytest] testpaths = tests
-
norecursedirs
: Specifies the directory that pytest ignores. For example, it can be ignored.git
andvenv
Table of contents:
[pytest] norecursedirs = .git venv
-
python_files
: Specify pytest to find only files containing the specified filename pattern. For example, just look uptest_*.py
document:
[pytest] python_files = test_*.py
-
python_functions
: Specify pytest to find only functions containing the specified function name pattern. For example, just look up totest_
Functions at the beginning:
[pytest] python_functions = test_*
-
python_classes
: Specify pytest to find only classes containing the specified class name pattern. For example, just look up toTest
Classes that begin:
[pytest] python_classes = Test*
3 Advanced configuration
In addition to common parameter configurations, the file also supports some advanced configuration options, such as:
-
markers
Tag parameters: Multiple test tags can be defined, each tag can specify a description for marking test cases in test reports. -
xfail_strict
: Default is False. If set to True, all are marked asxfail
All test cases must fail, otherwise the test will fail. -
disable_test_id_escaping_and_forfeit_all_rights_to_community_support
: Default is False. If you set it to True, you can use a wider range of characters in the test ID, but you lose the right to community support.
4 Conclusion
Through the editor's introduction, you should have mastered the common configuration methods and advanced configuration options of files. In actual projects, rational configuration of files can greatly improve testing efficiency and quality. It is recommended that you make appropriate configurations according to project needs.
This is the end of this article about in-depth understanding of configuration methods and parameters. For more related configuration methods and parameters, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!