The Simplest Test That Can Fail

A test drives a design, watches what comes out, and decides pass or fail. Turn a test you have to watch into one that checks itself. Then learn why a test that cannot fail is worse than no test at all.

You Will Be Able To

  • Name the three jobs of every test: drive, observe, decide.
  • Turn a test that prints values into one that checks them.
  • Spot a test that cannot fail, and make sure a test can fail before trusting it.
Lesson
B2.1
Time
18 min
Runs on
Your browser
Check
5 questions

Eight lines scroll past: 2 + 2 = 4, 7 + 1 = 8, 3 + 5 = 0, 9 + 6 = 15. Did you catch it? Now imagine eight thousand lines, every night, read by a tired engineer. A test that needs a person to spot the wrong line will miss it.

Stimulus in, Response Out, a ComparisonStep through the smallest test that can fail. It drives the design, watches what comes out, and compares it with an answer it worked out on its own.
  • Driver
  • DUT
  • Monitor
  • Model
  • Test
  • Result
  • Data
  • Analysis
  • Control
CallValueReturnWaiting
Stimulus In, Response Out, a ComparisonStep through the smallest test that can fail. It drives the design, watches what comes out, and compares it with an answer it worked out on its own. driver Stimulus the inputs youchoose: a = 3, b = 5 dut Design under test adder4 monitor Response what came out: sum =0 model Expected worked out apartfrom the design: 8 test Compare same or different? result Pass or fail printed by the test,not read off by you driveobservesame inputsactualexpecteddecide
  1. driver Stimulusthe inputs you choose: a = 3, b = 5
    • drive → Design under test
    • same inputs → Expected
  2. dut Design under testadder4
    • observe → Response
  3. monitor Responsewhat came out: sum = 0
    • actual → Compare
  4. model Expectedworked out apart from the design: 8
    • expected → Compare
  5. test Comparesame or different?
    • decide → Pass or fail
  6. result Pass or failprinted by the test, not read off by you
  1. Drive

    The test chooses inputs and puts them on the design's pins.

    Follow the calls, values and waits
    1. data a=3 b=5: Stimulus → Design under test
  2. Observe

    It reads what the design produced.

    Follow the calls, values and waits
    1. data sum=0: Design under test → Response
  3. Expect

    From the same inputs, it works out the right answer without asking the design.

    Follow the calls, values and waits
    1. data 3+5=8: Stimulus → Expected
  4. Decide

    It compares the two and prints the verdict. Nobody has to read a waveform to know.

Read the diagram as text
  • Stimulus: the inputs you choose: a = 3, b = 5
  • Design under test: adder4
  • Response: what came out: sum = 0
  • Expected: worked out apart from the design: 8
  • Compare: same or different?
  • Pass or fail: printed by the test, not read off by you
  • Stimulus → Design under test: drive
  • Design under test → Response: observe
  • Stimulus → Expected: same inputs
  • Response → Compare: actual
  • Expected → Compare: expected
  • Compare → Pass or fail: decide

The three jobs of every test: drive, observe, decide. The expected value must not come from the design.

Before You Start

Read The Specification and the Design Under Test first: a test compares the design with an agreed reading of its specification. This lesson uses the 4-bit adder of A Chip You Cannot Patch.

Five questions this page answers
Question
1What are the three jobs of every test?
2Why is a test you have to watch not enough?
3Where must the expected value come from?
4What makes a test unable to fail?
5How do you know a test can fail?

The Idea: A Test Must Decide

A test has three jobs. It drives the design's inputs with values it chooses. It observes the outputs. And it decides: right or wrong. The first two are easy to write. The third is where tests go bad.

The oldest kind of test leaves the deciding to you. It prints values, or you open a waveform, and you decide on each line by eye. That works for ten lines once. It fails for ten thousand lines every night, and it fails silently, because a missed wrong line looks like a pass.

A self-checking test decides for itself. For each input it works out the expected output on its own, compares, and counts mismatches. At the end it prints one verdict. Now a regression of a thousand tests can run overnight and report only what failed.

New Words

