test_python_path.py


Overview

`test_python_path.py` is a test utility file designed to verify the behavior of the `pythonpath` configuration in pytest. It uses the `Pytester` testing helper from [_pytest.pytester](/projects/286/67470) to dynamically create test files, modules, and directories on the fly, and then runs pytest against those files to assert correct import behaviors.

The core purpose of this file is to ensure that when `pythonpath` is specified in pytest's configuration (`pytest.ini`), Python modules located in those paths can be correctly imported in tests. It also verifies that plugins are loaded correctly when using `pythonpath`, and that cleanup after test runs correctly resets the system path.


Detailed Explanation

Imports and Fixtures


Test Functions

1. test_one_dir(pytester: Pytester, file_structure) -> None

2. test_two_dirs(pytester: Pytester, file_structure) -> None

3. test_local_plugin(pytester: Pytester, file_structure) -> None

4. test_module_not_found(pytester: Pytester, file_structure) -> None

5. test_no_ini(pytester: Pytester, file_structure) -> None

6. test_clean_up(pytester: Pytester) -> None


Implementation Details and Algorithms


Interaction with Other System Components


Usage Summary

This file is primarily used during pytest's own test suite development or when testing pytest plugins and configurations related to `pythonpath`. It ensures that:


Visual Diagram

flowchart TD
    A[Fixture: file_structure] --> B[test_one_dir]
    A --> C[test_two_dirs]
    A --> D[test_local_plugin]
    A --> E[test_module_not_found]
    A --> F[test_no_ini]
    G[test_clean_up] --> H[Plugin: pytest_unconfigure hook]

    B --> I[Creates .ini with pythonpath=sub]
    C --> J[Creates .ini with pythonpath=sub sub2]
    D --> K[Creates localplugin.py in sub]
    E --> L[Creates .ini without pythonpath]
    F --> M[No .ini file created]
    G --> N[Creates .ini with dummy pythonpath]

    subgraph "Test Runs"
      I --> O[Run pytest on test_foo.py]
      J --> P[Run pytest on test_foo.py and test_bar.py]
      K --> Q[Run pytest with local plugin]
      L --> R[Run pytest on test_foo.py expecting error]
      M --> S[Run pytest on test_foo.py expecting error]
      N --> T[Run pytest in-process with Plugin]
    end

Summary

`test_python_path.py` is a pytest integration test file that validates the behavior of the `pythonpath` setting in pytest configurations. By dynamically creating test modules and configurations, it verifies import success and failure scenarios, plugin loading behavior, and cleanup mechanisms related to modifying the Python module search path. It is tightly coupled with pytest's internal testing facilities and serves as a crucial check on pytest's support for extended Python paths and plugin discovery.