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


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


Interaction with Other Parts of the System


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:

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`.**