liveBook · Manning

Test harness

As explained in Code Like a Pro in Rust

A test harness is infrastructure that controls test execution and collects or reports test results.

A test harness is the infrastructure that runs tests and reports their results. Rust’s built-in libtest harness covers standard execution, while specialized helpers, fixtures, and parameterized testing require custom code or libraries. section 6.3 section 6.5

Use the harness ecosystem according to the testing requirement:

  • Add parameterized testing with parameterized or test-case, fixtures with rstest, and alternative assertions with assert2. section 6.3
  • Use proptest for generated inputs, failure reporting, and minimized failing cases; retain hand-picked values for specification compliance. section 6.3
  • Treat shared test state as concurrent state: use atomic types, Arc, lazy_static, or Mutex rather than unsynchronized mutable statics. section 6.5

Finding an overflow with proptest

A generic add function can pass a property test for ordinary inputs yet fail on generated i64 values. proptest reports a minimal failing pair; changing the implementation and assertion to wrapping_add makes cargo test pass.

How Code Like a Pro in Rust covers it

How deep it goes
Explains harness replacement with harness = false and addresses parallel shared state, without detailing libtest internals.
Examples
Demonstrates proptest-based addition testing and synchronized global counters shared across parallel tests.
What it assumes
Assumes familiarity with Rust unit tests and introduces the concurrency hazards of mutable global test state.
What you can do after
Operate/configure Rust tests with cargo test, proptest, synchronized state, and harness = false for custom test entry points.
How it connects
Connects test harnesses to parameterized testing, fixtures, property testing, libtest, global state, atomics, and mutexes.

Also explained in

Compare how 2 books explain test harness →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Code Like a Pro in Rust. The section links point to the parts of the book it draws on.