搜索与研究
specx-tests
Add or refine tests for specx Python services. Use when creating unit tests for use cases/services, integration tests for FastAPI controllers or infrastructure adapters, e2e smoke tests, architecture import guardrails, DI override tests, pytest fixtures, or coverage and boundary checks.
maksimzayats · 已有 AI 审核记录
安装与使用
运行命令前先阅读来源与依赖说明。复制命令不会自动安装或执行。
npx skills add maksimzayats/specx --skill specx-tests技能说明
name: specx-tests description: Add or refine tests for specx Python services. Use when creating unit tests for use cases/services, integration tests for FastAPI controllers or infrastructure adapters, e2e smoke tests, architecture import guardrails, DI override tests, pytest fixtures, or coverage and boundary checks.
specx Tests
Use this skill when behavior, wiring, or architecture boundaries need tests.
Read references/testing.md before creating test files.
Test Layers
tests/_support/: generic clients, DB helpers, and shared integration helpers only. This is not a test suite and does not hold project-specific doubles.tests/unit/: core services, use cases, and capabilities resolved from a fresh application container returned by the project'sget_container().tests/integration/: real internal graph tests. Core use-case integration tests call resolved use cases against the transactional DB; delivery integration tests exercise HTTP mapping; migrations prove Alembic behavior.tests/e2e/: optional whole-app smoke flows.tests/guardrails/: optional programmaticspecx.testing.architecture.assert_specx_architecturewrappers for genuinely project-specific extra rules. Standard packaged rules run throughuv run specx check.
Rules
- Test behavior and boundaries, not implementation ceremony.
- Required generated coverage is currently scoped to core services, use cases, and capabilities.
- Mirror source module paths directly with flat test files, for example
tests/unit/core/tasks/services/test_title_service.py. - Do not create per-target test folders,
harness.py, target factories, or target harnesses. tests/unit/conftest.pyowns the fresh real-appContainerfixture for unit tests and any project-wide test overrides.tests/integration/conftest.pyowns the transactional DB-backedcontainerfixture for integration tests.- Test functions receive
container, register any scenario-specific overrides before resolution, then callcontainer.resolve(Target). - If a complete replacement is needed by every test in one module, a
module-local
containerfixture may register it before returning the container. - Keep one-off class-based test doubles in the
test_*.pymodule that uses them. When the same double is reused by multiple unit modules, put it in a mirroredtests/unit/core/<scope>/{capabilities,gateways,repositories}/fake_<source_module>.pyfile. - Do not create
tests/_support/fakes,tests/**/_fakes.py, generic_scenarios.py, fake modules outside those mirrored unit port/capability packages, or double classes inconftest.py. - Use
MagicMockorAsyncMockinline in the test function when only one behavior needs to be changed for that scenario. Prefer autospeccing when call signatures matter andspec_setwhen unexpected attributes must fail. - Unit tests replace external IO, time, randomness, network, Redis, database, and framework resources with local doubles or inline mocks.
- Integration tests use the real internal app graph. Do not mock internal use cases, services, or capabilities; stub only external systems when needed.
- Add core use-case integration tests under
tests/integration/core/...for use cases that inject a UoW manager; delivery tests should own HTTP mapping, not be the only persistence proof. - Persistence integration tests use the production database family when dialect behavior matters. A rollback harness may not replace isolated commit-visible tests for locking, concurrency, isolation, or after-commit behavior.
- Core health tests cover required-dependency readiness and any reusable probe
services and use cases.
Delivery probe tests cover
/healthzand/readyzas operational endpoints, not versioned business API routes./healthzmust prove a lightweight process response only;/readyzmust prove required infrastructure readiness, including a real bounded DB check for SQLAlchemy services. - Probe route tests assert
Cache-Control: no-store, readiness failure returns503, probe routes are excluded from OpenAPI, and legacy/api/v1/healthis absent when replacing old generated health endpoints. - Unit-test logging configurators by overriding logging settings,
monkeypatching
logging.config.dictConfig, and asserting the generated stdlib config. Usecaplogonly when a log record is meaningful behavior. - Unit-test FastAPI lifecycle managers by overriding closeable infrastructure resources and asserting shutdown order. Route integration helpers must run ASGI lifespan explicitly.
- Use
httpx2, not legacyhttpx, for generated HTTP client and ASGI transport tests. EnterLifespanManager, then pass the yielded manager'smanager.apptoASGITransportso request scopes receive lifespan state. - FastAPI route tests compare response status codes with
fastapi.statusconstants, not raw integer literals. - Use
container.resolve(...)for normal synchronous graph construction, even when the resolved use case has an asyncexecute(...); useawait container.aresolve(...)only when DI construction itself has async providers. - Mock fixtures should register one external collaborator for the behavior under test. Do not bundle unrelated mocks in a dict or class-keyed fixture.
- Use native pytest fixtures for test dependencies. Do not enable
diwire.integrations.pytest_plugin, and do not useInjected[...]parameters in tests. - AnyIO runs tests on every installed supported backend by default. If the app
graph is asyncio-specific, override the top-level
anyio_backendfixture to return"asyncio"; leave it unpinned only when the suite intentionally supports every installed backend. - Do not add filler smoke tests that only assert
container.resolve(...)returns an instance. - Do not hand-build application graphs in test bodies. Resolve project classes from the container; local test doub