Skip to content

livery.workshop._pytest_layout

livery.workshop._pytest_layout

The test layout rule the suite enforces at session start.

Test modules are named by their path (pytest's importlib mode, which the project template sets), so two packages may both have a tests/test_lifecycle.py. That leaves the tests directories off sys.path; the template puts every package's tests directory on pythonpath instead, one shared path, so a helper module in packages/<dir>/tests/ starts with <dir>_ (dashes as underscores) and two packages never ship the same helper name. A session whose layout breaks that is refused before any test runs, naming each module and the name it wants.

Functions:

Name Description
offenders

Helper modules under a package's tests/ without their package's prefix.

pytest_sessionstart

Refuse the session when a helper module lacks its package's prefix.

Attributes:

Name Type Description
UNNAMED

UNNAMED module-attribute

UNNAMED = ('conftest.py', '__init__.py')

offenders

offenders(root: Path) -> list[tuple[Path, str]]

Helper modules under a package's tests/ without their package's prefix.

Each entry is the module and the name it wants. A directory under packages counts when it carries a workshop.toml; test files and the files every directory may hold are never offenders. The prefix is the package's path under packages/, so a package in a group directory carries the group's name too (extensions_widgets_).

Source code in packages/workshop/src/livery/workshop/_pytest_layout.py
def offenders(root: Path) -> list[tuple[Path, str]]:
    """Helper modules under a package's ``tests/`` without their package's prefix.

    Each entry is the module and the name it wants. A directory under
    ``packages`` counts when it carries a ``workshop.toml``; test files
    and the files every directory may hold are never offenders. The
    prefix is the package's path under ``packages/``, so a package in
    a group directory carries the group's name too
    (``extensions_widgets_``).
    """
    from livery.workshop import package_directories

    found: list[tuple[Path, str]] = []
    for package in package_directories(root):
        tests = package / "tests"
        if not (package / "workshop.toml").is_file() or not tests.is_dir():
            continue
        member = package.relative_to(root / "packages").as_posix()
        prefix = member.replace("-", "_").replace("/", "_") + "_"
        for module in sorted(tests.glob("*.py")):
            if module.name.startswith("test_") or module.name in UNNAMED:
                continue
            if not module.name.startswith(prefix):
                found.append((module, prefix + module.name))
    return found

pytest_sessionstart

pytest_sessionstart(session: Session) -> None

Refuse the session when a helper module lacks its package's prefix.

Source code in packages/workshop/src/livery/workshop/_pytest_layout.py
def pytest_sessionstart(session: pytest.Session) -> None:
    """Refuse the session when a helper module lacks its package's prefix."""
    root = Path(session.config.rootpath)
    bad = offenders(root)
    if not bad:
        return
    listed = "\n".join(
        f"  {path.relative_to(root)}: rename to {wanted}" for path, wanted in bad
    )
    raise pytest.UsageError(
        "a helper module in a package's tests directory carries the package's"
        " name, so every tests directory can share one pythonpath without a"
        f" clash:\n{listed}"
    )