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


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)

markers = slow: marks tests as slow

`filterwarnings`

Filter warnings emitted during tests

filterwarnings = ignore::DeprecationWarning


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

  1. Place the pytest.ini file at the root of your project directory.

  2. Run pytest from the root directory without needing extra command-line options.

  3. Use defined markers in your test files, e.g.,

    import pytest
    
    @pytest.mark.slow
    def test_long_running():
        # test code here
    
  4. Run tests excluding slow tests:

    pytest -m "not slow"
    

Important Implementation Details


Interaction with Other System Components


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:**


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.