← 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
Updated
Closed

Current evaluation

Charm names starting with 'test' cause pytest to misidentify the generated charm class (e.g. TestApplicationCharm) as a test class, failing unit tests. Maintainers discussed on 2025-11-05 and prefer rejecting such names at init time. No fix implemented yet; no labels or assignee.

Suggested action: keep open

Reason: Maintainers @james-garner-canonical and @benhoyt actively discussed the issue on 2025-11-05, with @benhoyt stating 'We (Charm Tech) would be keen on KISS: Have Charmcraft reject charm names that begin with test.' The bug is confirmed reproducible, the fix direction is agreed upon, but no code change has landed (no `__test__` or name-rejection logic found in init.py). The issue remains valid and actionable.

Impact: 30 Quick Win: 22.5 Staleness: 45 Complexity: 25 Confidence: 72 Support Request: 5

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

Date Model Scores Action Summary
qwen/qwen3.8-27b
Impact: 30
Quick Win: 22.5
Staleness: 45
Complexity: 25
Confidence: 72
Support Request: 5
keep open Charm names starting with 'test' cause pytest to misidentify the generated charm class (e.g. TestApplicationCharm) as a test class, failing unit tests. Maintainers discussed on 2025-11-05 and prefer rejecting such names at init time. No fix implemented yet; no labels or assignee.
qwen/qwen3.6-35b-a3b
Staleness: 75
Complexity: 15
Confidence: 60
Support Request: 0
needs triage Charmcraft generates unit test failures when charm names start with test due to pytest collecting the charm class. Currently under discussion among contributors regarding whether to reject such names or add a __test__ flag.
qwen3.6-35b-a3b-mtp-q6
Staleness: 65
Complexity: 20
Confidence: 75
Support Request: 10
needs triage Charmcraft generates unit tests that fail when charm names start with test due to pytest collection conflicts. Currently under discussion among maintainers regarding whether to reject such names or add configuration.
qwen3.6-35b-a3b-mtp-q6
Staleness: 60
Complexity: 20
Confidence: 85
Support Request: 0
keep open Charmcraft generates unit tests that fail when charm names start with test due to pytest collection conflicts. Maintainers discussed and proposed rejecting such names with a helpful error message.
qwen3.6-35b-a3b-mtp-q6
Staleness: 45
Complexity: 20
Confidence: 85
Support Request: 0
keep open Charms named starting with test fail unit tests as pytest collects the charm class as a test class. Under discussion, the Charm Tech team leans toward rejecting such names in Charmcraft while suggesting alternatives like foo-test.

Update history

No update history recorded yet.

Related issues

No related issues found above the similarity threshold.