Testing¶
Use the environment prepared in the contributing guide.
The default gate combines unit tests and deterministic integrations; each test
creates its own local inputs.
Run the default gate¶
uv run --locked --no-sync pytest -m "not slow and not real_data and not perf" \
--cov --cov-report=xml --cov-report=term-missing
For a localized change, start with the affected module, for example:
uv run --locked --no-sync pytest tests/application/b3_docs
pytest.ini defines discovery and markers; pyproject.toml defines coverage.
For an HTML report, add --cov-report=html to the coverage command.
Classify each test¶
Every test has exactly one primary tier:
| Tier | Contract |
|---|---|
unit |
Isolated behavior with pure collaborators, fakes, or stubs. |
integration |
Multiple real components and deterministic local filesystem. |
perf |
Benchmarks or resource measurements; always opt-in. |
Use slow for heavy scenarios and asyncio for asynchronous execution.
real_data requires integration, uses caller-supplied COTAHIST, and is
opt-in. These qualifiers do not replace the primary tier.
uv run --locked --no-sync pytest -m unit
uv run --locked --no-sync pytest -m "integration and not slow and not real_data"
uv run --locked --no-sync pytest tests/perf -m perf -o addopts=''
A command with -m replaces the default selection; use explicit qualifiers
to keep external and slow scenarios outside the intended run.
Write useful tests¶
- Mirror the affected boundary:
tests/application/, the source under
tests/brazil/, or shared foundations. Subdirectories organize topics. - Prove behavior, limits, transitions, and regressions through observable
outcomes; covered line counts do not replace useful assertions. - Use
tmp_pathand ZIP, CSV, or Parquet inputs created by the test. Replace
network calls withhttpx.MockTransportor local collaborators. - Parquet reading and writing also need real PyArrow proofs of schema,
types, content, and temporary-file cleanup. Pandas and Polars are not runtime
dependencies of the library. - Document deletions and consolidations in
.test-integrity-policy.json.
The structural gate below checks tiers and observations in test bodies;
it is heuristic and does not prove assertion semantics:
uv run --locked --no-sync python scripts/check_test_quality.py
Use local COTAHIST¶
Official data belongs in a caller-owned directory such as
cotahist_b3_local/, which Git ignores. Tests do not download these files or
read .env implicitly. Supply paths and a year explicitly:
COTAHIST_PATH=./cotahist_b3_local COTAHIST_TEST_YEAR=2023 \
uv run --locked --no-sync pytest -m "real_data and not slow"
COTAHIST_PATH=./cotahist_b3_local COTAHIST_TEST_YEAR=2023 \
uv run --locked --no-sync pytest -m "real_data and slow"
The first selection does not write Parquet, but the current fixture performs
full preflight of every catalog year, including hashes and every record.
Even with not slow, this setup can read large files in full. Extraction
proofs, including sampling, annual validation, and fast/slow parity, are
slow. Full auditing uses an independent decoder.
The fixture inspects the complete catalog before selecting a year. Without
COTAHIST_PATH, the selected suite is skipped. When it is set, invalid, empty,
unreadable, or missing-year input fails. A sole year can be inferred; with
multiple years, COTAHIST_TEST_YEAR is required and must contain four digits.
ZIP takes precedence over TXT. Internal members accept modern
COTAHIST_A{YYYY}.TXT and historical COTAHIST.A{YYYY} and
COTAHIST_A{YYYY} names, always matching the year.
cotahist_b3/ contains only the examples’ versioned synthetic samples,
generated by scripts/generate_example_cotahist.py. They are tested under
tests/examples without real_data. An official annual dataset may become
mandatory in CI only after a versioned fixture or a published artifact with
a compatible license exists.
Coverage and CI¶
The product requires 85% aggregate coverage of src/, including branches.
The real-validation executor has a separate 85% floor, also including branches.
To run its gate without copying the module list and options:
uv run --locked --no-sync pre-commit run real-validation-coverage \
--hook-stage pre-push --all-files
The quality matrix runs on Python 3.12, 3.13, and 3.14. Canonical settings are
pyproject.toml, pytest.ini, .pre-commit-config.yaml, and
.github/workflows/pipeline.yml. Release acceptance
adds native Windows/macOS evidence, publication fault scenarios, and packages.
Audit external data¶
The complete CVM/COTAHIST campaign is opt-in and may take hours. Use the
external-data validation guide to run it, interpret
reports, and resume a campaign. It is outside the deterministic local gate.