Other

Pinning tests: making unfamiliar C# code safe to change

C# Corner Patrick Kearns
Other

Pinning (characterisation) tests capture how unfamiliar C# code behaves today, so a refactor that silently changes edge cases shows up immediately. Patrick Kearns shows how to turn observed boundary values into xUnit theories and what to keep once the cleanup is done.

Key points

  • Pinning tests take their expected values from the running code, not from a requirement document.
  • Cover boundaries explicitly: below, at and above a threshold; 100m versus 100.01m proves the `>` comparison.
  • Quirks like the case-sensitive `countryCode == "IE"` check only become visible once a case is recorded.
  • Use `[Theory]` with a fixed `TheoryData` via `[MemberData]`; never generate expectations by calling the production code.
  • Run the tests against the original implementation first, otherwise you capture the refactor instead of a baseline.
  • Pin observable effects too (published messages, database state, generated files) without mocking every internal call.

Real-world use

  • Simplifying a legacy calculation method where one awkward condition decides a customer's invoice.
  • Restructuring a report or JSON generator whose column order is consumed by another system.
  • Refactoring a message handler while proving the outgoing message did not lose a field.
  • Keeping sanitised production and support-case inputs as lasting regression cases.

This summary was generated by the agent from the original post on C# Corner. The full text and all rights belong to its author and publisher. Go to original

Report this story

Is something wrong with the summary, or do you own this content? Your message comes straight to my inbox.

Type of message

Related stories

Feedback

Found a bug or have an idea? Your message lands straight in my inbox.

Type of message