museflow / tests
AGENTS.md1 starsChanged 17 days agoMITView on GitHub
Tests are the contract with our users: clean, clear and easy to maintain, not just 100% coverage.
Where files are headingNew AGENTS.md files have more rules about keeping secrets out of the code, and fewer about code style.
Of the AGENTS.md files we read, the 32,671 first seen in the last six months, against the older ones: the kind of rule that gained most share, and the kind that lost most. About files like this in general, not this one, and an estimate, since a kind is a pattern in the words.
In this repository4 other files it gives its agents.
- AGENTS.mdAGENTS.md
- migrations/AGENTS.mdAGENTS.md
- arch-reviewSkill
- security-reviewSkill
The file
We split this AGENTS.md into the rules it gives an agent
11 rules. About 0 are common, 4 are distinctive, and 7 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈4
Distinctive · estimate
This author's own choices. Worth a look.
7
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 9
Do not mirror happy paths: if integration covers the nominal flow, no unit test for the same.
- Distinctive: line 10
Coverage is measured on the whole suite: no dedicated test for a line another test already runs, nor to check a third-party library's …
- Distinctive: line 17
No fake (in-memory) repositories.
- Very specific: line 24
If an assertion must loop, put an identifier in its message: assert item.active, f"Item {i} (id={item.id}) failed".
- Very specific: line 44
Place a fixture as close to its use as possible: class, then module, then conftest.py.
- Very specific: line 50
Use cases get uow (integration: each block runs in a savepoint of the test transaction) or mock_uow (unit: its repositories are the …
- Very specific: line 53
Never
with mock.patch(...)inside a test: always a pytest.fixture. - Very specific: line 79
Never module-level helpers (_make_*, _build_*): call the factory directly or use a fixture.
- Distinctive: line 85
Don't override what polyfactory generates (UUIDs, fields with defaults).
- Very specific: line 87
Never bound a field to fit its column: SQLAlchemyFactory already reads the column limits (String length, Numeric precision).
- Very specific: line 97
After 3 failed fix attempts on the same problem, stop and report structured options A/B/C.
≈ “Common” is an estimate: at least 1 in 5 of the 41,581 AGENTS.md files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Testing guidelines
Tests are the contract with our users: clean, clear and easy to maintain, not just 100% coverage.
Philosophy
- 100% branch coverage is mandatory.
- Integration tests are the primary focus: full flows with a real DB and WireMock.
- Unit tests fill gaps: error branches and edge cases hard to reach via integration.
- Do not mirror happy paths: if integration covers the nominal flow, no unit test for the same.
- Coverage is measured on the whole suite: no dedicated test for a line another test already runs, nor to check a third-party library's behaviour.
- Error paths go where the error comes from:
- an error our code decides (user not found → raise) belongs in a unit test, with
AsyncMockrepositories, even when an integration test also runs through it; - an error produced by the database or an external API (constraint violation, WireMock 4xx) belongs in an integration test: a mock would only assert our assumption about it. No fake (in-memory) repositories.
- an error our code decides (user not found → raise) belongs in a unit test, with
- A unit test whose coverage an integration test contains is a mirror: remove it. If it asserts something the
integration test doesn't (an argument passed, a batch size), move the assertion into the integration test, on the
observable outcome, first. Measure with
--cov-context=test(issue #92). - All test code is typed: fixtures, functions, factories, helpers.
- No inner imports in test functions: all imports at module level.
- A failing test is fixed in the test, never by bending the source; if the source is wrong, fix it on purpose.
- If an assertion must loop, put an identifier in its message:
assert item.active, f"Item {i} (id={item.id}) failed".
File mirror
One test file per source file, never consolidated:
museflow/application/use_cases/user_create.py
→ tests/unit/application/use_cases/test_user_create.py
→ tests/integration/application/use_cases/test_user_create.pyNaming
- Classes:
Test<Subject>, named after what they test (TestTrack,TestCreateUserUseCase); CLI commands follow CLI commands. - Tests:
test__<part>__<part>: parts separated by a double underscore, from the general to the specific (method or field, then condition, then outcome), words within a part by a single underscore:test__update_email__already_exists,test__make_api_call__retry__rate_limit__without_header. test__nominalfor the happy path; when a class covers several methods,test__<method>__nominal.
Fixtures
- Place a fixture as close to its use as possible: class, then module, then
conftest.py. scope="session"for expensive shared setup (DB engine, test DB, logging);scope="function"(default) for anything that touches state.autouse=Trueonly when every test in scope needs it.request.paramfor parametrized fixtures, never module-level_make_*helpers.async_session_db(rolled back at the end) by default;async_session_trans(real commits, TRUNCATE cleanup) only to test commit behaviour.- Use cases get
uow(integration: each block runs in a savepoint of the test transaction) ormock_uow(unit: its repositories are themock_*_repositoryfixtures;mock_uow.block_opentells whether a remote call ran inside a block). - Never
with mock.patch(...)inside a test: always apytest.fixture. mock.AsyncMockfor async ports,mock.Mockfor sync ones.- A fixture is named after what it does to the patched callable:
mock_<name>replaces it,spy_<name>wraps it (wraps=) so the real code still runs and the call is recorded. - Mocks and spies belong in unit tests. An integration test asserts the observable outcome (result, rows, requests
received by WireMock); when a value only shows as an argument passed, test its source where it is produced (e.g. the
repository) and the forwarding in a unit test. Exceptions, mocked in integration too:
- what an integration test can't run: terminal input (
typer.prompt), a real device (play_track), the logging setup; - the use case called by a CLI command's integration
TestXLogic: that test checks the real wiring (user and token loaded from the database), and relies on the use case's own integration tests for the rest.
- what an integration test can't run: terminal input (
- Assert logs with
caplog(e.g.with caplog.at_level(logging.ERROR), pytest.raises(SomeError):), never by patching the modulelogger.
CLI commands
Each command has 3 unit classes (TestXParserCommand, TestXCommand, TestXLogic) and 1 integration TestXLogic
(happy path only). Canonical: tests/unit/infrastructure/entrypoints/cli/commands/users/test_update.py.
Xis<Group><Command>, like the production names:TestPlaylistHistoryLogictestsplaylist_history_logic(museflow playlist history), patched by the fixturemock_playlist_history_logic.TestXParserCommand: Typer parsing only;TestXCommand: the callback (output, try/except), with*_logicmocked;TestXLogic: the logic branches, with aTARGET_PATHclass attribute pointing to the command module.- Error paths (user not found, …) belong in the unit
TestXLogic, not in the integration one.
Factories
DataclassFactory→ domain entities,TypedDictFactory→ TypedDicts,ModelFactory→ Pydantic,SQLAlchemyFactory→ DB models.- Never module-level helpers (
_make_*,_build_*): call the factory directly or use a fixture. A missing factory is added totests/unit/factories/. __set_relationships__ = Falseon SQLAlchemy factories;__use_defaults__ = TrueonDataclassFactorysubclasses (entity defaults such asfingerprint = ""are kept);__set_as_default_factory_for_type__ = TrueonTypedDictFactorysubclasses, chained withUse(ChildFactory.build);__allow_none_optionals__ = Falseon Update input factories.- Don't override what polyfactory generates (UUIDs, fields with defaults); set only realistic values (names, emails) or values the test depends on.
- Never bound a field to fit its column:
SQLAlchemyFactoryalready reads the column limits (Stringlength,Numericprecision). ADataclassFactoryknows no column: bound only a field whose generated value breaks a save (an unboundedDecimal), with a comment saying why.
Markers
@pytest.mark.slow: skipped by default, run with--slow.@pytest.mark.spotify_live: requires--spotify-refresh-token, hits real Spotify.@pytest.mark.wiremock: required on any integration class using WireMock (prevents xdist flakiness).
Dead End Protocol
After 3 failed fix attempts on the same problem, stop and report structured options A/B/C. Full template in the
test agent (.claude/agents/test.md).
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

