pytest.ini


Overview

The `pytest.ini` file is a configuration file used by the **pytest** testing framework to customize and control the behavior of test discovery and execution within a project. It enables developers to specify default options, define test paths, configure plugins, set markers, and control various pytest features without needing to pass command-line arguments each time tests run.

This file typically resides at the root of the project directory and influences how pytest runs throughout the project lifecycle, ensuring consistent testing behavior across different development environments and CI pipelines.


Purpose and Functionality


Typical Contents of pytest.ini

While this file is empty in the provided content, a typical `pytest.ini` includes sections and keys such as:

[pytest]
minversion = 6.0
addopts = -ra -q
testpaths = tests
markers =
    slow: marks tests as slow (deselect with '-m "not slow"')
    smoke: quick smoke tests

Explanation of Common Fields

Field

Description

`minversion`

Minimum pytest version required to run tests in this project

`addopts`

Default command line options added automatically when pytest runs

`testpaths`

List of directories to search for tests by default

`markers`

Defines custom markers for categorizing or skipping tests

`filterwarnings`

Controls warning filters (e.g., ignore or error on specific warnings)

`log_cli`

Enables logging output during test runs

`python_files`

Glob patterns for test file names (e.g., `test_*.py`)

`python_classes`

Glob patterns for test class names

`python_functions`

Glob patterns for test function names


How pytest.ini Interacts with the System


Implementation Details and Best Practices


Example Usage

**Example `pytest.ini` file:**

[pytest]
minversion = 6.0
addopts = -v --maxfail=3 --disable-warnings
testpaths = tests integration_tests
markers =
    smoke: quick smoke tests
    slow: slow running tests
    regression: regression tests

**How this affects pytest runs:**


Visual Diagram: pytest.ini Configuration Impact Flowchart

flowchart TD
    A[pytest.ini file] --> B[pytest test runner]
    B --> C{Load Configuration}
    C --> D[Set test discovery paths]
    C --> E[Apply default CLI options]
    C --> F[Register custom markers]
    C --> G[Configure plugins]
    B --> H[Discover tests based on config]
    H --> I[Execute tests]
    I --> J[Produce test reports]

Summary

`pytest.ini` is a critical file for controlling pytest’s behavior in a project. While it contains no code, its configuration directives significantly influence test discovery, execution, and reporting. Proper setup of this file ensures consistent, efficient, and maintainable testing workflows, especially in teams and CI/CD environments.


If you require a more detailed example or integration with other pytest-related files (like [conftest.py](/projects/286/67243) or plugin configs), please provide those files or specify the use case.