Part II — Intermediate Development · Chapter 10 · Lesson 2 of 6 · 9 min read
Lesson 2: Filtering, Watch Mode, and Coverage Baselines
Lesson level: Intermediate
Purpose: Run the right tests at the right time, keep a fast edit–test loop, and use coverage measurements as a regression signal without mistaking them for proof of correctness.
Learning objectives
By the end of this lesson, you should be able to:
- select a subset of tests by file, name pattern, or tag;
- explain when a filtered run is appropriate and when the full suite is required;
- use watch mode to shorten the edit–test loop;
- interpret line and branch coverage and name what they cannot prove;
- record a coverage baseline and use it as a regression gate; and
- avoid coverage gaming and misleading thresholds.
Release note: commands, option names, and output formats in this lesson are illustrative. Verify each against the installed v0.63 release with the built-in help before relying on it.
2.1 Why run a subset
A mature suite grows until running everything after every edit becomes slow. Slow feedback changes behavior: developers batch changes, skip runs, and discover failures late. Filtering restores a fast, focused loop by running only the tests relevant to the change in hand.
The full suite remains the authority. A filtered run answers “did I break the thing I am touching?” while the full run answers “is the project releasable?”. Both are needed, and neither replaces the other.
2.2 The running example
This lesson continues with the Chapter 9 order-discount project. Its rules module produces the reason codes GOLD_BULK, GOLD, BULK, NONE, and VALIDATION_ERROR, with a bulk threshold of 10 units. The tests from Lesson 1 cover cases A through K. All data is synthetic.
orders/
src/rules.pea
src/validate.pea
src/config.pea
tests/rules_test.pea
tests/validate_test.pea
tests/config_test.pea
2.3 Filtering by path
The coarsest filter is a path. Pointing the test command at one file or directory runs only the tests defined there.
pea test tests/rules_test.pea
pea test tests/
Path filters are predictable and cheap. They are the right default when a change is confined to one module, because the dependency direction established in Chapter 9 means lower modules cannot be affected by edits to higher ones.
2.4 Filtering by name pattern
A name filter selects tests whose declared names match a pattern. Because test names are stable strings, descriptive names directly improve filtering.
pea test tests/ --filter "gold"
pea test tests/ --filter "bulk threshold"
Name a test for the behavior it verifies, such as “gold customer at bulk threshold returns GOLD_BULK”. Names like “test1” cannot be filtered meaningfully and make failures hard to read.
2.5 Filtering by tag
When the toolchain supports tags or groups, they let a team label tests by purpose rather than location. Useful tags include fast, slow, boundary, regression, and needs-capability. A tag filter can run all boundary tests across several files.
pea test tests/ --tag boundary
Keep the tag vocabulary small and documented. An undocumented tag set drifts, and a test that is never selected by any run is silently dead.
2.6 Check what actually ran
A filter that matches nothing is a dangerous kind of success. Always confirm the count of selected tests, and treat zero selected tests as a failure in automation.
selected: 4 passed: 4 failed: 0 skipped: 0
A typo in a pattern should never produce a green result. Configure automation to fail when no tests match, and review the selected count whenever a filter is changed.
2.7 Watch mode
Watch mode keeps the test runner alive, observes source and test files, and re-runs affected tests whenever a file is saved. Combined with a filter, it produces a tight loop with feedback in seconds.
pea test tests/rules_test.pea --watch
A productive watch session follows a rhythm: write a failing test, save, see it fail for the expected reason, write the smallest change that passes, save, and see it pass. The visible failure matters, because a test that has never failed has not been shown to detect anything.
2.8 Limits of watch mode
- It runs only what you selected, so it can hide breakage elsewhere.
- It reflects the files on disk, including unsaved experiments left in other files.
- Tests that depend on time, order, or shared state can flicker between runs.
- Output from a long session can scroll past a failure; read the final summary line.
Treat watch mode as a development aid only. Never use its result as release evidence.
2.9 What coverage measures
Coverage records which parts of the code executed while tests ran. Two measures matter most:
- Line coverage: the fraction of executable lines that ran at least once.
- Branch coverage: the fraction of decision outcomes taken, so that both the true and false sides of each if are counted.
Branch coverage is the more informative of the two for rule engines. A single test can execute every line of an if/else chain’s condition yet never take the else path.
pea test tests/ --coverage
rules.pea lines 94% branches 81%
validate.pea lines 100% branches 100%
2.10 What coverage cannot prove
Coverage shows that code ran, not that its result was checked. A test that calls a function and asserts nothing still raises coverage. High coverage with weak assertions is false comfort.
- Executed does not mean verified.
- Covered branches do not imply covered combinations of inputs.
- Boundary values can be missed even at 100% branch coverage.
- Missing behavior, such as an unimplemented rule, is invisible to coverage.
Use coverage to find untested code, not to certify tested code. An uncovered branch is a precise question: “what behavior lives here, and why is it untested?”
2.11 Reading an uncovered branch
Suppose the report shows that the VALIDATION_ERROR path for a negative quantity never ran. That points to a missing test, not merely a missing percentage. Write the test from the rule’s contract, then confirm that coverage rises as a consequence.
test "negative quantity is rejected with VALIDATION_ERROR" {
let result = rules.decide({"quantity": -1, "tier": "gold"})
assert result["reason"] == "VALIDATION_ERROR"
}
2.12 Setting a coverage baseline
A baseline is a recorded coverage result for a known-good revision. It turns coverage from a vanity number into a regression signal: a future change that lowers coverage below the baseline is flagged for review.
- Run the full suite with coverage on a clean, reviewed revision.
- Record per-file line and branch figures, not only the project total.
- Store the baseline in version control with the revision it describes.
- Compare later runs against it and fail on a drop beyond an agreed tolerance.
- Update the baseline deliberately, in a reviewed change, when coverage legitimately rises.
Per-file figures matter because a project total can stay flat while one critical module quietly loses coverage and a trivial module gains it.
2.13 Choosing thresholds
| Approach | Strength | Risk |
|---|---|---|
| Fixed project minimum (for example, 80%) | Simple to explain | Encourages padding to reach the number |
| No-decrease against baseline | Protects existing quality | Can lock in a poor starting point |
| Stricter bar for critical modules | Focuses effort on risky rules | Needs an owner to maintain the list |
| Diff coverage on changed lines | Rewards tests with the change | Ignores untouched weak areas |
A sound policy usually combines a no-decrease gate with a higher bar for rules and validation modules, where defects are most costly.
2.14 Avoid gaming coverage
- Do not add assertion-free tests to raise a number.
- Do not exclude difficult code from measurement without a written reason.
- Do not lower the baseline silently to make a failing run pass.
- Do not reward individuals for percentages; reward defects caught and risks removed.
When a team is measured on the metric, the metric stops measuring. Review the content of tests, not only the figure.
2.15 Combining local and CI runs
| Context | Typical command scope | Purpose |
|---|---|---|
| Editing | One file, watch mode | Seconds-level feedback |
| Before commit | Affected directory plus boundary tag | Catch local regressions |
| Pull request | Full suite with coverage | Authoritative gate against baseline |
| Release candidate | Full suite, coverage, clean environment | Evidence for the release record |
The CI run should never use filters that a developer can quietly change. Keep its command in version-controlled configuration.
2.16 Common mistakes
| Mistake | Consequence | Correction |
|---|---|---|
| A filter selects zero tests and reports success | False confidence | Fail on zero selected tests |
| Relying on a project-wide coverage total | Critical module regresses unnoticed | Track per-file baselines |
| Chasing 100% line coverage | Assertion-free tests | Review assertions and branch outcomes |
| Using watch results as release evidence | Unrun tests hide failures | Run the full suite in CI |
| Updating the baseline to silence a failure | Regression becomes the new normal | Update only in a reviewed change |
| Vague test names | Filtering and failure reports are useless | Name tests for the behavior verified |
2.17 Diagnostic workflow
- Confirm how many tests were selected and compare with expectation.
- If a test did not run, check the path, pattern, and tag spelling.
- If watch mode seems stale, confirm the file was saved and the watcher is monitoring that directory.
- If coverage dropped, list the files and branches that changed against the baseline.
- Decide whether the drop is removed code, a deleted test, or new untested behavior.
- Add or restore the test, rerun, and confirm the figure recovers for the right reason.
2.18 Guided practice
- Write three filter commands that would run only the bulk-threshold tests, in three different ways.
- Explain why a filter that selects zero tests must fail in automation.
- Give an example of a function with full line coverage but an untested branch.
- Choose per-file thresholds for rules, validation, and configuration modules and justify them.
- Describe how a baseline could hide a poor starting point.
2.19 Hands-on lab: Measure and improve the order-discount suite
Goal: Use filtering, watch mode, and coverage to find and close a gap in the Chapter 9 project.
- Run the full suite and record the selected, passed, and failed counts.
- Run only the rules tests by path, then by name pattern, then by tag. Confirm each selected count.
- Start watch mode on the rules tests. Break one expectation deliberately, observe the failure, and restore it.
- Run the full suite with coverage and record per-file line and branch figures.
- Identify one uncovered branch and write the behavior it implies.
- Add a test with a meaningful assertion and confirm the branch is now covered.
- Record the new figures as the baseline, with the revision identifier.
- Lower one coverage figure by deleting a test on a scratch copy and confirm the gate would flag it.
Deliverable: A short report with selected counts for each filter, before-and-after coverage per file, the new test, the baseline file, and a note explaining what the coverage figures do not prove.
2.20 Review questions
- Why is the full suite still required when filtered runs pass?
- Name three ways to filter tests.
- Why should zero selected tests be treated as a failure?
- What advantage does branch coverage have over line coverage?
- Why can high coverage coexist with weak testing?
- What is a coverage baseline, and where should it be stored?
- Why track coverage per file?
- What distinguishes gaming a metric from improving it?
2.21 Applied challenge
Design a testing policy for a small team maintaining a rules library. Specify the filters developers use locally, the CI command and its coverage gate, the thresholds for rules and validation modules, the procedure for updating the baseline, and two safeguards against coverage gaming.
Lesson summary
Filtering by path, name, or tag gives a fast, focused feedback loop, and watch mode shortens it further, but neither replaces the full suite. Coverage reveals untested code and, through a recorded per-file baseline, protects against regression. It does not prove correctness, so assertions, boundaries, and reviewed baselines remain essential.
Transition to Lesson 3
The next lesson covers fixtures, deterministic tests, and boundary cases: how to build reliable test data, remove sources of flakiness, and choose inputs that expose off-by-one and precedence defects.
Lesson status: Ready for review.