Mutation testing on changed lines only, in plain words
The idea in one paragraph
Mutation testing asks a simple question about your tests: if the code were wrong, would any test fail? To find out, it makes the code wrong on purpose. It changes one thing, runs the tests, and watches. If a test fails, the test caught the mistake. If every test still passes, the mistake went unnoticed. The idea is not new. It has been around for decades.
The words you will see
A deliberate mistake is called a mutant, or a break. RedMachine uses two words for the outcome.
- KILLED means a test failed with the line broken. A test noticed the break. A break that makes the code crash or raise counts too.
- SURVIVED means every test passed with the line broken. No test noticed the break, whatever the test names say.
A third word, UNVIABLE, covers a line where no break still builds. It is listed for a person to look at, and it is not counted as proven.
What a break looks like
The breaks are plain. RedMachine flips a comparison (> to <), a sign (+ to -), a word (true to false), or the condition of an if. If none of those fits, it deletes the line, or in Python replaces it with pass. It tries them in a fixed order and uses the first one that still builds. A separate check command, such as a compile of the file, decides whether the broken code still builds. A break that does not build would only prove that the compiler works.
Each line gets one break by default. That is a limit, and the receipt does not hide it: a proven line may still hide a different wrong version that no test notices.
Why the whole program needs so many test runs
Applied to a whole program, mutation testing needs one run of your tests for each thing it breaks, and a program has things to break on nearly every line. At one test run per line, 60 lines and a ten-minute suite come to ten hours of test runs. Because of this, the technique is rarely part of everyday work.
Changed lines only
RedMachine narrows the job to the lines of one git change. A change is everything that differs between your work and the point where your branch left the base branch:
- commits on your branch,
- staged and unstaged edits,
- new source files that git is not ignoring.
The base is the first of main, master, origin/main, origin/master or origin/HEAD that exists, or the branch you name with --base. Only lines that were added or edited are judged. Lines you removed are not, with one exception: if a removal brings other code back to life, such as the lines after a deleted early return, those lines are judged.
Some lines are left out because there is nothing to break: blank lines, comments, imports, docstrings, most declarations, test code and lines made only of brackets. The run counts them by reason and prints the counts, so you can see what was set aside.
You can see the scope before anything is broken. redmachine --list prints the base, the test command and the changed lines it would judge. It breaks nothing and writes no receipt.
What narrowing to changed lines changes
When an assistant hands you a piece of work, the question you care about is whether its tests check that piece. The lines that matter are the lines it just changed. Restricting the run to them has three effects.
- The number of test runs follows the number of changed lines, not the number of lines in the program. Fewer changed lines mean fewer test runs.
- The answer is about the work in front of you. A PASS means that no counted line of this change survived. UNVIABLE and waived lines are listed, not proven.
- The result is a list of specific lines, each with the broken version that every test accepted. That list is something a person or an assistant can act on.
Here is what a run costs. RedMachine runs your tests once on unchanged code. Then, for each counted line, it runs a check for each break it tries and your tests once with the line broken. Each counted line costs at least about one second plus one test run, so the time depends on how long your tests take and on the number of lines you changed. By default it runs the tests for at most 60 lines, and later lines are marked NOT_TRIED, which fails the run, until you raise the limit with --max. You can give it a test command narrowed to the code you changed, with --test. A narrower command can leave a line SURVIVED that only the rest of the suite would notice.
What it does not cover
- Lines you did not change are not judged. A PASS says nothing about them.
- It does not write the missing tests and it does not find bugs.
- It needs a git repository with at least one commit, a base to compare against, a test command that exits 0 when the tests pass and not 0 when one fails, tests that pass with nothing broken, and a licence key to judge.
To see what a finished run reports, read how to read a RedMachine receipt. For the questions people ask, see the questions page.