Configuration
pytest allows you to customize its behavior through configuration files. These settings control test discovery, default command-line options, custom markers, plugins and more. Configuration is typically defined in a pytest.ini file placed at the project root. You may also put pytest settings inside tox.ini if you are using tox (a tool that automates testing across multiple isolated virtual environments).
A sample pytest configuration file looks like this:
# pytest.ini
[pytest]
minversion = 6.0
addopts = -rsxX -l
testpaths = src/api_tests src/unit_tests
norecursedirs = .git venv build dist
python_files = test_*.py
python_classes = Test*
python_functions = test_*
xfail_strict = true
markers =
slow: marks tests as slow (deselect with -m "not slow")
pytest.ini (or equivalent config file) is used per test run. Pytest stops searching after it finds the first configuration file (pytest.ini, tox.ini, or setup.cfg) relative to the invocation directory.Here are the common pytest configuration settings:
| Setting | Purpose (What it does) |
|---|---|
| minversion | Enforces a minimum required version of pytest before running tests, preventing issues caused by older pytest features or behavior. |
| addopts | Defines default command-line options automatically applied to every pytest run, saving you from repeatedly typing common flags. |
| testpaths | Tells pytest which directories to scan for tests. Useful if your tests aren’t in the default locations or if you want to speed up collection by limiting search paths. |
| norecursedirs | Tells pytest which directories to ignore entirely during test discovery (Ex: build/, env/, .venv/, large data folders). |
| python_files | Defines the filename patterns pytest uses to identify test modules. Defaults include test_*.py or *_test.py. |
| python_classes | Defines the naming pattern for test classes. By default, pytest treats classes starting with Test as test containers. |
| python_functions | Defines the naming pattern pytest uses for test functions. By default, it detects functions beginning with test_. |
| markers | Registers custom markers (Ex: @pytest.mark.slow) to avoid "unknown marker" warnings and to document marker usage. |
| xfail_strict | Makes xfail strict. If enabled, tests marked xfail that unexpectedly pass will be treated as failures (xpass becomes an error). |
Plugins
pytest is designed to be highly extensible. While the core framework is powerful, its plugin system allows you to add new features or modify existing behavior to suit your specific testing needs.
Introduction
pytest is rarely used in isolation. In most real-world testing environments, it acts as a central test runner while integrating smoothly with debugging tools, coverage analyzers, mocking libraries, environment managers and even other testing frameworks. This interoperability is one of pytest’s strongest advantages.
