SoFunction
Updated on 2025-03-03

Deeply understand configuration methods and parameters in python

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 aslowTags to indicate slow tests:
[pytest]
markers =
    slow: mark test as slow
  • addopts: Specify the default pytest option. For example, you can specify-rAOptions to display test results:
[pytest]
addopts = -rA
  • testpaths: Specify the test file path. For example, you can specifytestsThe directory is the test file path:
[pytest]
testpaths = tests
  • norecursedirs: Specifies the directory that pytest ignores. For example, it can be ignored.gitandvenvTable of contents:
[pytest]
norecursedirs = .git venv
  • python_files: Specify pytest to find only files containing the specified filename pattern. For example, just look uptest_*.pydocument:
[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 toTestClasses 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:

  • markersTag 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 asxfailAll 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!