Mutation testing with Stryker.NET: why line coverage overstates test quality, how mutants are generated and killed, mutation score and thresholds, surviving mutants, incremental runs and run-time cost.
Code coverage tells you which lines ran during tests, not whether any test would notice if they were wrong. A test with no assertions still gives coverage. Mutation testing measures that directly: a tool makes small changes to your code, called mutants (> to >=, + to -, true to false, removing a statement), runs the tests against each, and expects at least one test to fail. A failing test kills the mutant; if all tests pass, the mutant survived, which shows a gap. Stryker.NET does this for C# via dotnet stryker and reports a mutation score with build-breaking thresholds. The trade-off is run time: it re-runs tests many times, so you scope it to core logic and use incremental --since runs.
"Coverage only tells you a line executed while tests ran. It says nothing about whether a test checked the result. I've seen suites at 90% coverage where half the tests called a method and asserted that it didn't throw. Mutation testing measures what coverage can't: whether the tests would notice a bug.
Stryker.NET does it by making small, deliberate changes to the compiled code, called mutants. It flips
>to>=,&&to||,+to-, replaces a string with an empty one, swapsAny()forAll(), or removes a statement block. Then it runs the tests that cover each mutant. If a test fails, the mutant is killed, which is what you want. If every test passes, it survived, which means you could introduce that exact bug and the suite wouldn't notice. The mutation score is roughly killed divided by all the mutants that tests could have caught. Instryker-config.jsonyou set high, low and break thresholds, and the break threshold fails the build.The mistake I see is treating it like coverage and chasing a number across the whole solution. It's expensive, because tests run once per mutant, although Stryker reduces that with mutant schemata, which compile all mutants into one assembly and switch them on at run time, and with per-test coverage so it only runs relevant tests. I'd run it on the core domain project, use
--since mainon pull requests so only changed code is mutated, and treat surviving mutants as code review prompts: either a missing assertion or dead code. Some mutants are equivalent and can't be killed, so 100% is the wrong goal."
| Line or branch coverage | Mutation score | |
|---|---|---|
| Measures | Code executed by tests | Bugs tests would detect |
| Fooled by | Tests with weak or no assertions | Equivalent mutants (rare) |
| Cost | Near zero (one run) | Many test runs |
| Use | Find untested code | Find weak tests |
| Status | Meaning | Action |
|---|---|---|
| Killed | A test failed | Good |
| Survived | Code covered, all tests passed | Add or strengthen an assertion |
| No coverage | No test runs this code | Add a test, or delete dead code |
| Timeout | Mutant caused an infinite loop | Usually counted as detected |
| Compile error / Ignored | Mutant invalid or excluded | None |
Where it doesn't: controllers that only map, EF Core configuration, generated code, logging.
A billing library had 94% line coverage. A first Stryker.NET run scored 61%: 140 survivors, most on boundary checks like >= 1000m where tests only used values far from the boundary. Adding about 30 boundary-value test cases took a day and raised the score to 86%. Two months later a change to a GST rounding rule was caught by one of those new tests on the PR.
Checks whether the candidate understands the limits of coverage and how to measure test effectiveness pragmatically.
Green flags
--since, nightly runs).Red flags
Mutation testing is based on two observations: programmers usually make small mistakes, and tests that catch small mistakes also tend to catch bigger ones (the coupling effect). So if your tests detect deliberately injected small bugs, they are probably good at detecting real ones.
dotnet stryker
|
1. build the solution, find the project under test and its test projects
2. parse source with Roslyn, apply mutators to the syntax tree
3. mutant schemata: compile ALL mutants into one assembly,
each guarded by if (ActiveMutation == 42) { mutated } else { original }
4. initial test run with coverage capture: which tests cover which mutants
5. for each mutant: activate it, run only the tests that cover it
6. classify (killed / survived / no coverage / timeout), write HTML report
Mutant schemata avoid recompiling for every mutant, which is the reason it is feasible at all on large codebases.
| Mutator | Original | Mutant |
|---|---|---|
| Equality / boundary | a > b |
a >= b, a < b |
| Arithmetic | a + b |
a - b |
| Logical | a && b |
a \|\| b |
| Boolean | true |
false |
| Unary | !flag |
flag |
| String | "AUD" |
"" |
| LINQ | First() |
Last(), Any() → All() |
| Block removal | { Audit(); } |
{ } |
| Null coalescing | a ?? b |
a, b |
score = detected / (detected + undetected)
detected = killed + timeout
undetected = survived + no coverage
stryker-config.json sets thresholds: { high: 80, low: 60, break: 50 }. Below break, the command exits non-zero and fails CI.
Some mutants don't change behaviour, for example changing i < list.Count to i != list.Count in a loop that increments by one. No test can kill them. Mark them ignored with a // Stryker disable once comment and a reason, rather than writing silly tests.
--project Billing.Domain.csproj).--since:main mutates only files changed against main, ideal for PRs.--with-baseline) reuse results from the last full run.dotnet tool install -g dotnet-stryker and run dotnet stryker in a test project folder.[InlineData] at the boundary and rerun.thresholds with a break value and confirm the exit code changes.--since:main after a small change and compare run time with a full run.A rule, a weak test that still gives 100% coverage, and the stronger tests that kill the mutants:
namespace Billing.Domain;
public static class Shipping
{
// Free shipping for orders of $100 or more within Australia.
public static decimal Fee(decimal subtotal, string country) =>
country == "AU" && subtotal >= 100m ? 0m : 9.95m;
}
namespace Billing.Domain.Tests;
using Xunit;
public sealed class ShippingTests
{
// Weak: 100% line coverage, but `>=` -> `>` and `&&` -> `||` both survive.
[Fact]
public void Fee_runs() => Assert.True(Shipping.Fee(250m, "AU") >= 0);
// Strong: boundary and both branches of the condition.
[Theory]
[InlineData(100.00, "AU", 0.00)] // kills >= -> >
[InlineData(99.99, "AU", 9.95)] // kills >= -> <=
[InlineData(150.00, "NZ", 9.95)] // kills && -> ||
[InlineData(50.00, "NZ", 9.95)]
public void Fee_is_free_from_100_in_australia_only(decimal subtotal, string country, decimal expected) =>
Assert.Equal(expected, Shipping.Fee(subtotal, country));
}
// stryker-config.json in the test project folder
{
"stryker-config": {
"project": "Billing.Domain.csproj",
"reporters": ["html", "progress", "dashboard"],
"thresholds": { "high": 80, "low": 60, "break": 55 },
"mutation-level": "Standard",
"since": { "enabled": true, "target": "main" }
}
}
dotnet tool install -g dotnet-stryker
dotnet stryker # uses stryker-config.json
--since main on PRs, full runs nightly.Mutation testing is the honest answer to "how good are our tests?", but it is a tool for focused investment, not a blanket metric. I'd introduce it on the one or two projects where a logic error costs real money or trust, report the score as a trend, and use surviving mutants as review prompts. A coverage gate plus a mutation score on core logic is much harder to game than either alone.
Real codebases make it messier than the textbook. Code tangled with static state, I/O or DateTime.Now produces mutants that only integration tests could kill, which makes runs slow and scores noisy; that is a design signal to push logic into pure functions. Timeouts usually mean a mutant created an infinite loop and are counted as detected, but a sudden rise in timeouts often means the test timeout is too tight on a slow agent. Equivalent mutants pull the score down permanently, so the target is a stable trend on core code, with each suppressed mutant carrying a // Stryker disable once comment explaining why.
| Option | When it fits | Cost | When not to |
|---|---|---|---|
| Coverage gate only | Any project, cheap baseline | Near zero | As the only quality signal |
Stryker.NET on PRs with --since |
Core domain libraries | Minutes per PR | Thin CRUD services with little logic |
| Nightly full Stryker run | Shared libraries, financial rules | CI hours overnight | Huge solutions without scoping |
| Manual review of tests | Always | Reviewer time | Never a replacement for automation |
Measure: mutation score trend on core projects, number of survivors per PR, and whether escaped defects fall.
dotnet test builds the test projects and runs them through the test platform, using the adapter the project references (xUnit, NUnit or MSTest). I use --filter to pick tests, for example --filter "Category=Unit&FullyQualifiedName~Orders". I use --logger trx so CI can show the results, and --collect "XPlat Code Coverage" with Coverlet for coverage, which I turn into a report. When tests hang or crash, --blame-hang-timeout and --blame-crash capture dumps. The command exits non-zero on failure, so the pipeline fails. In .NET 10 the SDK can also run in Microsoft.Testing.Platform mode, which you opt into in global.json.
Boundary-value analysis picks test inputs at and either side of each boundary in a rule, because off-by-one mistakes cluster there. For "free shipping from $100", test $99.99, $100.00 and $100.01, not just $50 and $250. It pairs with equivalence partitioning: one value per class of input (below, at, above; AU vs other countries). In xUnit, a [Theory] with [InlineData] rows for each boundary kills the >= → > and >= → <= mutants that Stryker.NET reports as survivors.
How to tell it (STAR)
**S:** Our [billing] library had 94% coverage but two pricing bugs had still reached production in [a quarter]. **T:** I wanted to find out why our tests weren't catching them. **A:** I ran Stryker.NET on the project. The mutation score was 61%, and most survivors were boundary mutants, like `>=` changed to `>`, because our tests only used values far from the thresholds. I added boundary-value test cases, replaced weak assertions, removed dead code, and added Stryker to PR builds with a break threshold. **R:** The score went to 86% in [two days], and in the next [few months] a GST rounding change was caught on the PR instead of in production.
Last updated 23 Nov 2026.