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.
- Driver
- DUT
- Monitor
- Model
- Test
- Result
- Data
- Analysis
- Control
- Drive
The test chooses inputs and puts them on the design's pins.
Follow the calls, values and waits
- data a=3 b=5: Stimulus → Design under test
- Observe
It reads what the design produced.
Follow the calls, values and waits
- data sum=0: Design under test → Response
- Expect
From the same inputs, it works out the right answer without asking the design.
Follow the calls, values and waits
- data 3+5=8: Stimulus → Expected
- 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.
| Question | |
|---|---|
| 1 | What are the three jobs of every test? |
| 2 | Why is a test you have to watch not enough? |
| 3 | Where must the expected value come from? |
| 4 | What makes a test unable to fail? |
| 5 | How 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
| Word | Meaning |
|---|---|
| Test | Code that drives a design, observes it, and decides pass or fail. |
| Testbench | Everything around the design that runs tests: the code that sets inputs, the checks, the clock. Often shortened to "bench". |
| Stimulus | The input values a test puts on the design. |
| Expected value | The output the test believes is right, worked out apart from the design. |
| Self-checking | Able to decide pass or fail without a person reading the output. |
| Regression | A large set of tests run again and again, often every night, to catch new bugs. |
| Waveform | A 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.
| What it does | Why it cannot fail |
|---|---|
| Copies its expected value from the design | It compares the design with itself |
| Has a check that is never reached | The comparison sits in a branch the test never runs |
| Compares only some of the output | The wrong bits are in the part it ignores |
| Ends before the design answers | There is nothing to compare, so nothing mismatches |
| Counts errors but never reports them | The 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
Rule 1: Every test drives, observes and decides.
Leave out the decision and a person has to make it, line by line.
Rule 2: Work out the expected value apart from the design.
An answer taken from the design can never disagree with it.
Rule 3: Compare everything that matters.
Every output bit, every time the specification says it is valid.
Rule 4: End with one verdict.
PASS or FAIL with a count, so a regression can sort a thousand runs.
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.
$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.
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 wrongThe 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.
| The idea from this page | Where it is taught |
|---|---|
| The parts a bigger bench needs | The Parts of a Testbench |
| Where an expected value can come from | Where the Right Answer Comes From |
| Choosing the inputs of a test on purpose | Directed Tests |
| Tasks and functions, used here for apply | Tasks, Functions and Arguments |
| Comparisons with unknown values, and why !== matters | Four-State vs Two-State Types |
| A full self-checking bench for a FIFO | Build 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?
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 wrongCheck Yourself
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.