scaffold-dotnet-test-project
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseScaffold a .NET Test Project
搭建.NET测试项目
Create the smallest test project that fits the repository's existing build and
test conventions, wire it to the correct production project and build entry
point, and prove solution-level test discovery sees it. This skill scaffolds the
container for tests; it does not invent a solution-wide test architecture.
创建符合仓库现有构建和测试规范的最小测试项目,将其关联至正确的生产项目和构建入口点,并验证解决方案级别的测试发现机制能够识别它。本技能仅搭建测试容器,不会设计全解决方案的测试架构。
When to Use
适用场景
- A .NET solution or project has production code but no suitable test project.
- A user asks to "set up tests" or "add a test project" from a vague starting point.
- Tests pass when the new is targeted directly but CI cannot discover it.
.csproj - A multi-project solution needs a bounded test project for one production project.
- .NET解决方案或项目包含生产代码,但无合适的测试项目。
- 用户从模糊的起点要求「设置测试」或「添加测试项目」。
- 直接针对新的运行时测试通过,但CI无法发现该测试项目。
.csproj - 多项目解决方案需要为某个生产项目创建一个限定范围的测试项目。
When Not to Use
不适用场景
- A compatible test project already references the target production project.
Reuse it and continue with .
code-testing-agent - The request is specifically about authoring or modernizing MSTest test code.
Use after the project exists.
writing-mstest-tests - The repository's current build is broken for an unrelated reason. Report that blocker; do not redesign project structure to hide it.
- The user asks to migrate xUnit, NUnit, MSTest, TUnit, VSTest, or MTP.
- 已有兼容的测试项目引用了目标生产项目。请复用该项目并继续使用。
code-testing-agent - 请求明确涉及编写或现代化MSTest测试代码。请在项目创建完成后使用。
writing-mstest-tests - 仓库当前构建因无关原因失败。请报告该阻塞问题;不要通过重新设计项目结构来掩盖它。
- 用户要求迁移xUnit、NUnit、MSTest、TUnit、VSTest或MTP。
Inputs
输入参数
| Input | Required | Description |
|---|---|---|
| Repository or solution path | No | Discover from the current workspace when omitted |
| Production project | No | Infer the narrowest project in the requested scope |
| Test framework | No | Use an explicit choice; otherwise infer repository convention |
| Build entry point | No | Existing |
| 输入项 | 是否必填 | 描述 |
|---|---|---|
| 仓库或解决方案路径 | 否 | 若省略则从当前工作区自动发现 |
| 生产项目 | 否 | 从请求范围中推断最窄范围的项目 |
| 测试框架 | 否 | 使用明确指定的框架;否则推断仓库的规范 |
| 构建入口点 | 否 | CI使用的现有 |
Workflow
工作流程
Step 1: Establish the repository contract
步骤1:确定仓库约定
Inspect only the files needed to answer these questions:
- Which production project is in scope?
- What command does CI or the repository use to build and test?
- Does a suitable test project already reference that production project?
- Which test framework and runner do neighboring test projects use?
- Are package versions centrally managed by ,
Directory.Packages.props,Directory.Build.props, or an MSBuild SDK declaration?global.json - Which target framework(s) must the test project compile against?
Treat a test project as suitable only when its target framework can reference the
production project and its purpose matches the requested layer. Do not create a
second test project merely because its name differs from your preferred name.
No-op stop condition: when a suitable project already exists and is registered
in the requested build entry point, do not repair, normalize, convert, or replace
the solution. If the user did not ask for tests yet, report the existing project
path and stop with the workspace byte-for-byte unchanged.
仅检查需要回答以下问题的文件:
- 哪些生产项目在范围内?
- CI或仓库使用什么命令来构建和测试?
- 是否已有合适的测试项目引用了该生产项目?
- 相邻的测试项目使用哪种测试框架和运行器?
- 包版本是否由、
Directory.Packages.props、Directory.Build.props或MSBuild SDK声明集中管理?global.json - 测试项目必须针对哪些目标框架进行编译?
只有当测试项目的目标框架能够引用生产项目,且其用途与请求的层级匹配时,才认为该测试项目是合适的。不要仅仅因为名称与你偏好的不同就创建第二个测试项目。
无操作终止条件:当合适的项目已存在并已注册到请求的构建入口点时,不要修复、标准化、转换或替换解决方案。如果用户尚未要求添加测试,只需报告现有项目路径并保持工作区完全不变后终止操作。
Step 2: Choose one bounded project
步骤2:选择一个限定范围的项目
Default to one test project per production project, named according to repository
convention (, , and so on). For a vague multi-project
request, start with the project that owns the user-visible behavior or has the
highest-value untested logic; do not create one test project per source project
without evidence that the repository wants that layout.
Foo.TestsFoo.UnitTestsMatch, in order:
- The user's explicit framework choice.
- Existing test projects in the same repository.
- Repository-wide package/SDK conventions.
- A standard SDK template only when the repository provides no convention.
Never mix frameworks in one test project. Never add package versions directly
when central package management supplies them.
默认每个生产项目对应一个测试项目,命名遵循仓库约定(如、等)。对于模糊的多项目请求,从拥有用户可见行为或具有最高价值未测试逻辑的项目开始;不要在没有证据表明仓库需要该布局的情况下,为每个源项目创建一个测试项目。
Foo.TestsFoo.UnitTests按以下优先级匹配:
- 用户明确指定的框架选择。
- 仓库中已有的测试项目。
- 仓库范围内的包/SDK约定。
- 仅当仓库未提供任何约定时,才使用标准SDK模板。
切勿在一个测试项目中混合使用多种框架。当中央包管理提供包版本时,切勿直接添加包版本。
Step 3: Scaffold and reference the target
步骤3:搭建并关联目标项目
Use the matching template (, , or ) rather than
hand-writing template boilerplate. Then make only the repository-specific edits:
dotnet newxunitnunitmstest- Align target framework, nullable, implicit-usings, runner, and package style.
- Add a to each production project directly exercised by the planned tests. Do not reference every project in the solution.
ProjectReference - Remove template sample tests that do not test repository behavior.
For xUnit v3 projects that the repository runs through , preserve or
add both:
dotnet testxml
<OutputType>Exe</OutputType>
<TestingPlatformDotnetTestSupport>true</TestingPlatformDotnetTestSupport>OutputType=Exedotnet rundotnet testPrefer for project
references. Inspect the resulting project file before continuing.
dotnet add <test-project> reference <production-project>使用匹配的模板(、或),而非手动编写模板样板代码。然后仅进行仓库特定的编辑:
dotnet newxunitnunitmstest- 对齐目标框架、可空类型、隐式using、运行器和包风格。
- 添加到每个计划测试直接调用的生产项目。不要引用解决方案中的所有项目。
ProjectReference - 删除与仓库行为无关的模板示例测试。
对于仓库通过运行的xUnit v3项目,保留或添加以下两项配置:
dotnet testxml
<OutputType>Exe</OutputType>
<TestingPlatformDotnetTestSupport>true</TestingPlatformDotnetTestSupport>仅设置可使自托管运行器配合工作;但这并不代表仓库的命令能够发现测试。
OutputType=Exedotnet rundotnet test优先使用来添加项目引用。继续操作前请检查生成的项目文件。
dotnet add <test-project> reference <production-project>Step 4: Register with the real build entry point
步骤4:注册至实际构建入口点
Creating a is not enough. Add it to the exact solution artifact used by
the repository:
.csproj- or
.sln:.slnxdotnet sln <solution> add <test-project> - : add the project to the underlying solution and include it in the filter used by the requested/CI test command.
.slnf - No solution artifact: keep the existing project-oriented workflow. Do not create a solution solely for aesthetics unless the user asked for one.
Do not substitute a different solution file because it is easier to edit.
仅创建是不够的。需将其添加至仓库使用的具体解决方案工件中:
.csproj- 或
.sln:执行.slnxdotnet sln <solution> add <test-project> - :将项目添加至底层解决方案,并包含在请求/CI测试命令使用的筛选器中。
.slnf - 无解决方案工件:保留现有的面向项目的工作流程。除非用户要求,否则不要仅为了美观而创建解决方案。
不要因为编辑更简单就替换为其他解决方案文件。
Step 5: Add a real smoke test
步骤5:添加真实的冒烟测试
Replace template examples with the smallest smoke suite requested. The tests
must:
- instantiate or invoke a real symbol from the referenced production project;
- assert a concrete result, not only non-null/truthiness;
- avoid network, wall-clock, process, and real filesystem dependencies.
These tests prove the project reference and discovery path. Stop after every
behavior explicitly named by the user is covered; extra boundary permutations
are out of scope here and belong to .
code-testing-agent用用户要求的最小冒烟测试套件替换模板示例。测试必须满足:
- 实例化或调用引用生产项目中的真实符号;
- 断言具体结果,而非仅断言非空/真值;
- 避免依赖网络、时钟、进程和真实文件系统。
这些测试用于验证项目引用和发现路径。在覆盖用户明确指定的所有行为后停止;额外的边界排列超出了本技能的范围,属于的职责。
code-testing-agentStep 6: Verify direct and harness-level execution
步骤6:验证直接执行和 harness 级执行
Run, in this order:
- to isolate scaffolding failures.
dotnet test <test-project> - The repository's solution/root test command to prove CI discovery.
- A solution/project listing command to confirm the new project is registered.
If the direct command passes but the harness-level command discovers no new
test, the scaffolding is incomplete. Fix registration before reporting success.
Do not claim success from alone.
dotnet build按以下顺序运行:
- :隔离搭建过程中的失败。
dotnet test <test-project> - 仓库的解决方案/根测试命令:验证CI能够发现测试。
- 解决方案/项目列表命令:确认新项目已注册。
如果直接命令通过,但harness级命令未发现新测试,则搭建不完整。在报告成功前修复注册问题。不要仅通过就声称成功。
dotnet buildOutput Contract
输出约定
Report a compact table:
| Requirement | Evidence |
|---|---|
| Test project created/reused | Project path |
| Production reference | Referenced |
| Build registration | |
| Test discovery | Passing harness-level command and discovered test |
If validation is blocked, report the exact failing command and first actionable
error. Do not describe an unrun command as successful.
报告一个简洁的表格:
| 要求 | 证据 |
|---|---|
| 测试项目已创建/复用 | 项目路径 |
| 生产项目引用 | 被引用的 |
| 构建注册 | |
| 测试发现 | 通过的harness级命令及发现的测试 |
如果验证被阻塞,报告确切的失败命令和第一个可解决的错误。不要将未运行的命令描述为成功。
Validation
验证清单
- A suitable existing test project was ruled out before creating another.
- Framework, runner, target framework, and package style match the repository.
- Project references cover only the production projects under test.
- Central package management was preserved.
- Template sample tests were removed.
- At least one real deterministic test asserts a concrete behavior.
- The test project passes directly.
- The repository's solution/root command discovers and runs the new test.
- 在创建新项目之前,已排除存在合适的现有测试项目。
- 框架、运行器、目标框架和包风格与仓库一致。
- 项目引用仅覆盖被测生产项目。
- 保留了中央包管理。
- 已删除模板示例测试。
- 至少有一个真实的确定性测试断言了具体行为。
- 测试项目直接运行通过。
- 仓库的解决方案/根命令能够发现并运行新测试。
Common Pitfalls
常见陷阱
| Pitfall | Corrective action |
|---|---|
| Creating a project that CI never sees | Register it with the exact solution/filter used by CI and run that command |
| Picking a favorite framework | Infer the repository convention before using a default |
| Adding package versions under CPM | Add versionless references and keep versions in |
| Referencing the whole solution | Reference only projects whose APIs the tests compile against |
Keeping | Replace it with a concrete test of repository behavior |
| Creating parallel unit/integration projects from a vague ask | Start with one bounded project; expand only for a demonstrated boundary |
| Treating a green build as test discovery | Run the harness-level test command and observe the test |
| 陷阱 | 纠正措施 |
|---|---|
| 创建的项目从未被CI识别 | 将其注册至CI使用的具体解决方案/筛选器,并运行该命令 |
| 选择偏好的框架 | 在使用默认框架之前,先推断仓库的约定 |
| 在CPM下添加包版本 | 添加无版本引用,将版本保留在 |
| 引用整个解决方案 | 仅引用测试编译时需要其API的项目 |
保留 | 将其替换为针对仓库行为的具体测试 |
| 因模糊请求创建并行的单元/集成项目 | 从一个限定范围的项目开始;仅在有明确边界需求时扩展 |
| 将构建成功视为测试已被发现 | 运行harness级测试命令并观察测试结果 |