Test Maintainability Assessment
Analyze .NET test code for maintainability issues: duplicated boilerplate, copy-paste test methods, and structural repetition across test methods and classes. Produce a report of refactoring opportunities with concrete before/after suggestions. The goal is analysis only — do not modify any files.
When to Use
- User asks to find duplicated code or boilerplate in tests
- User wants to know where test code can be DRY-ed up
- User asks to reduce test duplication, improve test readability, or clean up test boilerplate
- User asks for refactoring opportunities in a test suite
- User wants to identify shared setup or teardown candidates
- User asks "what patterns repeat across my tests?"
- User wants to centralize test data, introduce builders or helpers
When Not to Use
- User wants to write new tests from scratch (use
writing-mstest-tests)
- User wants to detect anti-patterns or code smells (use
test-anti-patterns)
- User wants to actually perform the refactoring (help them directly, this skill only analyzes)
Inputs
| Input |
Required |
Description |
| Test code |
Yes |
One or more test files or a test project directory to analyze |
| Production code |
No |
The code under test, for context on what abstractions might help |
| Scope |
No |
Whether to analyze within a single class or across multiple classes |
Workflow
Step 1: Gather the test code
Read all test files the user provides or references. If the user points to a directory or project, scan for all test files — see the dotnet-test-frameworks skill for framework-specific markers.
Step 2: Identify maintainability issues
Scan for these categories:
Category 1: Repeated object construction
Look for the same object being constructed in 3+ test methods with identical or near-identical parameters.
Indicators:
new ClassName(...) appearing with identical arguments in multiple tests
- Multiple tests creating the same "system under test" with similar configuration
- Repeated mock/fake/stub creation with the same setup
Potential refactorings:
- Extract a factory method or test helper (e.g.,
CreateSut(), CreateDefaultOrder())
- Use
[TestInitialize]/constructor/[SetUp] for shared construction
- Introduce a builder pattern for complex objects with many variations
Example — before:
[TestMethod]
public void Process_ValidOrder_Succeeds()
{
var logger = new FakeLogger();
var email = new FakeEmailService();
var inventory = new FakeInventory(stock: 100);
var processor = new OrderProcessor(logger, email, inventory);
// ...
}
[TestMethod]
public void Process_EmptyItems_Fails()
{
var logger = new FakeLogger();
var email = new FakeEmailService();
var inventory = new FakeInventory(stock: 100);
var processor = new OrderProcessor(logger, email, inventory);
// ...
}
After — extract factory:
private static OrderProcessor CreateProcessor(int stock = 100)
{
return new OrderProcessor(new FakeLogger(), new FakeEmailService(), new FakeInventory(stock));
}
Category 2: Repeated assertion patterns
Look for the same sequence of assertions appearing in 3+ test methods.
Indicators:
- Multiple tests asserting the same set of properties on a result object
- Repeated null-check-then-value-check sequences
- Same collection of
Assert.AreEqual calls across methods
Potential refactorings:
- Extract a custom assertion helper (e.g.,
AssertValidOrder(order, expectedTotal, expectedStatus))
- Use framework-specific assertion extensions
- Introduce a
Verify method that checks a standard set of properties
Category 3: Copy-paste test methods
Look for test methods with near-identical bodies differing only in input values or a single parameter.
Indicators:
- 3+ methods with the same structure but different literal values
- Methods that could be collapsed into
[DataRow]/[Theory]/[TestCase]
- Test names that follow a pattern like
Method_Input1_Result, Method_Input2_Result
Potential refactorings:
- Convert to parameterized tests with
[DataRow]/[InlineData]/[TestCase]
- Use
[DynamicData]/[MemberData]/[TestCaseSource] for complex inputs
- Prefer
[DataRow] with DisplayName over [DynamicData] when all values are compile-time constants. Reserve [DynamicData] for computed or complex values.
- Add
DisplayName for non-obvious parameter values. [DataRow("Gold", 100.0, 90.0)] is self-explanatory; [DataRow(3, 7, 42)] is not.
Category 4: Duplicated setup/teardown logic
Look for initialization or cleanup code repeated across test classes.
Indicators:
- Multiple
[TestInitialize]/[SetUp] methods with similar bodies
- Repeated database seeding, file creation, or HTTP client configuration
- Same
using/IDisposable cleanup pattern across classes
Potential refactorings:
- Extract a shared test base class or fixture
- Use composition with a shared helper class
- Create a test context factory
Category 5: Repeated test infrastructure
Look for structural patterns shared across test classes.
Indicators:
- Same mock interfaces configured identically in multiple classes
- Repeated
HttpClient setup with similar DelegatingHandler patterns
- Same logging/configuration scaffolding across test classes
Potential refactorings:
- Extract a shared test fixture or helper library
- Create reusable fake implementations
- Introduce a test harness class
Step 3: Apply calibration rules
Before reporting, filter findings through these rules:
- Only report at 3+ occurrences. Two similar setups are not boilerplate — they may be intentional clarity.
- Don't flag simple constructors.
new Calculator() or new List<int>() is not meaningful boilerplate. Don't recommend builders for new User(1, "Alice") either.
- Respect intentional verbosity. If each test is self-contained and reads clearly on its own, explicit setup per test is a valid choice. Note it but don't flag it as a problem.
- Distinguish structural similarity from true duplication. Tests that follow AAA (Arrange-Act-Assert) will look similar by nature. Only flag when the actual code (not just the structure) is duplicated.
- Consider the blast radius of refactoring. A helper shared across 20 tests creates coupling. Note the trade-off.
- If tests are already well-maintained, say so. A report finding only minor opportunities is perfectly valid. Acknowledge what's already good.
Step 4: Report findings
Present findings in this structure:
- Summary — How many patterns found, broken down by category. If the test suite is clean, lead with that.
- Findings by category — For each pattern found:
- Category name and description
- Locations: list the specific test methods and files involved
- The duplicated code pattern (show a representative sample)
- Suggested refactoring with a concrete before/after example
- Estimated impact: how many lines/methods would be simplified
- Refactoring priority — Rank findings by:
- Occurrence count (more occurrences = higher value)
- Complexity of the duplicated code (complex setup > simple construction)
- Risk (low-risk extractions first)
- Trade-offs — For each suggestion, note:
- What readability is gained
- What locality/independence is lost
- Whether it's worth it given the occurrence count
Validation
Common Pitfalls
| Pitfall |
Solution |
| Flagging AAA structure as duplication |
The Arrange-Act-Assert pattern is not boilerplate — flag only when the actual code repeats |
| Suggesting extraction for 2 occurrences |
Wait for 3+ before recommending extraction |
| Recommending base classes for everything |
Prefer composition (helpers, factories) over inheritance |
| Ignoring the readability cost |
Every extraction adds indirection — note the trade-off |
Flagging simple new X() as boilerplate |
Only flag complex construction with multiple parameters or configuration |
| Recommending DRY at the expense of test isolation |
Tests that share mutable state through helpers become coupled — warn about this |
1---2name: exp-test-maintainability3description: Detects duplicate boilerplate, copy-paste tests, and structural maintainability issues across .NET test suites. Use when the user asks to reduce repetition, consolidate similar test methods, convert copy-paste tests to data-driven parameterized tests, suggest a better test structure, or identify refactoring opportunities. Identifies repeated construction, assertion patterns, copy-paste methods convertible to DataRow/Theory/TestCase, redundant setup/teardown, and shared infrastructure. Produces an analysis report with concrete before/after suggestions. Works with MSTest, xUnit, NUnit, and TUnit. DO NOT USE FOR: writing new tests (use writing-mstest-tests), reviewing test quality or anti-patterns (use test-anti-patterns), or deep mock auditing (use exp-mock-usage-analysis).4license: MIT5---67# Test Maintainability Assessment89Analyze .NET test code for maintainability issues: duplicated boilerplate, copy-paste test methods, and structural repetition across test methods and classes. Produce a report of refactoring opportunities with concrete before/after suggestions. The goal is analysis only — do not modify any files.1011## When to Use1213- User asks to find duplicated code or boilerplate in tests14- User wants to know where test code can be DRY-ed up15- User asks to reduce test duplication, improve test readability, or clean up test boilerplate16- User asks for refactoring opportunities in a test suite17- User wants to identify shared setup or teardown candidates18- User asks "what patterns repeat across my tests?"19- User wants to centralize test data, introduce builders or helpers2021## When Not to Use2223- User wants to write new tests from scratch (use `writing-mstest-tests`)24- User wants to detect anti-patterns or code smells (use `test-anti-patterns`)25- User wants to actually perform the refactoring (help them directly, this skill only analyzes)2627## Inputs2829| Input | Required | Description |30|-------|----------|-------------|31| Test code | Yes | One or more test files or a test project directory to analyze |32| Production code | No | The code under test, for context on what abstractions might help |33| Scope | No | Whether to analyze within a single class or across multiple classes |3435## Workflow3637### Step 1: Gather the test code3839Read all test files the user provides or references. If the user points to a directory or project, scan for all test files — see the `dotnet-test-frameworks` skill for framework-specific markers.4041### Step 2: Identify maintainability issues4243Scan for these categories:4445#### Category 1: Repeated object construction4647Look for the same object being constructed in 3+ test methods with identical or near-identical parameters.4849**Indicators:**50- `new ClassName(...)` appearing with identical arguments in multiple tests51- Multiple tests creating the same "system under test" with similar configuration52- Repeated mock/fake/stub creation with the same setup5354**Potential refactorings:**55- Extract a factory method or test helper (e.g., `CreateSut()`, `CreateDefaultOrder()`)56- Use `[TestInitialize]`/constructor/`[SetUp]` for shared construction57- Introduce a builder pattern for complex objects with many variations5859**Example — before:**60```csharp61[TestMethod]62public void Process_ValidOrder_Succeeds()63{64 var logger = new FakeLogger();65 var email = new FakeEmailService();66 var inventory = new FakeInventory(stock: 100);67 var processor = new OrderProcessor(logger, email, inventory);68 // ...69}7071[TestMethod]72public void Process_EmptyItems_Fails()73{74 var logger = new FakeLogger();75 var email = new FakeEmailService();76 var inventory = new FakeInventory(stock: 100);77 var processor = new OrderProcessor(logger, email, inventory);78 // ...79}80```8182**After — extract factory:**83```csharp84private static OrderProcessor CreateProcessor(int stock = 100)85{86 return new OrderProcessor(new FakeLogger(), new FakeEmailService(), new FakeInventory(stock));87}88```8990#### Category 2: Repeated assertion patterns9192Look for the same sequence of assertions appearing in 3+ test methods.9394**Indicators:**95- Multiple tests asserting the same set of properties on a result object96- Repeated null-check-then-value-check sequences97- Same collection of `Assert.AreEqual` calls across methods9899**Potential refactorings:**100- Extract a custom assertion helper (e.g., `AssertValidOrder(order, expectedTotal, expectedStatus)`)101- Use framework-specific assertion extensions102- Introduce a `Verify` method that checks a standard set of properties103104#### Category 3: Copy-paste test methods105106Look for test methods with near-identical bodies differing only in input values or a single parameter.107108**Indicators:**109- 3+ methods with the same structure but different literal values110- Methods that could be collapsed into `[DataRow]`/`[Theory]`/`[TestCase]`111- Test names that follow a pattern like `Method_Input1_Result`, `Method_Input2_Result`112113**Potential refactorings:**114- Convert to parameterized tests with `[DataRow]`/`[InlineData]`/`[TestCase]`115- Use `[DynamicData]`/`[MemberData]`/`[TestCaseSource]` for complex inputs116- Prefer `[DataRow]` with `DisplayName` over `[DynamicData]` when all values are compile-time constants. Reserve `[DynamicData]` for computed or complex values.117- Add `DisplayName` for non-obvious parameter values. `[DataRow("Gold", 100.0, 90.0)]` is self-explanatory; `[DataRow(3, 7, 42)]` is not.118119#### Category 4: Duplicated setup/teardown logic120121Look for initialization or cleanup code repeated across test classes.122123**Indicators:**124- Multiple `[TestInitialize]`/`[SetUp]` methods with similar bodies125- Repeated database seeding, file creation, or HTTP client configuration126- Same `using`/`IDisposable` cleanup pattern across classes127128**Potential refactorings:**129- Extract a shared test base class or fixture130- Use composition with a shared helper class131- Create a test context factory132133#### Category 5: Repeated test infrastructure134135Look for structural patterns shared across test classes.136137**Indicators:**138- Same mock interfaces configured identically in multiple classes139- Repeated `HttpClient` setup with similar `DelegatingHandler` patterns140- Same logging/configuration scaffolding across test classes141142**Potential refactorings:**143- Extract a shared test fixture or helper library144- Create reusable fake implementations145- Introduce a test harness class146147### Step 3: Apply calibration rules148149Before reporting, filter findings through these rules:150151- **Only report at 3+ occurrences.** Two similar setups are not boilerplate — they may be intentional clarity.152- **Don't flag simple constructors.** `new Calculator()` or `new List<int>()` is not meaningful boilerplate. Don't recommend builders for `new User(1, "Alice")` either.153- **Respect intentional verbosity.** If each test is self-contained and reads clearly on its own, explicit setup per test is a valid choice. Note it but don't flag it as a problem.154- **Distinguish structural similarity from true duplication.** Tests that follow AAA (Arrange-Act-Assert) will look similar by nature. Only flag when the actual code (not just the structure) is duplicated.155- **Consider the blast radius of refactoring.** A helper shared across 20 tests creates coupling. Note the trade-off.156- **If tests are already well-maintained, say so.** A report finding only minor opportunities is perfectly valid. Acknowledge what's already good.157158### Step 4: Report findings159160Present findings in this structure:1611621. **Summary** — How many patterns found, broken down by category. If the test suite is clean, lead with that.1632. **Findings by category** — For each pattern found:164 - Category name and description165 - Locations: list the specific test methods and files involved166 - The duplicated code pattern (show a representative sample)167 - Suggested refactoring with a concrete before/after example168 - Estimated impact: how many lines/methods would be simplified1693. **Refactoring priority** — Rank findings by:170 - Occurrence count (more occurrences = higher value)171 - Complexity of the duplicated code (complex setup > simple construction)172 - Risk (low-risk extractions first)1734. **Trade-offs** — For each suggestion, note:174 - What readability is gained175 - What locality/independence is lost176 - Whether it's worth it given the occurrence count177178## Validation179180- [ ] Every finding includes specific file and method locations181- [ ] Every finding shows the actual duplicated code, not just a description182- [ ] Every suggestion includes a concrete before/after example183- [ ] Findings are filtered through the 3+ occurrence threshold184- [ ] Simple constructors are not flagged185- [ ] Trade-offs are acknowledged for each suggestion186- [ ] If tests are clean, the report says so upfront187188## Common Pitfalls189190| Pitfall | Solution |191|---------|----------|192| Flagging AAA structure as duplication | The Arrange-Act-Assert pattern is not boilerplate — flag only when the actual code repeats |193| Suggesting extraction for 2 occurrences | Wait for 3+ before recommending extraction |194| Recommending base classes for everything | Prefer composition (helpers, factories) over inheritance |195| Ignoring the readability cost | Every extraction adds indirection — note the trade-off |196| Flagging simple `new X()` as boilerplate | Only flag complex construction with multiple parameters or configuration |197| Recommending DRY at the expense of test isolation | Tests that share mutable state through helpers become coupled — warn about this |