New words on this page
WordMeaning
TestCode that drives a design, observes it, and decides pass or fail.
TestbenchEverything around the design that runs tests: the code that sets inputs, the checks, the clock. Often shortened to "bench".
StimulusThe input values a test puts on the design.
Expected valueThe output the test believes is right, worked out apart from the design.
Self-checkingAble to decide pass or fail without a person reading the output.
RegressionA large set of tests run again and again, often every night, to catch new bugs.
WaveformA picture of signals over time, one row per signal, drawn from what the simulator recorded.
Unknown value (x)A bit the simulator cannot call 0 or 1, such as a wire nothing drives or a flip-flop never reset.

A Test That Cannot Fail

A test that cannot fail is worse than no test. It reports PASS whatever the design does, and everyone believes it. There are a few common ways to write one by accident.

Ways a test loses the power to fail
What it doesWhy it cannot fail
Copies its expected value from the designIt compares the design with itself
Has a check that is never reachedThe comparison sits in a branch the test never runs
Compares only some of the outputThe wrong bits are in the part it ignores
Ends before the design answersThere is nothing to compare, so nothing mismatches
Counts errors but never reports themThe verdict line ignores the count

The cure is simple and nearly free. Before you trust a new test, break the design on purpose and watch the test fail. If it still passes, the test is the bug.

The Rules

  1. Rule 1: Every test drives, observes and decides.

    Leave out the decision and a person has to make it, line by line.

  2. Rule 2: Work out the expected value apart from the design.

    An answer taken from the design can never disagree with it.

  3. Rule 3: Compare everything that matters.

    Every output bit, every time the specification says it is valid.

  4. Rule 4: End with one verdict.

    PASS or FAIL with a count, so a regression can sort a thousand runs.

  5. Rule 5: Make a new test fail once before trusting it.

    Break the design on purpose. A test that still passes is broken.

Sources for These Rules

Spear and Tumbush (2012), section 1.3: a testbench generates stimulus, applies it, captures the response, checks it, and measures progress. Section 1.4: directed tests whose logs and waveforms are reviewed by hand.

Spear and Tumbush (2012), section 1.5: automatic stimulus needs an automatic way to predict results.

Mehta (2018), section 15.5.2: a testbench provides stimulus to the DUT and measures its response.

Breaking a design on purpose to show a check can fail is common practice. No single source states it as a rule.

The Lab: Watch, Check, Copy

The lab drives the buggy adder of the first lesson with eight pairs of inputs. For each pair it runs three tests side by side. The apply task holds all three.

Inside apply: one pair, three tests. expected is a 5-bit value, sum plus carry.
$display("watch: %0d + %0d = %0d", a, b, {cout, sum});   // 1. you decide
expected = a + b;                                          // 2. the test decides
if ({cout, sum} !== expected) wrong++;
expected = {cout, sum};                                    // 3. copied from the design
if ({cout, sum} !== expected) copied++;

$display prints a line. The braces in {cout, sum} join the carry and the sum into one 5-bit number. wrong++ adds 1 to a count.

!== means "differs from", and it compares every bit exactly. That matters when a bit is x. The plain != gives an unknown answer if any bit is x. An if treats unknown as false, so the wrong result slips through as a pass. With !==, an x bit simply counts as different, and the check fails as it should.

Printed by the lab (Icarus).
watch: 2 + 2 = 4
watch: 7 + 1 = 8
watch: 3 + 5 = 0
check: FAIL 3 + 5 gave 0, expected 8
watch: 9 + 6 = 15
watch: 12 + 3 = 15
watch: 4 + 4 = 8
watch: 15 + 1 = 16
watch: 6 + 9 = 15
self-checking test: 1 of 8 wrong
copying test: 0 of 8 wrong

The watch lines are the hook's eight lines. The wrong one, 3 + 5 = 0, sits third among seven correct ones. The self-checking test flags it at once with a FAIL line. Its verdict says 1 of 8 wrong: rule 4 at work. The copying test says 0 of 8 wrong on the same broken adder. Rule 2 explains why: it compared the adder with itself.

Notice that the copying test looks just as tidy as the real one. Nothing in its output warns you. The only way to find out is rule 5: break something and see whether the test notices.

Traps

The first trap is the reference that borrows from the design. Nobody writes expected = dut_sum on purpose. A reference model is a second program that works out the answer. It does the same thing more quietly if it reuses the design's own function. So does a table made by running the design.

The second trap is the check that waits in the wrong place. A comparison inside an if that only runs on some cycles checks only those cycles. A test with no failures may simply have skipped every check.

