exp-test-maintainability
它能做什么
这项技能审阅 .NET 测试代码中重复的设置、断言和复制粘贴的测试方法,然后提出整理建议。适合测试项目变得杂乱、重复的开发者。
适用场景
- ✓当你的测试文件多次重复相同的设置或断言时。
- ✓当许多相似的测试方法可以合并为一个数据驱动的测试时。
- ✓当你希望在不立即改动的情况下,获得测试套件的重构思路的时候。
技能链接
https://github.com/dotnet/skills/blob/main/plugins/dotnet-experimental/skills/exp-test-maintainability/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“请在我的支付测试项目中找出重复的设置和复制粘贴的测试,并建议更清晰的结构。”
每周 5 个新技能,1 分钟读完。
工作原理
智能体会阅读测试文件,并在有用时阅读被测代码,然后报告重复的模式,并给出修改前后的建议。它不会修改文件。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
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 using these framework markers:
| Framework | Test class markers | Test method markers |
|---|---|---|
| MSTest | [TestClass] |
[TestMethod], [DataTestMethod] |
| xUnit | (none — convention-based) | [Fact], [Theory] |
| NUnit | [TestFixture] |
[Test], [TestCase], [TestCaseSource] |
| TUnit | (none — convention-based) | [Test] |
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.AreEqualcalls across methods
Potential refactorings:
- Extract a custom assertion helper (e.g.,
AssertValidOrder(order, expectedTotal, expectedStatus)) - Use framework-specific assertion extensions
- Introduce a
Verifymethod 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]/`
常见问题
它会修改我的测试文件吗?
不会。它只做分析,并报告你可以选择应用的建议。
它能建议参数化测试吗?
能。它可以建议把复制粘贴的测试改写为数据驱动的测试。
它会编写新测试吗?
不会。编写新测试需要使用另一项技能。
详情
作者的原始描述
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
repo: dotnet/skills
热度
- GitHub 星标
- 5.8k
- 排名
- #14581
- 许可证
- MIT
- 更新时间
- 2026/11/2
评论
- …
同一仓库的更多技能
create-custom-agent
5.8k帮你的智能体创建 VS Code 自定义智能体文件,为每个智能体设置专属工具、指令以及向其他智能体的交接。
create-skill
5.8k帮你的智能体创建新的智能体技能,包括有效的名称、清晰的触发描述,以及符合标准的 SKILL.md 文件。
csharp-scripts
5.8k帮你的智能体通过 .NET 命令行工具以单个文件运行小型 C# 程序,无需创建完整项目。
dotnet-pinvoke
5.8k帮你的智能体在 .NET 中正确调用原生 C 或 C++ 库,涵盖函数签名、字符串处理、内存和安全句柄。