Lesson 53 of 93 · 6 min read · Updated
Mutation Testing in .NET with Stryker.NET: Testing Your Tests
On this page
The previous lesson showed that coverage only tells you which lines ran. Mutation testing answers the harder question: if the code were wrong, would your tests notice? A mutation testing tool makes small, deliberate changes to your production code and runs your tests against each one. This lesson explains how that works, runs Stryker.NET against the loyalty points calculator, and maps each mutant to the test row that catches it.
Technical Requirements
- .NET 11 SDK
- xUnit 2.9.3
- FluentAssertions 7.2.2 (Apache-2.0; 8.x is commercial)
- Stryker.NET, installed as a .NET global tool (
dotnet-stryker, open source under Apache-2.0) - The
LoyaltyPointsCalculatorclass and theCoverageAndMutationTestsclass from the previous lessons
How Mutation Testing Works
A mutant is a copy of your code with one small change, the kind of slip a developer makes on a bad day: < becomes <=, *= becomes /=, Math.Min becomes Math.Max, a statement is removed. For each mutant, the tool runs the tests that cover the changed line and records the outcome:
- Killed: at least one test failed. Good. Your suite noticed the bug.
- Survived: all tests passed. Bad. Either an assertion is missing or a scenario is missing.
- No coverage: no test ran the changed line at all.
- Timeout: the mutant caused an infinite loop or a very slow path. This usually counts as killed.
The mutation score is the share of mutants your tests killed. Unlike coverage, it is hard to inflate. A test without real assertions runs the mutated line, passes anyway, and lets the mutant survive. The BAD_HighCoverage_ZeroProtection test from the previous lesson is the clearest example: its coverage is close to 100%, and the number of mutants it kills is close to zero.
Running Stryker.NET
Install the tool once, then run it from the test project folder:
Stryker finds the project that the test project references, builds it, creates the mutants and runs the tests for each one. When the test project references more than one project, tell Stryker which one to mutate:
At the end it prints the mutation score and writes an HTML report under a StrykerOutput folder. The report shows your source code with every mutant marked as killed or survived, so you can click through the survivors one by one.
Mutants in the Calculator and the Rows That Kill Them
Here are the lines of LoyaltyPointsCalculator.Calculate where the interesting mutants live:
M06.BestPractices/LoyaltyPointsCalculator.cs
And here is the data-driven test, where each row is written to kill a specific mutant:
M06.BestPractices.Tests/CoverageAndMutationTests.cs
| Mutation | Test row that kills it |
|---|---|
amountSpent < MinimumSpend becomes <= | [InlineData(100.00, Standard, false, 10)] |
1.25m becomes 1.20m | [InlineData(1_000, Silver, false, 125)] |
multiplier *= 2m becomes *= 3m | [InlineData(1_000, Gold, true, 300)] |
Math.Min becomes Math.Max | [InlineData(100_000, Gold, true, 5_000)] |
Check one by hand. With <=, a spend of exactly 100 returns 0 instead of 10, so the 100.00 row fails. With Math.Max, a spend of 100,000 for a Gold member on a campaign day returns 30,000 instead of 5,000, so the cap row fails.
Stryker's built-in mutators work on operators, method calls and statements. It generates the < to <= and the Math.Min to Math.Max mutants itself, and for multiplier *= 2m it generates *= to /=, which the same campaign row kills (Gold on a campaign day would earn 75 points instead of 300). Changes to numeric literals, such as 1.25m to 1.20m, are mutants you can apply by hand: edit the value, run the tests, watch a row fail, and undo the change. Doing that a few times is a good way to build the habit of asking "which test would catch this?"
Reading a Survivor
The useful output of a Stryker run is the list of survivors. Each one points to a change in behavior that no test notices. Take the guard clause at the top of the method:
M06.BestPractices/LoyaltyPointsCalculator.cs
Stryker mutates amountSpent < 0 to amountSpent <= 0. With that change, a spend of exactly 0 throws instead of returning 0 points. No test in the project calls Calculate with 0, so this mutant survives. The fix is one more row, for example [InlineData(0, MemberTier.Standard, false, 0)], which documents that a zero spend is valid and earns nothing.
For each survivor, ask one of three questions:
- Is a scenario missing? Add a row or a test, as with the zero spend above.
- Is an assertion missing? The test runs the line but does not check the value that changed.
- Is it an equivalent mutant? Some changes do not alter behavior at all, for example a mutation in code whose result is never used. Those cannot be killed; mark them as ignored and move on.
Using Mutation Testing Well
Mutation testing is slow, because it runs the tests many times. A few habits keep it practical:
- Run it on domain logic, where the rules are, not on generated code, DTOs or wiring.
- Run it nightly or before a release rather than on every commit. Stryker.NET also has a
sinceoption that limits mutation to files changed against a given branch, which makes it usable in pull requests. - Do not chase a 100% score. Some survivors are equivalent mutants, and others sit in code where a bug costs little. Use the score as a trend and the survivors as a to-do list.
A common mistake is to treat the mutation score as the new coverage number and set a hard gate on it. The real value is in reading the survivors: each one is a concrete bug your suite would let through.
Summary
- Mutation testing changes production code in small ways and checks whether your tests fail.
- Killed mutants show real protection; survivors show a missing assertion or scenario.
- Assertion-free tests reach high coverage but kill almost no mutants.
- Install Stryker.NET with
dotnet tool install -g dotnet-strykerand rundotnet strykerfrom the test project. - Write test rows that target boundaries, multipliers and caps; each row should kill a specific mutant.
- Treat survivors as a to-do list and ignore equivalent mutants instead of chasing 100%.