pytest.ini
Overview
`pytest.ini` is a configuration file used by the **pytest** testing framework to customize its behavior for the current project or workspace. This file allows developers to specify various pytest options and settings globally, eliminating the need to provide command-line arguments each time tests are run.
The primary purpose of this particular `pytest.ini` file is to configure pytest to recognize which files should be considered as test files when running test discovery. By default, pytest looks for test files following certain naming conventions, but this file overrides or confirms that behavior to ensure all Python files ending with `.py` are included during test collection.
Detailed Explanation
Configuration Section: [pytest]
This section header defines the scope of the configuration settings relevant to the pytest tool.
python_files = *.py
Description:
This option instructs pytest on the filename pattern(s) it should recognize as test files.Default behavior:
By default, pytest searches for files matching the patternstest_*.pyor*_test.py.Configured behavior:
Here,python_files = *.pytells pytest to consider all Python files ending with.pyas potential test modules, regardless of their naming convention.Effect:
This broadens the scope of test discovery significantly, enabling tests located in any.pyfile to be collected and executed by pytest.
Usage Example
Assuming you have the following files in your project directory:
test_example.py
example_test.py
example.py
With the default pytest behavior, only `test_example.py` and `example_test.py` would be discovered and run as test files. However, with the `pytest.ini` configuration:
[pytest]
python_files = *.py
pytest will also include `example.py` as a candidate for test discovery and execution.
Implementation Details and Notes
The
pytest.inifile uses an INI-style syntax where options are grouped under sections in square brackets.The
python_filesoption accepts glob-style patterns (e.g.,*.py,test_*.py, etc.).This file must be located in the root directory of the project or a parent directory where pytest is invoked; pytest automatically discovers it during test runs.
This configuration affects test collection phase only and does not influence test execution behavior directly.
Using a broad pattern like
*.pycan lead to non-test Python files being collected as tests, which may slow down test runs or cause unexpected failures if those files contain code not structured as tests.
Interaction with Other Parts of the System
This file interacts with the pytest testing framework exclusively.
It influences pytest's test discovery mechanism, dictating which files are parsed and searched for test functions and classes.
It complements or overrides command-line options passed to pytest, offering a persistent project-level configuration.
Other pytest configuration files (
tox.ini,setup.cfg, orpyproject.toml) can also define pytest settings;pytest.initakes precedence if present.This configuration indirectly affects Continuous Integration (CI) pipelines and local development workflows by standardizing test discovery rules.
Visual Diagram
Below is a simple flowchart showing how pytest uses the `pytest.ini` file during the test discovery process.
flowchart TD
A[Start pytest run] --> B{Is pytest.ini present?}
B -- Yes --> C[Read pytest.ini configuration]
B -- No --> D[Use default pytest settings]
C --> E[Load python_files pattern (*.py)]
D --> F[Load default python_files pattern (test_*.py, *_test.py)]
E --> G[Discover test files matching *.py]
F --> H[Discover test files matching test_*.py or *_test.py]
G --> I[Collect tests from discovered files]
H --> I
I --> J[Execute collected tests]
Summary
The `pytest.ini` file is a minimal yet powerful configuration file for pytest, enabling project-wide customization of test discovery rules. In this file, the only configuration present is:
python_files = *.py— instructing pytest to treat all Python files as test files.
This can greatly simplify test organization in projects where tests do not follow pytest's default naming conventions but should be used with caution to avoid unintended test file inclusions.
**End of documentation for `pytest.ini`.**