Skip to content

create-module: identical tests/test_module.py basename breaks pytest collection in multi-module repos #264

Description

@antosubash

Summary

smpy create-module always names the generated test file tests/test_module.py. Since smpy new scaffolds a workspace whose members live under modules/*, the second module you create in a repo makes root-level pytest fail during collection:

ERROR collecting modules/geowiki_articles/tests/test_module.py
import file mismatch:
imported module 'test_module' has this __file__ attribute:
  /…/modules/geowiki_apps/tests/test_module.py
which is not the same as the test file we want to collect:
  /…/modules/geowiki_articles/tests/test_module.py
HINT: remove __pycache__ / .pyc files and/or use a unique basename for your test file modules

make test runs uv run pytest from the repo root, so this breaks the scaffolded quality gate — with 5 modules I got 4 collection errors and 0 tests run.

Cause

The tests/ directories have no __init__.py, so pytest's default prepend import mode derives the module name from the basename alone. Five files all named test_module collide.

Reproduction

smpy new demo --preset standard --yes --no-install
cd demo
smpy create-module alpha --dest modules/alpha
smpy create-module beta  --dest modules/beta
uv sync --all-packages
uv run pytest        # collection error

Suggested fix

Any one of these works; the first is the smallest change:

  1. Name the file after the moduletests/test_{{PACKAGE_NAME}}.py instead of tests/test_module.py. Also reads better in output.
  2. Emit an empty tests/__init__.py in the module template, making the test modules properly packaged and their names unique.
  3. Set [tool.pytest.ini_options] importmode = "importlib" in the workspace template's pyproject.toml.

I went with (1) locally.

Environment

  • simple_module_cli 0.0.30, pytest 8.x, Python 3.14

Minor, but it hits every multi-module repo on the first make test, which is also the first thing the scaffold's README tells you to run.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions