← Back to issue list

Charm with name beginning `test` fails unit tests

View original Github issue

Metadata

Project
charmcraft
Number
#2437
Type
issue
State
open
Author
dwilding
Labels
Created
2025-09-10 03:01:48+00:00
Updated
2025-11-05 08:23:57+00:00
Closed

Current evaluation

No evaluation has been recorded for this issue yet.

Issue body

### Bug Description If I initialize a charm whose name begins with `test`, the charm's unit tests fail. For example, if the name is `test-application`. Cause: `tests/unit/test_charm.py` imports `src/charm.py`, which has a class called `TestApplicationCharm`. pytest tries to interpret this as a test class, but fails with a warning. Possible ways to fix: - Use `collect_imported_tests = false` in the charm's pytest config. See [pytest discussion](https://github.com/pytest-dev/pytest/discussions/12748#discussioncomment-13780875). It's probably not a good idea to enable this by default in the templates. - Use `__test__ = False` in the body of the charm class. Would Charmcraft only add this if the charm name begins with `test`? - Change the format of the name of the charm class, to avoid the issue. Again, maybe only if the charm name begins with `test`? - Have Charmcraft reject charm names that being with `test`. (Seems a bit strong) ### To Reproduce ``` mkdir test-application cd test-application charmcraft init --profile=machine tox -e unit ``` ### Environment n/a ### charmcraft.yaml ```yaml (as generated by charmcraft init --profile=machine) ``` ### Relevant log output ```shell $ tox -e unit ... ~/test-application/src/charm.py:17: PytestCollectionWarning: cannot collect test class 'TestApplicationCharm' because it has a __init__ constructor (from: tests/unit/test_charm.py) class TestApplicationCharm(ops.CharmBase) ... ```

Evaluation history

No evaluation history available.