SwiftiOSTestingAI

Unit testing with AI, part 3: break the code to check your tests

In part 1 we compared two suites, and in part 2 we saw how to ask for them and review them. What’s left is what answers the underlying question: would your tests notice a bug?

There’s a direct way to find out: introduce the bug and see what happens.

What mutation testing is

The idea is simple:

  1. You make a small change to the code, like >= to > or removing a condition. That change is called a mutant.
  2. You run the tests.
  3. If any test fails, the suite killed the mutant. If they all pass, the mutant survived, and you found something your tests don’t guard.

The mutation score is the percentage of mutants the suite kills. It’s what the study from part 1 measures, where there are suites with 100% coverage and a 4% mutation score.

Doing it by hand

You don’t need to install anything to start. Pick the function you’d most hate to see fail, like the one that charges the customer, and try changes like these:

Kind of changeExample
Change a boundary>= to >
Flip a condition== to !=
Change an operation- to +
Remove a safeguardDelete a guard or a min(_, 100)
Change a value/ 100 to / 10
Return something elsereturn true at the start

With the example from part 1, these are the results of six changes:

MutantWeak suiteSolid suite
>= to > in free shippingSurvivesKilled
Remove min(_, 100)SurvivesKilled
> 0 to != 0 in the guardSurvivesKilled
- to +KilledKilled
/ 100 to / 10KilledKilled
> 0 to >= 0 in the guardSurvivesSurvives

Fifteen minutes of this with your most critical function tells you more about your tests than any coverage percentage.

When a mutant survives, there isn’t always a problem

Look at the last row: neither suite catches it, not even the solid one. It isn’t a gap. With a discount of 0, the guard no longer returns early, but the calculation subtracts 0 and the result is identical.

This is called an equivalent mutant: a change to the code that doesn’t change the behavior, so no test can catch it. That’s why every survivor needs a judgment call, and why chasing a 100% mutation score isn’t worth it.

When a mutant survives, ask yourself:

  • Does it change what the caller sees? If not, it’s equivalent. Move on.
  • Does it change, but not in a way that matters? Maybe that code isn’t needed.
  • Does it change, and does it matter? A test is missing. Write it.

With an agent’s help

This mechanical work is a great task for an agent: apply a change, run the tests, revert and take notes. Commit first, so you can go back safely:

I want to check the tests for Pricing.swift with manual mutation testing.
Don't modify any test.

For each condition and operation in the file:
1. Apply a single small change (a boundary, an operator, removing a safeguard).
2. Run swift test.
3. Revert the change with git before moving on to the next one.

At the end, give me a table with each mutant and whether it survived. For the
ones that survived, tell me whether you think it's an equivalent mutant and why.

The two key instructions are “don’t modify any test” and “revert before moving on to the next one”. When it’s done, check with git status that the code is back to how it was.

The table is a starting point. The decision on each survivor is still yours, because only you know which rules matter.

With tools

If you want to automate it, for Swift there’s Muter, an open-source tool (MIT) that generates the mutants and runs your tests for you. I haven’t tried it yet, and its documentation doesn’t mention Swift Testing, so check that it works with your project before you adopt it.

Whatever the tool, mutation testing is slow, because it runs the tests many times. The sensible thing is to limit it to what matters: your most critical functions, or the files you changed on that branch.

The full checklist

This is what I take away from the series, for any test, wherever it comes from:

When asking for them

  • I gave it the rules, not just the code.
  • I asked for expected values written by hand and the edges of every rule.

When reviewing them

  • The expected value doesn’t repeat the code’s formula.
  • My logic runs, not just a stub.
  • Empty, zero, negative, the exact boundary and the error case are there.
  • There are no fixed-time waits.
  • Every name says which rule it protects.
  • If an expected value changed, I know why.

When checking them

  • I saw every test fail at least once.
  • I broke my critical functions on purpose and the tests noticed.
  • I reviewed every surviving mutant: equivalent, code that isn’t needed, or a missing test.

Wrapping up

AI makes writing tests cheap, and that’s great news: the work you used to put off now takes seconds. What doesn’t become automatic is deciding what has to be true in your code. With clear rules and a little time spent breaking things on purpose, those tests really do look after you.

Sources