📁 Sangam Dhakal - Portfolio v2.0 - Netscape Navigator 4.0
_
□
×

📚 Interview question

What is mutation testing, and why can a test suite with 90% code coverage still be weak? Explain how Stryker.NET works, what a mutation score means, how you read surviving mutants, and what it costs to run.

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.

Mid-Level Theory Intermediate code-coverage dotnet mutation-testing stryker-net test-quality testing

🎯 Key points

  • Coverage measures what code executed, not what the tests checked; assertion-free tests still give coverage
  • Mutation testing changes the code (mutants) and checks a test fails: killed = good, survived = gap
  • Stryker.NET mutators: arithmetic, equality and boundary, boolean, logical, string, LINQ, block removal
  • Mutation score = detected / (detected + undetected); timeouts count as detected, compile errors and ignored mutants are excluded, no-coverage counts as undetected
  • Thresholds high/low/break in stryker-config.json; break fails the build
  • Cost: re-runs tests per mutant; mitigated by mutant schemata, per-test coverage analysis and --since incremental runs
  • Equivalent mutants can't be killed and lower the score; review and suppress them with a reason, don't chase 100%
  • Best on core business logic with fast unit tests; code behind I/O, static state or slow integration tests makes results noisy and runs slow

⏱ 30-second answer

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.

🗣 Full answer (senior)

How I would answer this in an interview

"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, swaps Any() for All(), 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. In stryker-config.json you 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 main on 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."


Coverage vs mutation score

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

How Stryker.NET classifies mutants

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 pays off

  • Pricing, tax (GST), eligibility and fee calculations, where a boundary mistake costs money.
  • Validation and authorisation rules.
  • Libraries shared across many services.

Where it doesn't: controllers that only map, EF Core configuration, generated code, logging.

Production example

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.

🔍 What the interviewer is checking

Checks whether the candidate understands the limits of coverage and how to measure test effectiveness pragmatically.

Green flags

  • Explains why coverage overstates test quality, with an example.
  • Describes mutants, killed vs survived, and mutation score.
  • Knows the cost and how to limit it (scoping, --since, nightly runs).
  • Mentions equivalent mutants and not chasing 100%.

Red flags

  • Thinks 90% coverage means well-tested.
  • Would run full mutation testing on every commit across the whole solution.
  • Can't explain what a surviving mutant means.

📚 Learn in depth

1. The idea

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.

2. How Stryker.NET runs

 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.

3. Common mutators

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

4. Mutation score and thresholds

 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.

5. Equivalent mutants

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.

6. Keeping the cost down

  • Mutate only the core project (--project Billing.Domain.csproj).
  • --since:main mutates only files changed against main, ideal for PRs.
  • Baselines (--with-baseline) reuse results from the last full run.
  • Run the full suite nightly, not on every commit.
  • Keep unit tests fast; slow integration tests multiply the cost.

Try it yourself

  1. dotnet tool install -g dotnet-stryker and run dotnet stryker in a test project folder.
  2. Open the HTML report and find a surviving boundary mutant; add an [InlineData] at the boundary and rerun.
  3. Write a test with no assertion and see its coverage vs mutation result.
  4. Add thresholds with a break value and confirm the exit code changes.
  5. Try --since:main after a small change and compare run time with a full run.

💻 Code

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

⚠ Common mistakes

  • Mistake: Treating code coverage as a measure of test quality. Why it is wrong: tests without meaningful assertions still produce coverage. Instead: use coverage to find untested code and mutation testing to find weak tests.
  • Mistake: Running Stryker.NET across the whole solution on every PR. Why it is wrong: run times balloon and the team turns it off. Instead: scope to core projects and use --since main on PRs, full runs nightly.
  • Mistake: Chasing a 100% mutation score. Why it is wrong: equivalent mutants can't be killed, and the last few percent cost more than they return. Instead: set realistic thresholds (for example break at 55–60, high at 80) on code that matters.
  • Mistake: Writing tests that assert implementation details just to kill mutants. Why it is wrong: they make the suite brittle without adding real protection. Instead: kill mutants with behaviour assertions, often boundary values.
  • Mistake: Including slow integration tests in the mutation run. Why it is wrong: the cost multiplies by the number of mutants. Instead: run mutation testing against fast unit tests.
  • Mistake: Ignoring "no coverage" mutants. Why it is wrong: they show code no test touches, sometimes dead code. Instead: add a test or delete the code.

🏛 Senior insight: trade-offs

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.

🔗 Follow-ups the interviewer may ask

  1. When: Candidate mentions code coverage numbers. Why: Checks the candidate knows how coverage is collected and reported in CI.
    How to answer

    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.

  2. When: Candidate mentions surviving boundary mutants such as >= changed to >. Why: Checks the candidate can design test inputs, not just count tests.
    How to answer

    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.

🛠 Real-world problems: how you handle them

A 94% coverage billing library scores 61% on mutation testing
Situation
A shared [billing] library used by [six] services calculated fees, GST and discounts for [N] invoices a month. It had 94% line coverage and a coverage gate in CI, yet two pricing bugs had escaped to production in a quarter.
Signals
Escaped defects were both boundary mistakes (a `>` that should have been `>=`). Coverage reports showed the lines as covered. PR reviews showed many tests asserting only `NotNull` or that no exception was thrown.
Investigation
Ran `dotnet stryker` on the billing project. Report: mutation score 61%, 140 survived mutants, 22 with no coverage. Grouped survivors in the HTML report by mutator: most were equality and boundary mutants, then arithmetic mutants in rounding code. Checked a sample against tests to confirm missing boundary cases and weak assertions.
Resolution
Immediate: added boundary-value `[Theory]` cases for every threshold rule and replaced `NotNull` assertions with exact expected values, taking the score to 86% in two days. Deleted 15 methods flagged as no coverage that were dead code. Long term: added Stryker.NET to PR builds with `--since main` and a break threshold of 75.
Prevention
Mutation score on the billing project tracked on a dashboard; break threshold in `stryker-config.json`; PR template asks for boundary cases on any rule change; nightly full Stryker run with baseline.

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.

↩ Usually asked after

More in Test Types

Last updated 23 Nov 2026.

🌐 Sangam's Webring

⏪ Previous 🏠 Main Next ⏩
💬 Chat with me