PeaBox Lab › PeaLang › Learn › Chapter 10 › Lesson 2

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:

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

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:

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.

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.

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

ApproachStrengthRisk
Fixed project minimum (for example, 80%)Simple to explainEncourages padding to reach the number
No-decrease against baselineProtects existing qualityCan lock in a poor starting point
Stricter bar for critical modulesFocuses effort on risky rulesNeeds an owner to maintain the list
Diff coverage on changed linesRewards tests with the changeIgnores 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

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

ContextTypical command scopePurpose
EditingOne file, watch modeSeconds-level feedback
Before commitAffected directory plus boundary tagCatch local regressions
Pull requestFull suite with coverageAuthoritative gate against baseline
Release candidateFull suite, coverage, clean environmentEvidence 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

MistakeConsequenceCorrection
A filter selects zero tests and reports successFalse confidenceFail on zero selected tests
Relying on a project-wide coverage totalCritical module regresses unnoticedTrack per-file baselines
Chasing 100% line coverageAssertion-free testsReview assertions and branch outcomes
Using watch results as release evidenceUnrun tests hide failuresRun the full suite in CI
Updating the baseline to silence a failureRegression becomes the new normalUpdate only in a reviewed change
Vague test namesFiltering and failure reports are uselessName tests for the behavior verified

2.17 Diagnostic workflow

2.18 Guided practice

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.

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

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.

← Previous Next →