pytest.ini
Overview
The `pytest.ini` file is a configuration file used by the **pytest** testing framework in Python projects. Its primary purpose is to define and customize pytest’s behavior for the project, such as specifying test discovery rules, setting command-line options, configuring plugins, and defining markers. This file enables consistent and repeatable test runs by centralizing pytest settings, reducing the need for repetitive command-line flags or inline configurations.
This file is not a Python script but an INI-format text file that pytest automatically detects when running tests in the project directory hierarchy.
Purpose and Functionality
Customization of pytest behavior: Control test selection, reporting, logging, and plugin options.
Centralized management: Store test-related settings in one place for all developers and CI environments.
Marker definitions: Define custom markers to categorize and filter tests.
Plugin configuration: Enable or configure pytest plugins with specific options.
Compatibility: Supports various pytest settings such as test paths, Python warnings, and command-line arguments.
Typical Sections and Keys in pytest.ini
Though the provided file content is empty, a standard `pytest.ini` typically contains the following sections and keys:
[pytest] Section
This is the main section where nearly all pytest configuration options are placed.
Option | Description | Example |
|---|---|---|
`minversion` | Minimum pytest version required | `minversion = 6.0` |
`addopts` | Additional command line options to pass to pytest | `addopts = -ra -q` |
`testpaths` | List of directories or files where pytest will look for tests | `testpaths = tests` |
`python_files` | Glob pattern for Python test files | `python_files = test_*.py` |
`python_classes` | Glob pattern for test classes | `python_classes = Test*` |
`python_functions` | Glob pattern for test functions | `python_functions = test_*` |
`markers` | Custom marker definitions (to avoid warnings) | |
`filterwarnings` | Filter warnings emitted during tests |
Example pytest.ini File Content
[pytest]
minversion = 6.0
addopts = -ra -q
testpaths = tests
python_files = test_*.py
python_classes = Test*
python_functions = test_*
markers =
slow: marks tests as slow (deselect with '-m "not slow"')
filterwarnings =
ignore::DeprecationWarning
Usage Example
Place the
pytest.inifile at the root of your project directory.Run
pytestfrom the root directory without needing extra command-line options.Use defined markers in your test files, e.g.,
import pytest @pytest.mark.slow def test_long_running(): # test code hereRun tests excluding slow tests:
pytest -m "not slow"
Important Implementation Details
INI format: The file follows the standard INI format, with sections denoted by
[section]and key-value pairs.Automatic discovery: pytest automatically looks for
pytest.ini,tox.ini, orsetup.cfgfiles for configuration.Marker registration: Defining markers here prevents pytest warnings about unknown markers.
Extensibility: Adding sections for plugins (e.g.,
[pytest-watch]) is possible to configure third-party behavior.
Interaction with Other System Components
Test runner: pytest reads this file before test collection and execution.
Plugins: This file configures plugins that extend pytest’s core functionality.
CI/CD pipelines: Ensures consistent test runs across environments without manually passing CLI options.
Test code: Test markers and naming conventions defined here influence which tests are run or skipped.
Visual Diagram: pytest.ini Configuration Flow
flowchart TD
A[pytest.ini] --> B[pytest Test Runner]
B --> C[Test Collection]
B --> D[Plugin Configuration]
C --> E[Test Execution]
D --> E
E --> F[Test Reporting]
F --> G[Console / CI Logs]
**Diagram Explanation:**
pytest.iniprovides configuration inputs to the pytest test runner.The test runner uses this config during test collection and plugin setup.
Plugins and collected tests influence the execution phase.
Final results are reported to console or CI logs.
Summary
The `pytest.ini` file is a key configuration file enabling flexible, repeatable, and maintainable test runs using pytest. By centralizing test-related settings, it improves developer productivity and ensures consistent behavior across different environments. Although it is a simple text file, it plays a crucial role in the testing workflow within Python projects.