Batu Lab NotesPractical developer guides

Recognize test filename signals without running tests

By Batu · English technical notes

Also published in our Blogger archive.

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.