Asking an AI assistant for tests is one of the places where it saves the most time: in seconds you have a full suite, all green, with high coverage. The interesting question comes next: would those tests fail if the code broke?
That’s what this short series is about:
- Part 1 (this one): what makes a test valuable, and why coverage doesn’t prove it.
- Part 2: how to ask the AI for tests and what to review in what it gives you.
- Part 3: how to check that your tests work, by breaking the code on purpose.
I’m assuming you already know Swift Testing. If not, start with this post on unit testing.
What the evidence says
Two recent studies are worth reading together, because they seem to contradict each other:
- A paper accepted in IEEE Transactions on Software Engineering points out that line and branch coverage are weak indicators of a suite’s ability to catch bugs, and mentions suites with 100% coverage and only a 4% mutation score. We’ll come back to that term in part 3.
- An August 2026 preprint compares tests written by recent Claude models with the ones in Django and Pandas, and concludes that they are not weaker than human-written tests. It’s a preprint, it only covers Python, and it looks at tests from real tools.
Here’s how I read them: the problem isn’t who writes the test. The problem is accepting a test just because it passes and raises coverage, and that applies just as much when a person wrote it.
An example: 100% coverage
This code was made up for the post. It calculates the total of a purchase and decides whether shipping is free. Amounts are in cents:
public func total(subtotal: Int, discountPercent: Int) -> Int {
guard discountPercent > 0 else { return subtotal }
let discount = subtotal * min(discountPercent, 100) / 100
return subtotal - discount
}
public func hasFreeShipping(subtotal: Int) -> Bool {
subtotal >= 50_000
}
And these are its tests. They look reasonable, they all pass, and they reach 100% line, function and region coverage:
@Test func appliesDiscount() {
let subtotal = 10_000
let percent = 20
let expected = subtotal - (subtotal * percent / 100)
#expect(total(subtotal: subtotal, discountPercent: percent) == expected)
}
@Test func noDiscount() {
#expect(total(subtotal: 10_000, discountPercent: 0) == 10_000)
}
@Test func freeShipping() {
#expect(hasFreeShipping(subtotal: 60_000))
#expect(!hasFreeShipping(subtotal: 10_000))
}
Now the same functions with a different suite:
@Test("The total subtracts the discount", arguments: [
(subtotal: 10_000, percent: 20, expected: 8_000),
(subtotal: 10_000, percent: 0, expected: 10_000),
(subtotal: 10_000, percent: 100, expected: 0),
(subtotal: 10_000, percent: 150, expected: 0),
(subtotal: 10_000, percent: -10, expected: 10_000),
])
func totalApplies(subtotal: Int, percent: Int, expected: Int) {
#expect(total(subtotal: subtotal, discountPercent: percent) == expected)
}
@Test("Free shipping starts at exactly 50,000", arguments: [
(subtotal: 49_999, expected: false),
(subtotal: 50_000, expected: true),
])
func freeShippingBoundary(subtotal: Int, expected: Bool) {
#expect(hasFreeShipping(subtotal: subtotal) == expected)
}
To compare them, bugs are introduced into the code one at a time, and both suites are run. This is the result:
| Bug introduced | First suite | Second suite |
|---|---|---|
Free shipping with > instead of >= | Misses it | Catches it |
Remove the min(_, 100) cap | Misses it | Catches it |
Guard with != 0 instead of > 0 | Misses it | Catches it |
| Add the discount instead of subtracting it | Catches it | Catches it |
| Divide by 10 instead of 100 | Catches it | Catches it |
With 100% coverage, the first suite caught 2 of 5 bugs. The second one, 5 of 5.
Coverage tells you which lines ran. It doesn’t tell you whether any test would have noticed the result was wrong.
What makes a test valuable
Comparing the two suites gives four differences, and they’re what I look for in any test.
1. It fails when the behavior breaks
This is the only criterion that really matters; the others help you get there. A test that can’t fail protects nothing, however green it looks.
2. The expected value doesn’t come from the same logic
Look at the first test in the weak suite:
let expected = subtotal - (subtotal * percent / 100)
It repeats the formula from the code. If the formula is wrong, the test is wrong in the same way and passes. On the other hand, expected: 8_000 is a fact you can check with a calculator: a 20% discount on 100 pesos leaves 80.
3. It covers the edges, not just the typical case
The >= bug only shows up at the exact value of the boundary. Testing with 60,000 and 10,000 runs the line but leaves it unguarded. That’s why the second suite uses 49,999 and 50,000.
The same goes for the discount: 150% and -10% are odd inputs, and that’s exactly where the cap and the guard do their job.
4. It reads like a specification
“Free shipping starts at exactly 50,000” says what should happen and why. If it fails one day, you know which rule broke before you open the code.
Coverage is still useful
There’s no need to throw it out. It works very well for the opposite of what it’s usually used for: it shows you which code no test runs. 40% tells you there’s a lot left untested. 100% doesn’t tell you it’s well tested.
swift test --enable-code-coverage
swift test --show-codecov-path
In part 2 we look at how to ask the AI for tests like the second suite from the start, and what to review in what it gives you.