Lesson 53 of 93 · 6 min read · Updated

Mutation Testing in .NET with Stryker.NET: Testing Your Tests

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

  1. .NET 11 SDK
  2. xUnit 2.9.3
  3. FluentAssertions 7.2.2 (Apache-2.0; 8.x is commercial)
  4. Stryker.NET, installed as a .NET global tool (dotnet-stryker, open source under Apache-2.0)
  5. The LoyaltyPointsCalculator class and the CoverageAndMutationTests class 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:

  1. Killed: at least one test failed. Good. Your suite noticed the bug.
  2. Survived: all tests passed. Bad. Either an assertion is missing or a scenario is missing.
  3. No coverage: no test ran the changed line at all.
  4. 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:

dotnet tool install -g dotnet-stryker
cd M06.BestPractices.Tests
dotnet stryker

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:

dotnet stryker --project M06.BestPractices.csproj

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

if (amountSpent < MinimumSpend)
{
return 0;
}

var basePoints = (int)(amountSpent / 10m);

var multiplier = tier switch
{
MemberTier.Gold => 1.50m,
MemberTier.Silver => 1.25m,
_ => 1.00m,
};

if (isCampaignDay)
{
multiplier *= 2m;
}

var points = (int)(basePoints * multiplier);

return Math.Min(points, MaximumPoints);

And here is the data-driven test, where each row is written to kill a specific mutant:

M06.BestPractices.Tests/CoverageAndMutationTests.cs

[Theory]
// The same lines, but the EXPECTED RESULT is written for each one.
// If the < in the production code becomes <=, the 100.00 row breaks: the mutant dies.
[InlineData(99.99, MemberTier.Standard, false, 0)]
[InlineData(100.00, MemberTier.Standard, false, 10)]
// If the Silver multiplier changes from 1.25 to 1.20, this row breaks: the mutant dies.
[InlineData(1_000, MemberTier.Silver, false, 125)]
// If the campaign multiplier changes from 2 to 3, this row breaks: the mutant dies.
[InlineData(1_000, MemberTier.Gold, true, 300)]
// If Math.Min becomes Math.Max, this row breaks: the mutant dies.
[InlineData(100_000, MemberTier.Gold, true, 5_000)]
public void GOOD_SameCoverage_StrongProtection(
decimal amountSpent, MemberTier tier, bool isCampaignDay, int expected)
{
LoyaltyPointsCalculator.Calculate(amountSpent, tier, isCampaignDay)
.Should().Be(expected);
}
MutationTest 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

if (amountSpent < 0)
{
throw new ArgumentOutOfRangeException(nameof(amountSpent), "Spending cannot be negative.");
}

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:

  1. Is a scenario missing? Add a row or a test, as with the zero spend above.
  2. Is an assertion missing? The test runs the line but does not check the value that changed.
  3. 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:

  1. Run it on domain logic, where the rules are, not on generated code, DTOs or wiring.
  2. Run it nightly or before a release rather than on every commit. Stryker.NET also has a since option that limits mutation to files changed against a given branch, which makes it usable in pull requests.
  3. 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

  1. Mutation testing changes production code in small ways and checks whether your tests fail.
  2. Killed mutants show real protection; survivors show a missing assertion or scenario.
  3. Assertion-free tests reach high coverage but kill almost no mutants.
  4. Install Stryker.NET with dotnet tool install -g dotnet-stryker and run dotnet stryker from the test project.
  5. Write test rows that target boundaries, multipliers and caps; each row should kill a specific mutant.
  6. Treat survivors as a to-do list and ignore equivalent mutants instead of chasing 100%.