test_pathlib.py


Overview

`test_pathlib.py` is a comprehensive test suite for verifying the functionality of the `_pytest.pathlib` module and related utilities in the pytest testing framework. Its main focus is to ensure correctness, robustness, and compatibility of various path and import-related operations, including:

The tests use pytest fixtures and parametrization extensively to cover a wide range of cases and platforms (notably Windows vs others).


Detailed Explanations

Fixtures

autouse_pytester(pytester: Pytester) -> None

ns_param(request: pytest.FixtureRequest) -> bool


Classes and Their Tests

TestFNMatcherPort


TestImportPath


TestImportLibMode


TestNamespacePackages


Standalone Test Functions


Important Implementation Details & Algorithms


Interaction with Other Parts of the System


Usage Examples

Importing a Python module from a path considering namespace packages

from _pytest.pathlib import import_path

module = import_path(
    path=Path("/path/to/module.py"),
    root=Path("/path/to"),
    consider_namespace_packages=True,
    mode="importlib"
)
print(module.__name__)

Using fnmatch_ex for pattern matching of paths

from _pytest.pathlib import fnmatch_ex

if fnmatch_ex("tests/**/test_*.py", "tests/unit/test_example.py"):
    print("Matched!")

Mermaid Class Diagram

The file is primarily a test suite containing test classes, so a class diagram is appropriate to show the main test classes and their key methods.

classDiagram
    class TestFNMatcherPort {
        +test_matching(pattern, path)
        +test_matching_abspath()
        +test_not_matching(pattern, path)
    }
    class TestImportPath {
        +setuptestfs(path)
        +test_smoke_test(path1, ns_param)
        +test_import_path_missing_file(path1, ns_param)
        +test_renamed_dir_creates_mismatch(tmp_path, monkeypatch, ns_param)
        +test_messy_name(tmp_path, ns_param)
        +test_dir(tmp_path, ns_param)
        +test_a(path1, ns_param)
        +test_b(path1, ns_param)
        +test_c(path1, ns_param)
        +test_d(path1, ns_param)
        +test_import_after(tmp_path, ns_param)
        +test_check_filepath_consistency(monkeypatch, tmp_path, ns_param)
        +test_ensuresyspath_append(tmp_path, ns_param)
        +test_invalid_path(tmp_path, ns_param)
        +test_importmode_importlib(simple_module, tmp_path, request, ns_param)
        +test_remembers_previous_imports(simple_module, tmp_path, ns_param)
        +test_no_meta_path_found(simple_module, monkeypatch, tmp_path, ns_param)
    }
    class TestImportLibMode {
        +test_importmode_importlib_with_dataclass(tmp_path, ns_param)
        +test_importmode_importlib_with_pickle(tmp_path, ns_param)
        +test_importmode_importlib_with_pickle_separate_modules(tmp_path, ns_param)
        +test_module_name_from_path(tmp_path)
        +test_resolve_pkg_root_and_module_name(tmp_path, monkeypatch, pytester)
        +test_insert_missing_modules(monkeypatch, tmp_path)
        +test_import_module_using_spec(b_is_package, insert_modules, tmp_path)
        +test_parent_contains_child_module_attribute(monkeypatch, tmp_path)
        +test_importlib_package(monkeypatch, tmp_path, ns_param)
        +test_importlib_root_is_package(pytester)
        +test_importlib_same_name_as_stl(pytester, ns_param, tmp_path, name)
        +test_import_using_normal_mechanism_first(monkeypatch, pytester, ns_param)
        +test_import_using_normal_mechanism_first_integration(monkeypatch, pytester, ns_param)
        +test_import_path_imports_correct_file(pytester, ns_param)
    }
    class TestNamespacePackages {
        +setup_directories(tmp_path, monkeypatch, pytester)
        +test_resolve_pkg_root_and_module_name_ns_multiple_levels(tmp_path, monkeypatch, pytester, import_mode)
        +test_ns_multiple_levels_import_rewrite_assertions(tmp_path, monkeypatch, pytester)
        +test_ns_multiple_levels_import_error(tmp_path, pytester)
        +test_incorrect_namespace_package(tmp_path, monkeypatch, pytester, import_mode)
        +test_detect_meta_path(tmp_path, monkeypatch, pytester)
        +test_full_ns_packages_without_init_files(pytester, tmp_path, monkeypatch, insert)
    }

Summary

This test module rigorously validates pytest's pathlib and import mechanisms, including complex scenarios like namespace packages, import modes, and filesystem edge cases. It helps maintain the reliability of pytest's internal import utilities and path abstractions critical for test discovery and execution.