The third trap is the green log nobody reads to the end. The test counts errors but prints "test finished" whatever the count. Make the last line a verdict, and make the run fail when the count is not zero.

Where This Goes Next

This lesson built the smallest real test. These lessons build it out.

Where each idea is taught in depth
The idea from this pageWhere it is taught
The parts a bigger bench needsThe Parts of a Testbench
Where an expected value can come fromWhere the Right Answer Comes From
Choosing the inputs of a test on purposeDirected Tests
Tasks and functions, used here for applyTasks, Functions and Arguments
Comparisons with unknown values, and why !== mattersFour-State vs Two-State Types
A full self-checking bench for a FIFOBuild the Monitor and Scoreboard

Try It

Run the lab. Find the wrong watch line before you read the FAIL line.

Fix the adder (add the missing carry term). Predict all three verdicts.

Change the self-checking test to compare only sum with expected[3:0]. Which wrong results could it now miss?

RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: watch: 2 + 2 = 4 watch: 7 + 1 = 8 watch: 3 + 5 = 0 check: FAIL 3 + 5 gave 0, expected 8 watch: 9 + 6 = 15 watch: 12 + 3 = 15 watch: 4 + 4 = 8 watch: 15 + 1 = 16 watch: 6 + 9 = 15 self-checking test: 1 of 8 wrong copying test: 0 of 8 wrong

Check Yourself

Predict the outputThe same three tests now apply 1 + 7, 3 + 5 and 2 + 6 to the buggy adder. All three pairs hit the bug. What is the last line printed?
// adder4: a 4-bit adder, written one bit at a time (a ripple-carry adder).
module adder4 (input logic [3:0] a, b, output logic [3:0] sum, output logic cout);
  logic c1, c2, c3;
  assign sum[0] = a[0] ^ b[0];
  assign c1     = a[0] & b[0];
  assign sum[1] = a[1] ^ b[1] ^ c1;
  assign c2     = (a[1] & b[1]) | (a[1] & c1) | (b[1] & c1);
  assign sum[2] = a[2] ^ b[2] ^ c2;
  assign c3     = (a[2] & b[2]) | (a[2] & c2);           // one term is missing
  assign sum[3] = a[3] ^ b[3] ^ c3;
  assign cout   = (a[3] & b[3]) | (a[3] & c3) | (b[3] & c3);
endmodule

module tb;
  logic [3:0] a, b, sum;
  logic cout;
  int wrong = 0, copied = 0;
  logic [4:0] expected;
  adder4 dut (.a, .b, .sum, .cout);
  task automatic apply(input logic [3:0] x, y);
    a = x; b = y; #1;
    // 1. A test you watch: it prints, and a person must spot a wrong line.
    $display("watch: %0d + %0d = %0d", a, b, {cout, sum});
    // 2. A self-checking test: it works out the right answer itself and compares.
    expected = a + b;
    if ({cout, sum} !== expected) begin wrong++; $display("check: FAIL %0d + %0d gave %0d, expected %0d", a, b, {cout, sum}, expected); end
    // 3. A test that cannot fail: its "right answer" is copied from the design.
    expected = {cout, sum};
    if ({cout, sum} !== expected) copied++;
  endtask
  initial begin
    apply(1, 7);
    apply(3, 5);
    apply(2, 6);
    $display("self-checking test: %0d of 3 wrong", wrong);
    $display("copying test: %0d of 3 wrong", copied);
  end
endmodule
ConceptWhat are the three jobs of every test?
ConceptWhy is a test you have to watch not enough for a regression?
ConceptHow do you check that a new test can fail?
ConceptIn the lab, why did the copying test report 0 wrong on a broken adder?

Common Mistakes

Wrong Print values and check them by eye.

A person misses a wrong line among thousands, and a missed line looks like a pass.

Fix Make the test work out each expected value and compare it.

Wrong Take the expected value from the design.

The test then compares the design with itself and can never fail.

Fix Work out the expected value from the inputs, apart from the design.

Wrong Trust a new test because it passes.

A test that cannot fail also passes.

Fix Break the design on purpose once and watch the test fail.

Remember

  • Every test drives, observes and decides; a test you must watch leaves the decision to a tired person.
  • The expected value must be worked out apart from the design.
  • A test that cannot fail is worse than no test, and looks exactly like a good one.
  • Make every new test fail once, on a design broken on purpose, before you trust it.

Practice

Report a problem with this page