Checking how many agents are working

How this works
Contact
← Blog
A steel mousetrap set on a dark bench with a small brass gear in the jaws, lit in cold teal with a warm amber highlight, the AMARBARO mark and wordmark centered over it.
✎blogbericht

A test that cannot fail is not a test

The short version

A test suite is only known to work once you have changed the code on purpose and watched at least one test fail.

Part 7 in Gates that lie. Part 6 explained what a gate is.

What this is about

A test is a small program that checks another program. If the other program is right, the test passes. If it is wrong, the test fails.

But what if the test is broken and always passes? Then it tells you nothing, and you will not know.

There is a simple way to find out. Break your own code on purpose, then run the test. If the test notices, it works. If it stays green, you have found a test that cannot fail.

Why it matters

Tests give us confidence. We ship code because the tests are green. If some of those tests would stay green on broken code, that confidence is false, and false confidence is worse than none, because you stop looking.

Tests that cannot fail are more common than you would guess. A test can look at the wrong value. It can compare something to itself. It can allow so much error that every answer fits. All of these look fine from the outside.

How it works

The method has an official name, mutation testing, but the idea is small. A mutant is a copy of your code with one tiny change. You run your tests against the mutant. A mutant that makes a test fail is killed. A mutant that passes every test survives, and a survivor is a hole in your tests.

Step 1: Start with passing tests

Run your tests on the real code. They must be green. If they are not, fix that first.

Step 2: Make one small change

Pick one line. Change one thing: turn a plus into a minus, a greater-than into a less-than, a one into a zero. Make only one change at a time, so that you know which change was caught.

Step 3: Run the tests

If at least one test fails, good. That change was noticed.

If all tests pass, the mutant survived. Either the line is not covered by any test, or the test does not look closely enough.

Step 4: Undo, and repeat

Put the line back. Pick another. Do this for each interesting line.

Step 5: Fix the survivors

For each survivor, add or sharpen a test until it dies. Then you know that line is guarded.

A real example from our tools

Our tool mojo-port helps move a small Python command-line tool into Mojo, a newer programming language. It proves the new version behaves like the old one by running both on the same test cases and comparing what they print and how they exit.

The tool ships with a demo called wcl, a line and word counter. With the real port, 9 of 9 cases match.

Now break the port on purpose. In the demo, the exit status for an unreadable file is set to 1. We change it to 0. Run the check again and it fails. It prints the failing case, with the Python result and the Mojo result side by side. Only 7 of 9 cases match now.

Then the tool goes one step further. Its mutants command makes up to eight changes by itself, one operator per line, and checks that some test case notices each one. In the demo it reports 8 of 8 killed. The tool exists for exactly the reason in this article: a green verify only covers the cases you thought of, and the mutant check covers the ones you did not.

We use the same idea on our GPU code. In one case we removed one small correction term from a maths function that is shared by the CPU and GPU paths. The checks, which ran 40,017 generated inputs, required the mutant to go over one unit of error in the last place on the log function. It did.

What counts and what does not

Things that count as a real catch:

  • A test fails and names the thing you changed.
  • The failure message points to the right place.
  • The unchanged code still passes.

Things that do not count:

  • A test fails on everything. That only proves it is loud.
  • A mutant that does not even compile. The compiler caught it, not your tests.
  • A test that fails for some other reason, such as a missing file.

Glossary

  • Test case: one input and the output you expect for it.
  • Mutant: a copy of code with one deliberate small change.
  • Killed: a mutant that made at least one test fail.
  • Survivor: a mutant that every test accepted.
  • Oracle: a trusted version you compare against. In our port tool, the Python original is the oracle.
  • Exit status: the number a program returns when it ends. Zero usually means success.

Try it

Pick any small script you wrote and a test for it. Change one operator. Run the test. Did it fail?

For a ready-made setting, clone mojo-port and run its wcl demo. Change status = 1 to status = 0 in examples/wcl/wcl.mojo, run verify, and read the side-by-side output. Then restore the file and run mutants. You will have seen a test fail, and a mutant check kill its targets, in about ten minutes.

What to remember

A test you have never seen fail is a guess. Break the code once on purpose, and the guess becomes evidence.

Reacties

Nog geen reacties.

Inloggen om te reageren.