Skip to content

fix(py_test): Support target names with a test_ prefix #607

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

nahueespinosa
Copy link

Adds a test_main argument to py_pytest_main and propagates it from py_test. Uses a leading "." in the filename to suppress pytest collection.

In the current implementation, when pytest_main = True, the generated test entry file is named after the target with a .pytest_main.py extension. If the target name starts with test_, pytest may mistakenly collect the file as a test module, leading to confusing import errors.

==================================== ERRORS ====================================
_________ ERROR collecting examples/pytest/test_prefix.pytest_main.py __________
ImportError while importing test module '/home/nahue/.cache/bazel/_bazel_nahue/eb153f5901d0d2e97fac7a89d3cffa65/sandbox/linux-sandbox/402/execroot/aspect_rules_py/bazel-out/k8-fastbuild/bin/examples/pytest/test_prefix.runfiles/aspect_rules_py/examples/pytest/test_prefix.pytest_main.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
../../../../../../../../../../../../external/python_toolchain_x86_64-unknown-linux-gnu/lib/python3.9/importlib/__init__.py:127: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
E   ModuleNotFoundError: No module named 'test_prefix'

Alternatively, if this naming convention is decided to not be supported, we could maybe warn the user in a more friendly way when py_test is used with a name that may trigger this issue.


Changes are visible to end-users: yes

  • Searched for relevant documentation and updated as needed: yes
  • Breaking change (forces users to change their own code or config): no
  • Suggested release notes appear below: yes

fix(py_test): Target names with a test_ prefix do not generate an import error when specifying pytest_main = True.

Test plan

  • New test cases added

Signed-off-by: Nahuel Espinosa <nahue.espinosa@gmail.com>
@CLAassistant
Copy link

CLAassistant commented Jun 28, 2025

CLA assistant check
All committers have signed the CLA.

Copy link

aspect-workflows bot commented Jun 28, 2025

Test

4 test targets passed

Targets
//docs:update_2_test [k8-fastbuild]                           39ms
//examples/pytest:pytest_test [k8-fastbuild]                  2s
//examples/pytest:sharded/test [k8-fastbuild]                 3s
//examples/pytest:test_prefix [k8-fastbuild]                  1s

Total test execution time was 6s. 38 tests (90.5%) were fully cached saving 1m 5s.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants