Testing

Philosophy

  • NEVER dismiss test failures as transient. Investigate root cause; classify as an edge case only after thorough analysis.

  • All checks must pass before push — enforced by the pre-push hook.

  • Test results are saved to test-results/ at the repo root — always check it for failure context.

Test Output

All test results go in test-results/ at the repo root. Non-negotiable — do not mount, write, or look for results anywhere else.

test-results/
├── unit/           # cargo-nextest unit test JUnit XML
├── integration/    # cargo-nextest integration test JUnit XML
├── e2e/            # Playwright JUnit XML + traces/screenshots
└── ci/             # CI-only artifacts (coverage, SAST, container scan reports)
  • test-results/ is gitignored — never committed.

  • All test types produce JUnit XML in their subdirectory; CI consumes these exact paths.

  • When reviewing failures, read the full XML — do not tail/head/partial-read.

Test Runner

  • cargo-nextest is the standard runner for all Rust tests (unit + integration).

  • Config .config/nextest.toml; CI profile writes JUnit XML; all profiles fail-fast = false.

E2E Framework

  • Playwright is mandatory for all projects with a web UI — no Cypress, no Selenium.

  • E2E tests run inside Docker, never on the host.

  • Playwright config includes a JUnit reporter to test-results/.

  • Projects without a web UI delete the E2E sections entirely (no empty placeholders).

Pre-Push Hook

Location .githooks/pre-push; activate git config core.hooksPath .githooks && chmod +x .githooks/. The *functional-correctness gate — it runs the full local battery. canopy’s CI also runs the unit lane, the static audits, quality-budgets and plan-lint, but it does not replace the battery (the dated override in AGENTS.md). The hook runs eight steps, in this order:

Step 1 — commit-signature verification: every pushed commit not already on origin must be signed.

Step 2 — cargo xtask validate --skip-docker (all must pass): public-visibility check; commit-signing config; mandatory project docs exist; SPDX headers on .rs; cargo fmt --check --all; clippy with -D warnings; the nextest unit and integration arms; and the rest of validate’s stages.

Step 3 — cargo deny check: supply-chain check, run only if cargo-deny is installed.

Step 4 — cargo xtask e2e: Docker build + containers + Playwright (when the project has a web UI / docker-compose), then teardown.

Step 5 — cargo doc --workspace --no-deps: rustdoc compiles, with broken intra-doc links denied (RUSTDOCFLAGS="-D rustdoc::broken_intra_doc_links") so a broken link hard-blocks rather than only warning.

Step 6 — cargo xtask perf --profile smoke: k6 smoke when configured; soft-skips (exit 4) when there is no load suite (opt out with SKIP_PERF=1).

Step 7 — git lfs pre-push: run explicitly because a custom core.hooksPath overrides git’s built-in LFS hook; a no-op without git-lfs.

Step 8 — cargo xtask identity matrix: canopy’s identity conformance matrix, always the last step.

The ROOT What git push runs section is the canonical detail for each step.

What CI runs (it does NOT duplicate the battery)

CI runs security, supply-chain and release jobs: cargo-audit/cargo-deny/ cargo-machete (audit-tools image); GitLab SAST + secret-detection; plan-lint, secrets-yaml-lint, fn-shape-report; release (tags only) sbom
release-xtask. canopy’s CI also runs its unit lane, static audits and quality-budgets (ROOT testing.adoc); the pre-push battery stays the functional gate.

Nextest Profiles

Profile Use Timeout JUnit Output

default

Local dev (cargo nextest run)

60s

None

integration

Pre-push hook

120s

test-results/integration/results.xml

ci

CI unit tests

60s

test-results/unit/results.xml

ci-integration

CI full devstack tests

120s (8 threads)

test-results/integration/results.xml

All profiles fail-fast = false.

Deterministic Seed-Based Test Data

  • Generators take a --seed for deterministic output (same seed = same entities, UUIDs, relationships).

  • No seed → generate a random one and print it to stderr so failures reproduce.

  • E2E tests consume typed manifests generated from the seed, not hardcoded values.

  • Document the minimum seed size needed for pagination/boundary tests.

cargo run -p project-seed -- --seed 42 --families 12   # deterministic
cargo run -p project-seed                              # random; prints seed to stderr

Integration Test Guards

An infrastructure-dependent test (databases, Docker, external services) that the battery must run is never #[ignore]: the battery never passes --run-ignored, so an ignored test would silently never run. It guards on canopy_test_lib::infrastructure_available() instead:

#[tokio::test]
async fn creates_case_end_to_end() {
    if !canopy_test_lib::infrastructure_available().await {
        return; // only outside a battery lane; under CANOPY_TEST_INFRA=required it panicked
    }
    /* infra present */
}

