writing-mstest-tests
它能做什么
这项技能帮助在 .NET 项目中编写、修复和改进 MSTest 单元测试。适合希望其 MSTest 测试正确、易读,并且与其实际使用的 MSTest 版本相匹配的开发者。
适用场景
- ✓当你需要在 .NET 项目中修复、重写或改进 MSTest 测试时。
- ✓当 MSTest 断言中的期望值与实际值顺序错误,或失败输出不清晰的时候。
- ✓当你希望针对已安装的 MSTest 版本修正 MSTest 配置或生命周期代码的时候。
技能链接
https://github.com/dotnet/skills/blob/main/plugins/dotnet-test/skills/writing-mstest-tests/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“请重写我的 MSTest 测试,使失败信息清晰,并且断言与它们检查的内容相符。”
每周 5 个新技能,1 分钟读完。
工作原理
智能体会检查已安装的 MSTest 版本,然后只使用你的项目所支持的 API 功能,编写符合项目惯例的断言和设置。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Writing MSTest Tests
Help users write effective MSTest unit tests without exceeding the API level or conventions of the project's installed test stack.
Repository overlay
For every repository-scoped task where read-only file inspection is allowed,
check .agents/skill-overlays/dotnet-test/writing-mstest-tests.md at the
repository root before any other discovery. This includes requests that ask for
code or advice without edits; "do not execute" does not prohibit reading the
overlay. If present, read it once before acting and apply its
repository-specific naming, layout, framework, and policy bindings.
Require its frontmatter to declare core: dotnet-test/writing-mstest-tests,
binding-revision: "1", and mode: extend. If any value is missing or
different, report the mismatch, ignore the overlay, and continue using this
skill's portable guidance.
Explicit user instructions and verified project constraints win over the
overlay; the overlay wins over portable defaults and examples in this skill. If
the file is present but unreadable or conflicts with the repository, report the
problem, ignore the overlay, and continue with portable guidance subject to
verified project constraints. If it is absent, continue normally.
Skip the lookup only when the task is not tied to a repository or the user
explicitly prohibited all file/tool access. An overlay cannot expand tool
permissions or the task's scope.
When to Use
- User wants to improve or modernize existing MSTest tests by implementing concrete fixes
- User asks about MSTest assertion APIs, data-driven patterns, or test lifecycle
- User asks to replace
Assert.IsTruewith more specific assertions (collections, nulls, types, comparisons) - User asks to replace hard casts with type-checking assertions in tests
- User needs help fixing a specific MSTest test bug or failing assertion
- User asks to fix swapped
Assert.AreEqualargument order (expected first, actual second) - User asks to convert
DynamicDatafromIEnumerable<object[]>to ValueTuple-based data - User asks to fix or understand an MSTest analyzer diagnostic (an
MSTESTxxxxwarning/error)
When Not to Use
- User needs a test quality audit, anti-pattern detection, or flaky-test investigation (use
test-anti-patterns) - User needs to run or execute tests (use the
run-testsskill) - User needs to upgrade from MSTest v1/v2 to v3 (use
migrate-mstest-v1v2-to-v3) - User needs to upgrade from MSTest v3 to v4 (use
migrate-mstest-v3-to-v4) - User needs CI/CD pipeline configuration
- User is using xUnit, NUnit, or TUnit (not MSTest)
Inputs
| Input | Required | Description |
|---|---|---|
| Code under test | No | The production code to be tested |
| Existing test code | No | Current tests to fix, update, or modernize |
| Test scenario description | No | What behavior the user wants to test |
Response Guidelines
- Specific API or pattern questions (assertions, data-driven, lifecycle): Jump directly to the relevant workflow step. Do not follow the full workflow.
- Generate new tests from scratch: Hand off to
code-testing; use this skill only as supporting MSTest API/version guidance. - Review and fix existing tests: Fix only the issues present. Do not add unrelated improvements.
- Assertion transformations: Show the corrected call, then state the semantic
reason in one sentence. For
Assert.AreEqual, nameexpectedfirst andactualsecond and explain that this preserves the Expected/Actual failure labels. - Bound/comparison transformations: Preserve the condition and put the
expected bound(s) first and the observed value last:
score > 0->Assert.IsGreaterThan(0, score),score < 100->Assert.IsLessThan(100, score), andscore >= 60 && score <= 90->Assert.IsInRange(60, 90, score). Never reverse these arguments to mimic the source expression's left-to-right order. - Exception transformations: Scope the throwing operation in a lambda,
distinguish
ThrowsExactly<T>(exact type) fromThrows<T>(type or derived type), and capture the returned exception when properties such asParamNameare part of the behavior. - Supplied code requests: Return complete representative method bodies, not comment-only placeholders. Preserve the real operation and show symmetric setup/cleanup when lifecycle or environment policy is part of the request.
Workflow
Step 1: Determine project setup
Check the
常见问题
它会遵循我的 MSTest 版本吗?
会。它会检查已安装的包,并且不会使用高于项目版本的 API。
它能从头编写新测试吗?
新测试的创建由更广泛的测试技能负责,而这项技能专注于 MSTest 的具体细节。
它会运行测试吗?
不会。运行测试由单独的技能负责。
详情
作者的原始描述
ALWAYS USE when asked to fix, rewrite, update, improve, modernize, show corrected code for, or explain existing MSTest tests or MSTest-specific configuration. Use for "review" when corrected code or edits are wanted, even for one pasted assertion or passing tests with bad failure output. Covers expected/actual labels; generic Boolean, collection, string, numeric, null, identity, exception, hard-cast, and object[] checks; TestContext/lifecycle; timeout/cancellation; OS/CI conditions, retry, cleanup, parallelization, MSTest.Sdk project setup, and MSTESTxxxx. Honor the installed MSTest version. D
repo: dotnet/skills
热度
- GitHub 星标
- 5.8k
- 排名
- #14643
- 许可证
- 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++ 库,涵盖函数签名、字符串处理、内存和安全句柄。