conftest.py


Overview

This file customizes **pytest's test collection behavior at the directory level** by implementing a manifest-driven collection strategy. Instead of pytest’s default approach of discovering test files by filename patterns (e.g., `test_*.py`), this file enables pytest to read a `manifest.json` file inside a test directory and **collect only the test files explicitly listed there**.

This approach offers precise and deterministic control over which test files are collected, helping manage large or complex test suites where not all files in a directory should be considered tests or where custom selection rules are necessary.


Detailed Explanation

Class: ManifestDirectory

**Inheritance:** `pytest.Directory`

This class is a **custom directory collector** that overrides the default directory collection behavior by reading a manifest file and collecting only the files listed in it.

Method: collect(self)


Function: pytest_collect_directory(path, parent)


Important Implementation Details and Algorithms


Interaction with Other System Components


Visual Diagram

flowchart TD
    A[pytest starts directory collection] --> B{Is manifest.json present?}
    B -- Yes --> C[Instantiate ManifestDirectory collector]
    C --> D[ManifestDirectory.collect() called]
    D --> E[Read manifest.json and parse file list]
    E --> F[For each file in manifest]
    F --> G[Call ihook.pytest_collect_file(file_path)]
    G --> H[Yield collected test file nodes]
    H --> I[pytest processes collected tests]
    B -- No --> J[Use default pytest Directory collector]
    J --> I

Summary

The `conftest.py` file provides a **custom pytest directory collector** that reads a `manifest.json` file to determine exactly which test files to collect. This approach overrides the default filename pattern-based collection behavior on a per-directory basis, offering fine-grained control over test discovery.

By implementing the `ManifestDirectory` collector and hooking into `pytest_collect_directory`, this file integrates seamlessly with pytest’s collection framework while enabling deterministic and manageable test collection in complex test suites.


References


*End of conftest.py Documentation*