Every battery nextest lane sets CANOPY_TEST_INFRA=required, under which an unavailable stack panics, so the in-body return can never report a battery test that ran nothing as passed. Only a bare local cargo nextest run takes the return. This is a dated canopy override of pi’s rule against an in-body skip (AGENTS.md, #2997), and the posture, the helpers for a miss after the probe (skip_or_panic, ok_or_skip) and the lint that holds it are in Testing › CANOPY_TEST_INFRA.

#[ignore = "<why>"] is kept for a test the battery is not meant to run: an opt-in test that drives Docker or restarts a container (chaos, broker reconnect, the xtask devstack installers; run by hand with --run-ignored only), a perf measurement, or a child process a parent test runs.

Test Harness Pattern

  • A TestHarness struct manages authenticated HTTP clients, cleanup stacks, and lifecycle.

  • Builder patterns for entities (PersonBuilder, CaseBuilder) — don’t hand-build JSON.

  • Typed service clients with bearer-token injection.

  • Registered cleanup must actually run. Wire the cleanup stack into Drop (drain LIFO on a fresh thread with a current-thread runtime so it fires during unwind; bound each future) or into an explicit awaited teardown — a stack that is pushed to and never drained is a leak. Two hazards: a drop-time future must not reuse the harness’s pooled client (its connections belong to the parked test runtime and the request deadlocks); build a one-shot client inside the future. And this page describes the required contract — verify the harness you inherit actually implements it before relying on it.

Performance Testing

  • Criterion (micro) — add benchmarks when performance is a stated requirement or a hot path is identified; >10% regression on a hot path warrants investigation. On-demand (cargo bench), not in pre-push/CI by default.

  • k6 (macro) — cargo xtask perf; profiles smoke (1 VU ~5s, in pre-push), load, stress, soak; threshold assertions on p95 latency + error rate; results to test-results/k6/; runs in Docker (grafana/k6).

Test Categories (Taxonomy)

Name a test for the property it asserts, not the endpoint/function it calls.

Category Purpose Location

Functional

Happy-path correctness

tests/api/ or #[cfg(test)] mod tests

Invariant

Cross-cutting properties (uniqueness, referential integrity, drift)

tests/invariants/

Concurrency

Races, deadlocks, lock ordering, parallel safety

tests/concurrency/

Fault-injection

Behavior under failures (network/broker/partial-write)

tests/fault_injection/

Recovery

Behavior after failure (retry, idempotency, rollback, replay)

tests/recovery/

Contract

API/wire-format pinning (response shapes, error schemas, CSP)

tests/contract/

Property-based

Universally-quantified properties via proptest/quickcheck

tests/property/

Mutation

Uncovered-path detection via cargo mutants

driven by cargo xtask mutants

Process rule: concurrency / fault-injection / platform-invariant fixes MUST ship with a test in the corresponding category — a race-condition bug gets a concurrency test, not just a functional one.

Constraint Tests Are Hand-Curated

API-boundary rejection tests (UNIQUE/FK violations, enum validation, duplicate-create) are hand-written, not auto-derived from SQL schemas or OpenAPI. Auto-generation produces shallow tests that miss the real boundary cases — write the test for the rejection behavior you want.

Invariant Tests with Drift Contract

For projects with rulesets, migrations, or per-service catalogs, maintain an invariant test that sweeps the catalog at test time: every file parses, names are unique, constants match the code. Examples: all migrations apply against a fresh DB; all rulesets/*.json names unique; rulesets reference only code-defined enums.

Evil Input Corpus

For services accepting user-controlled input, maintain a hostile-input corpus per category, run via a parametric macro (adding an endpoint becomes a one-line subscription):

Category Example payloads

jwt

Tampered signature, expired, unsigned alg: none, oversized header

upload

Zip-bomb, polyglot, MIME mismatch, path-traversal filename

string

NULL bytes, oversized (10MB), homoglyph, RTL override, control chars

uuid

Wrong version, zero UUID, non-canonical, oversized

json

Deeply nested, duplicate keys, integer overflow, NaN/Infinity

path

../, ..\\, URL-encoded, absolute, symlink loop, reserved names

html

XSS variants, mXSS, SVG-embedded JS, data: URLs, on-attribute handlers

unicode

Bidi override, zero-width joiners, normalization mismatches

enum

Unknown variant, case-mismatch, oversized, wrong type

date

Pre-epoch, year 9999, leap second, timezone gap, non-ISO

multipart

Boundary in payload, missing boundary, oversized/malformed part

signature

Truncated, wrong algorithm, key confusion (HS256 vs RS256), replay

Each evil-input test asserts: rejected with 4xx, no PII in the error, no internal state mutation, no log spam.

Property-Based Testing

Property-based testing (proptest / quickcheck) is required for parsers, deserializers, serializers, and numerical/math logic, and optional elsewhere. State the invariant; let the tool find counter-examples. Example-based tests with hand-picked values miss edge cases (empty/max-size inputs, surrogate pairs, overflows). Prefer proptest (better shrinking); ProptestConfig { cases: 1000, .. } for slow targets.

Coverage Floor Gate

cargo xtask coverage [--threshold N] [--baseline] wraps cargo-llvm-cov (LCOV
JSON to test-results/coverage/), compares against .coverage-baseline.json, and fails on a >0.5% drop. --baseline refreshes; --threshold N enforces a minimum line coverage (CI-gateable). Tracking baselines prevents silent test decay.

Mutation Testing

cargo xtask mutants [--smoke] wraps cargo-mutants to find uncovered paths. --smoke runs a 1/20 shard (~minutes); full runs in scheduled CI. Accepted mutants are documented in mutants-baseline.toml with per-entry justification.

Critical Rules

  1. Never dismiss test failures as transient — investigate root cause.

  2. All checks must pass before push — fmt, clippy, tests (hook-enforced).

  3. Test results in test-results/ — check it before asking questions.

  4. Docker-based tests run in Docker — never run Playwright/E2E on the host.

  5. Never weaken a test to make code pass — fix the code, not the test. No delete/skip/#[ignore]; no loosened assertions; no changed expected values. The only legitimate test edit is a genuinely-incorrect test, explained in the commit.

  6. All test types produce JUnit XML to test-results/.

Project-specific test types, commands, the CI pipeline, and E2E setup live in the project’s project testing page.

Edit this page · default