Engineering Craft · 18 min · free lesson

Choosing a Test

Different bugs need different tests, and naming them is the answer.

Core idea

Choose tests for named failure modes and independent evidence; test labels alone prove little.

Lesson map: a proptest roundtrip of encode and decode, above the rule to choose tests for named failure modes.
Property-test sketch: requires proptest plus the application’s encode/decode functions; it is not a standalone std program.

Understand it

Examples, generated properties, fuzz targets and integration tests provide different evidence. None guarantees which bug is found first or that untested invariants hold. State what each test checks, then preserve minimized failures as regressions.

Different tests answer different questions. A unit test can pin one known rule with a small input. A property test generates many inputs and checks an invariant, such as parse-then-format preserving a value. A fuzz target feeds malformed or unexpected inputs to a parser and watches for crashes or violated assertions. An integration test checks boundaries among modules or a real storage adapter. None of these is a substitute for knowing what behavior matters to users.

Start with a parser. Unit tests cover empty input, a normal record, and a known malformed delimiter. A property test can assert that valid generated records round-trip through serialization and parsing. Fuzzing can explore arbitrary bytes that a hand-written list never anticipated. An integration test can verify that a parsed record is stored and later retrieved through the public API. Keep a regression test for every serious bug found by fuzzing so it stays covered.

In an interview, prioritize by risk and cost. Pure functions are cheap to test often; network-dependent end-to-end tests may be slower and more fragile. Coverage percentage reports which lines ran, not whether the assertions were meaningful. Describe one failure mode each layer is meant to catch, how tests run in development, and what remains for manual or production observation. A good strategy is a set of evidence sources, not a pile of test labels.

When a property test fails, save the minimized counterexample as a named regression test. When a fuzz target finds malformed input that panics, classify whether the panic violates the API contract. Add a resource limit for deeply nested or huge inputs as well; not every parser failure is a type or memory safety bug.

Interview follow-up: Why can encode/decode roundtrip still pass if both share the same bug? Add an independent oracle or known external vectors.

Read the code

#[test]
fn roundtrip() {
    proptest!(|(value: Vec<u8>)| {
        let encoded = encode(&value);
        prop_assert_eq!(decode(&encoded)?, value);
    });
}

Property-test sketch: requires proptest plus the application’s encode/decode functions; it is not a standalone std program.

Practice starter

// A parser accepts untrusted binary input.
// Known examples and deterministic regression tests already exist.

Practise this code in Playground ↗

Try it

Choose the right kind of test for a parser, a cache, and an HTTP route.

What success looks like: Each choice names the class of bug it is meant to catch.

Check your understanding

Which technique directly explores many malformed byte sequences for this parser?

Reveal the answer
  1. A single integration test proves correctness for every malformed byte sequence.
  2. A focused fuzz target with assertions, resource bounds and retained counterexamples.
  3. Line coverage alone establishes that every parser invariant is checked.

No technique is guaranteed to find a particular crash first. Combine known regression tests, properties, fuzzing and boundary integration tests.

Go deeper

Common misconception: Line coverage alone establishes that every parser invariant is checked.

Coverage reports which lines ran, not what was asserted. A line can run with no check on its result, and an invariant may never be stated at all.

  1. Identify the contract: Different bugs need different tests, and naming them is the answer.
  2. Trace the concrete example: The property is checked across many generated inputs, not just three hand-picked ones. Passing is evidence, not proof.
  3. Test the changed constraint: Why can encode/decode roundtrip still pass if both share the same bug? Add an independent oracle or known external vectors.

Transfer challenge: Why can encode/decode roundtrip still pass if both share the same bug? Add an independent oracle or known external vectors.

Connections

  • Hashing and Collisions: Why Rust seeds its hasher
  • API Design and Semver: Making misuse impossible to express
Learn this lesson with AI

Paste this into Claude, Codex, Cursor or another agent connected to Crabrix Academy. It contains only this lesson’s public details.

I'm learning Rust with Crabrix Academy. Teach me this lesson with the Crabrix Academy tools.
Lesson: Choosing a Test (courseId "interview", lessonId "q-testing-strategy")
Link: https://crabrix.com/learn/interview/q-testing-strategy/
Goal: Choose tests for named failure modes and independent evidence; test labels alone prove little.
Practice: open the starter in the Crabrix Playground (https://crabrix.com/playground/?lesson=interview/q-testing-strategy) and I will write the code there.
Explain it step by step for my level, let me write the code myself, give hints before any solution, and do not mark anything as passed: only Crabrix checks do that.

In Claude Code, Codex or Cursor, paste it into a chat with Crabrix Academy connected. On a phone, the Claude and ChatGPT apps open the same links.