Recognize test filename signals without running tests

Direct answer

A read-only scan can recognize filenames such as test_*.py and *_test.py without importing a module or starting a test runner. The example looks only in two declared locations, then prints a count marked executed=no. That makes the observation useful for inventory while avoiding a false claim about pass status.

The edge case is a Python file whose name looks like a test but has never been collected by the project’s real framework. It still appears in this signal because naming and execution are different facts. Conversely, a framework can run tests with a convention this scanner does not know.

The code intentionally does not recurse through every directory, load configuration, or evaluate decorators. Extend the supported patterns only as a documented scanner change. If a release decision needs test evidence, consume a separate record from the actual test command rather than upgrading this filename count into a result.

Complete example

from pathlib import Path
from tempfile import TemporaryDirectory

with TemporaryDirectory() as directory:
    root = Path(directory)
    (root / "tests").mkdir()
    (root / "tests" / "test_api.py").write_text("", encoding="utf-8")
    (root / "src_test.py").write_text("", encoding="utf-8")
    (root / "src.py").write_text("", encoding="utf-8")
    matches = sorted(item.name for item in root.glob("*.py") if item.name.endswith("_test.py"))
    matches += sorted(item.name for item in (root / "tests").glob("test_*.py"))
    assert matches == ["src_test.py", "test_api.py"]
    print("test_name_signals=2 executed=no")

Expected stdout:

test_name_signals=2 executed=no

Sources

- pathlib documentation

Prepared with AI assistance. The example uses synthetic data; its stated limits apply.

Comments

Popular posts from this blog

Compare Two Text Files in Python Without Modifying Them

Seven CSV Quality Checks to Run Before Importing Data

Explain why SQLite transactions cannot make an HTTP call atomic