Mutant Hunter — Testing

Hunt bugs with mutation testing: 12 levels writing tests that catch mutants. A testing game that trains you to think like a tester.

What you learn in this game

Welcome, Mutant Hunter. Mutation testing is a technique for testing the quality of your test cases. A mutant is a slightly altered version of the code (e.g. + becomes -). A good test must be able to catch every mutant: if a test still passes after the code is mutated, it means the test isn't strong enough.

Boundary testing is a technique for testing at the edge points. If a condition uses >, a test at 0 can tell > apart from >=. This is one of the most effective ways to catch mutants.

A function with multiple branches (if/else) needs a test for every branch. If there are 3 paths, you need at least 3 assertions. This is called branch coverage: every branch must be traversed at least once.

From this level on, some of the value choices are what the mutants return. If your label matches a mutant's result, your test fails on the original function instead. Think: what does the original function output for this input?

Business functions often have guard clauses (input validation). A mutant can change || to &&, which means the validation condition is never true. A test with invalid input is very important for catching mutants in a guard clause.

Memoize stores the result of a computation. A mutant that removes the caching is hard to catch because the output stays the same; the function just gets called again. This shows the limit of mutation testing: not every mutant can be caught with output assertions alone.

What the levels ask

  1. Fill in the expected value in the assertion to catch all the mutants.
  2. The mutant changes > to >=. Pick the assertion that catches it.
  3. There are 2 mutants. Fill in both assertions to catch them all.
  4. Write an assertion that catches the mutant. Watch the edge case.
  5. The clamp function limits a value to the range [min, max]. Catch 3 mutants.
  6. It's the classic FizzBuzz. Catch the mutants at the boundary values.
  7. Test the palindrome function. Careful: the mutants change the comparison logic.
  8. Some of the choices are now what the mutants themselves return. Work out what the original function returns, then catch both mutants.
  9. Pick the expected value. Watch the return value when an item isn't found.
  10. This is a critical business function. Catch all the mutants with 3 assertions.
  11. Flatten a nested array and catch the recursive mutants.
  12. Test the memoize function. Make sure the cache works, and that the function still returns the correct result.