Pin legacy behaviour before an agent refactors it, then grade the tests with mutation testing
An agent's first test suite for old code usually checks what you'd expect, not what the code does. Mutation testing shows the difference in under a minute.

You’re about to refactor a shipping calculator that nobody on the team wrote and nobody wants to touch. You ask an agent for tests first, it hands back four neat ones, they pass, and you start the refactor with a green light that means almost nothing. Four tests can’t pin forty decisions.
The fix is two steps. First, ask for characterisation tests: tests that record what the code does today, bugs included, rather than what it should do. Second, grade those tests with mutation testing, which breaks the code in small ways and counts how many breakages your tests notice. That grade is the part an agent can’t talk its way around.
The class and the usual first attempt
Take this class. It’s invented, but it has the traits that make legacy code hard to test: magic numbers, a special case buried in the middle, and behaviour no spec mentions.
namespace Shipping;
public static class ShippingCalculator
{
// Legacy shipping calculator. Nobody remembers why it works this way.
public static decimal ShippingCost(
decimal weightKg, string country, bool express = false, decimal subtotal = 0m)
{
if (weightKg <= 0)
return 0m;
if (subtotal >= 100 && country == "GB" && !express)
return 0m;
var baseCost = weightKg <= 1 ? 4.99m : 4.99m + (weightKg - 1) * 1.5m;
if (weightKg > 20)
baseCost += 15;
if (country != "GB")
baseCost *= 2.5m;
if (express)
baseCost += 7.5m;
return Math.Round(baseCost, 2);
}
}
Ask an agent “write xUnit tests for ShippingCost” and you’ll typically get tests of the behaviour a reasonable person would expect. A suite like this is typical:
using static Shipping.ShippingCalculator;
namespace Shipping.Tests;
public class ShippingCalculatorTests
{
[Fact]
public void LightUkParcel() =>
Assert.Equal(4.99m, ShippingCost(0.5m, "GB"));
[Fact]
public void InternationalIsDearer() =>
Assert.True(ShippingCost(0.5m, "US") > ShippingCost(0.5m, "GB"));
[Fact]
public void ExpressCostsMore() =>
Assert.True(ShippingCost(2m, "GB", express: true) > ShippingCost(2m, "GB"));
[Fact]
public void FreeOver100() =>
Assert.Equal(0m, ShippingCost(2m, "GB", subtotal: 150m));
}
$ dotnet test
Passed! - Failed: 0, Passed: 4, Skipped: 0, Total: 4, Duration: 365 ms - Shipping.Tests.dll (net10.0)
Every test reads like a requirement, and none of them would catch most refactoring mistakes. Two use inequalities, so changing 2.5 to 3.0 for international orders sails through. Nothing touches the 20 kg surcharge. Nothing checks the free-shipping threshold at exactly 100.
Grade the suite before you trust it
Mutation testing tools take your source, make one small change at a time (flip a <= to <, swap a + for a -, change a constant), and run your tests against each variant. A variant your tests fail on is killed. One they pass on survived, which means your suite can’t see that change.
Stryker.NET does this for C# and is Apache-2.0 licensed. It installs as a .NET tool and runs from the test project’s directory, with no config needed to start:
dotnet tool install -g dotnet-stryker
cd tests/Shipping.Tests
dotnet stryker --reporter ClearTextTree
Against the four-test suite, Stryker created 28 mutants. One was filtered out as a block already covered by another mutant, 18 were killed, 8 survived and 1 sat in code no test reaches. The score was 66.67%. The survivors, trimmed:
All files [18/28 (66.67%)]
└── ShippingCalculator.cs [18/28 (66.67%)]
├── [Survived] Equality mutation on line 9
│ ├── [-] weightKg <= 0
│ └── [+] weightKg < 0
├── [Survived] Equality mutation on line 11
│ ├── [-] subtotal >= 100
│ └── [+] subtotal > 100
├── [Survived] Arithmetic mutation on line 14
│ ├── [-] 4.99m + (weightKg - 1) * 1.5m
│ └── [+] 4.99m - (weightKg - 1) * 1.5m
├── [Survived] Equality mutation on line 15
│ ├── [-] weightKg > 20
│ └── [+] weightKg >= 20
├── [NoCoverage] AddAssignmentExpression to SubtractAssignmentExpression
│ mutation on line 16
│ ├── [-] baseCost += 15
│ └── [+] baseCost -= 15
...
Look at the last entry. The heavy-parcel surcharge could turn into a discount and every test would stay green. That’s the list to show your agent.
Write the characterisation suite
The method has two moves: probe the code, then pin what it returned. The probe is a throwaway program that calls the method with inputs chosen around every branch and boundary. On .NET 10 it can be a single file run with dotnet run probe.cs, which keeps it out of the solution. It can be written by hand, or by the agent from this prompt:
Read ShippingCalculator.ShippingCost. Do not change it.
Write a throwaway file-based app, probe.cs, that references the project
with a #:project directive and calls the method with inputs that exercise
every branch and both sides of every boundary (weights at 0, 1, just over
1, 20, just over 20; each country case; express on and off; subtotal at
99.99 and 100; combinations of express and free shipping). Print each
input with the return value.
Run it and show me the output. Then turn the output into an xUnit
[Theory] fed by [MemberData], where the expected values are exactly what
the method returned. Do not correct anything you think is a bug. Instead,
list every result that looks wrong at the end, separately.
The last instruction is the one that matters. An agent that sees a number it dislikes will quietly adjust the expectation to what it thinks the code should say, and now your safety net disagrees with production. The net has to match production first. Opinions come later and go in a separate list.
The probe prints something like this, trimmed to the interesting rows:
(0, GB, express: False, subtotal: 0) 0
(-1, GB, express: False, subtotal: 0) 0
(1, GB, express: False, subtotal: 0) 4.99
(1.01, GB, express: False, subtotal: 0) 5.00
(20, GB, express: False, subtotal: 0) 33.49
(20.01, GB, express: False, subtotal: 0) 48.50
(0.5, US, express: False, subtotal: 0) 12.48
(2, GB, express: True, subtotal: 0) 13.99
(2, GB, express: False, subtotal: 100) 0
(2, GB, express: False, subtotal: 99.99) 6.49
(2, GB, express: True, subtotal: 500) 13.99
(1, gb, express: False, subtotal: 0) 12.48
Several of these would make a reviewer stop. A negative weight ships free. Express orders never get the free-shipping threshold, even at 500. A lowercase "gb" is charged as international, so the same parcel to the same country costs 12.48 instead of 4.99. And 1.01 kg costs 5.00 rather than 5.01, because Math.Round on a decimal rounds halves to even by default, so 5.005 goes down. Each of those is something a refactor would “fix”, and each fix would change what customers pay.
The pinned file is a plain xUnit theory. One C# detail shapes it: attribute arguments can’t be decimal, so [InlineData(1.5m)] fails with error CS0182. MemberData with TheoryData gets around that:
using static Shipping.ShippingCalculator;
namespace Shipping.Tests;
// Pinned from what the code returned when probed. Not from a spec.
public class ShippingCalculatorTests
{
public static TheoryData<decimal, string, bool, decimal, decimal> Pinned => new()
{
{ 0m, "GB", false, 0m, 0m },
{ -1m, "GB", false, 0m, 0m },
{ 1m, "GB", false, 0m, 4.99m },
{ 1.01m, "GB", false, 0m, 5.00m },
{ 20m, "GB", false, 0m, 33.49m },
{ 20.01m, "GB", false, 0m, 48.50m },
{ 0.5m, "US", false, 0m, 12.48m },
{ 2m, "GB", true, 0m, 13.99m },
{ 2m, "GB", false, 100m, 0m },
{ 2m, "GB", false, 99.99m, 6.49m },
{ 2m, "GB", true, 500m, 13.99m },
{ 1m, "gb", false, 0m, 12.48m },
{ 3m, "GB", false, 0m, 7.99m },
{ 25m, "FR", false, 0m, 139.98m },
{ 0.5m, "GB", false, 0m, 4.99m },
{ 2m, "US", true, 0m, 23.72m },
};
[Theory]
[MemberData(nameof(Pinned))]
public void Pinned_values_match_current_behaviour(
decimal kg, string country, bool express, decimal subtotal, decimal expected) =>
Assert.Equal(expected, ShippingCost(kg, country, express, subtotal));
}
Keep the comment. In six months someone will see { 1m, "gb", false, 0m, 12.48m } and file a bug, and the comment says the value was recorded on purpose.
Run the grade again
Replace the four happy-path tests with the pinned theory (16 cases), confirm it passes, and run Stryker again:
$ dotnet test
Passed! - Failed: 0, Passed: 16, Skipped: 0, Total: 16, Duration: 71 ms - Shipping.Tests.dll (net10.0)
$ dotnet stryker --reporter ClearTextTree
All files [26/28 (96.30%)]
└── ShippingCalculator.cs [26/28 (96.30%)]
├── [Survived] Equality mutation on line 14
│ ├── [-] weightKg <= 1
│ └── [+] weightKg < 1
...
From 18 killed to 26, and one survivor. That one is worth reading rather than fighting. At exactly 1 kg the else branch computes 4.99m + (1 - 1) * 1.5m, which is 4.99 either way, so <= 1 and < 1 behave identically. It’s an equivalent mutant: different source, same behaviour. No test can kill it, and none should try.
Make it yours
On the front end the same approach works with Vitest and StrykerJS. Install @stryker-mutator/core and @stryker-mutator/vitest-runner as dev dependencies, set "testRunner": "vitest" in stryker.config.json, and run npx stryker run. The probe can be a throwaway script, and the pinned values go in a test.each table.
Slow suites are the main cost. Mutation testing runs your tests against every mutant, so a suite that takes a minute makes a few dozen mutants take most of an hour. Stryker’s --mutate option takes a glob, so point it at one file, for instance --mutate "**/ShippingCalculator.cs".
If the method has side effects or reads a clock, pin its inputs first. A characterisation test on code that calls DateTime.UtcNow is a flaky test with extra steps. Inject a TimeProvider, then pin.
For output that’s a document rather than a number (a rendered email, a generated config), store the full output as a snapshot file and compare against it. Same idea, bigger values.
What goes wrong
Characterisation tests pin bugs. That’s by design, and it’s also the biggest risk: someone treats the green suite as endorsement. Keep the “looks wrong” list the agent produced and file it somewhere your team will find it.
They also only cover the inputs you probed. A probe that skips a country code, a null string or a weight with awkward rounding leaves that behaviour unprotected, and mutation testing can’t flag a branch of behaviour that no mutant touches. It grades the tests against the code that exists, not against the inputs real customers send. If you have production logs, sample real inputs into the probe.
Stryker writes a StrykerOutput/ folder next to wherever it runs, full of reports. Add it to .gitignore.
And don’t bother if the code is about to be deleted, or if it’s already well covered by tests someone trusts. This earns its cost on code that’s old, untested and about to be changed.