mtp-hot-reload
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseMTP Hot Reload for Iterative Test Fixing
利用MTP Hot Reload迭代修复测试
Set up and use Microsoft Testing Platform hot reload to rapidly iterate fixes on failing tests without rebuilding between each change.
设置并使用Microsoft Testing Platform hot reload功能,无需在每次变更后重新构建,即可快速迭代修复失败的测试。
When to Use
适用场景
- User has one or more failing tests and wants to iterate fixes quickly
- User wants to avoid rebuild overhead while fixing test code or production code
- User asks about hot reload for tests or speeding up the test-fix loop
- User needs to set up MTP hot reload in their project
- 用户存在一个或多个失败的测试,希望快速迭代修复
- 用户希望在修复测试代码或生产代码时避免重新构建的开销
- 用户询问测试的hot reload或加速测试-修复循环的方法
- 用户需要在项目中设置MTP hot reload
When Not to Use
不适用场景
- User needs to write new tests from scratch (use general coding assistance)
- User needs to diagnose why a test is failing (use diagnostic skills)
- User wants Visual Studio Test Explorer hot reload (different feature, built into VS)
- Project uses VSTest — hot reload requires Microsoft Testing Platform (MTP)
- User needs CI/CD pipeline configuration
- 用户需要从头编写新测试(使用通用编码辅助功能)
- 用户需要诊断测试失败的原因(使用诊断类技能)
- 用户想要Visual Studio Test Explorer hot reload(这是VS内置的不同功能)
- 项目使用VSTest — hot reload需要Microsoft Testing Platform (MTP)
- 用户需要CI/CD管道配置
Inputs
输入参数
| Input | Required | Description |
|---|---|---|
| Test project path | No | Path to the test project (.csproj). Defaults to current directory. |
| Failing test name or filter | No | Specific test(s) to iterate on |
| 输入项 | 是否必填 | 描述 |
|---|---|---|
| 测试项目路径 | 否 | 测试项目(.csproj)的路径。默认值为当前目录。 |
| 失败测试名称或筛选器 | 否 | 要迭代处理的特定测试 |
Workflow
工作流
Step 1: Verify the project uses Microsoft Testing Platform
步骤1:验证项目是否使用Microsoft Testing Platform
Hot reload requires MTP. It does not work with VSTest.
Check the same detection signals as the skill:
run-tests- SDK 10+: →
global.json"test": { "runner": "Microsoft.Testing.Platform" } - SDK 8/9: in
<TestingPlatformDotnetTestSupport>true</TestingPlatformDotnetTestSupport>,.csproj, orDirectory.Build.propsDirectory.Packages.props - Other signals: ,
<Sdk Name="MSTest.Sdk">, TUnit package reference<EnableMSTestRunner>true</EnableMSTestRunner>
If the project uses VSTest, inform the user that MTP hot reload is not available and suggest migrating to MTP first, or using Visual Studio's built-in Test Explorer hot reload feature instead.
Hot reload需要MTP,不兼容VSTest。
检查与技能相同的检测信号:
run-tests- SDK 10+:→
global.json"test": { "runner": "Microsoft.Testing.Platform" } - SDK 8/9:在
<TestingPlatformDotnetTestSupport>true</TestingPlatformDotnetTestSupport>、.csproj或Directory.Build.props中Directory.Packages.props - 其他信号:、
<Sdk Name="MSTest.Sdk">、TUnit包引用<EnableMSTestRunner>true</EnableMSTestRunner>
如果项目使用VSTest,告知用户MTP hot reload不可用,建议先迁移到MTP,或改用Visual Studio内置的Test Explorer hot reload功能。
Step 2: Add the hot reload NuGet package
步骤2:安装hot reload NuGet包
Install the package:
Microsoft.Testing.Extensions.HotReloadshell
dotnet add <project-path> package Microsoft.Testing.Extensions.HotReloadNote: When using(included transitively by MSTest, NUnit, and xUnit runners), the extension is auto-registered when you install its NuGet package — no code changes needed.Microsoft.Testing.Platform.MSBuild
安装 包:
Microsoft.Testing.Extensions.HotReloadshell
dotnet add <project-path> package Microsoft.Testing.Extensions.HotReload注意:当使用(由MSTest、NUnit和xUnit runner自动引入)时,安装其NuGet包后会自动注册该扩展——无需修改代码。Microsoft.Testing.Platform.MSBuild
Step 3: Enable hot reload
步骤3:启用hot reload
Hot reload is activated by setting the environment variable to .
TESTINGPLATFORM_HOTRELOAD_ENABLED1Option A — Set it in the shell before running tests:
shell
undefinedHot reload通过设置环境变量 为 来激活。
TESTINGPLATFORM_HOTRELOAD_ENABLED1选项A — 运行测试前在Shell中设置:
shell
undefinedPowerShell
PowerShell
$env:TESTINGPLATFORM_HOTRELOAD_ENABLED = "1"
$env:TESTINGPLATFORM_HOTRELOAD_ENABLED = "1"
bash/zsh
bash/zsh
export TESTINGPLATFORM_HOTRELOAD_ENABLED=1
**Option B — Add it to `launchSettings.json` (recommended for repeatable use):**
Create or update `Properties/launchSettings.json` in the test project:
```json
{
"profiles": {
"<ProjectName>": {
"commandName": "Project",
"environmentVariables": {
"TESTINGPLATFORM_HOTRELOAD_ENABLED": "1"
}
}
}
}export TESTINGPLATFORM_HOTRELOAD_ENABLED=1
**选项B — 添加到`launchSettings.json`(推荐用于重复使用):**
在测试项目中创建或更新 `Properties/launchSettings.json`:
```json
{
"profiles": {
"<ProjectName>": {
"commandName": "Project",
"environmentVariables": {
"TESTINGPLATFORM_HOTRELOAD_ENABLED": "1"
}
}
}
}Step 4: Run the tests with hot reload
步骤4:使用hot reload运行测试
Run the test project directly (not through ) to use hot reload in console mode:
dotnet testshell
dotnet run --project <project-path>To filter to specific failing tests, pass the filter after :
--| Framework | Filter syntax |
|---|---|
| MSTest | |
| NUnit | |
| xUnit v3 | |
| TUnit | |
The test host will start, run the tests, and remain running waiting for code changes.
直接运行测试项目(而非通过 )以在控制台模式下使用hot reload:
dotnet testshell
dotnet run --project <project-path>要筛选特定的失败测试,在 后传递筛选条件:
--| 测试框架 | 筛选语法 |
|---|---|
| MSTest | |
| NUnit | |
| xUnit v3 | |
| TUnit | |
测试主机将启动、运行测试,并保持运行状态等待代码变更。
Step 5: Iterate on the fix
步骤5:迭代修复
- Edit the source code (test code or production code) in your editor
- The test host detects the changes and re-runs the affected tests automatically
- Review the updated results in the console
- Repeat until all targeted tests pass
Important: Hot reload currently works in console mode only. There is no support for hot reload in Test Explorer for Visual Studio or Visual Studio Code.
- 在编辑器中修改源代码(测试代码或生产代码)
- 测试主机会检测到变更并自动重新运行受影响的测试
- 在控制台中查看更新后的结果
- 重复上述步骤直到所有目标测试通过
重要提示:Hot reload目前仅支持控制台模式。不支持在Visual Studio或Visual Studio Code的Test Explorer中使用。
Step 6: Finalize
步骤6:最终确认
Once all tests pass:
- Stop the test host (Ctrl+C)
- Run a full to confirm all tests pass with a clean build
dotnet test - Optionally remove from the environment or keep
TESTINGPLATFORM_HOTRELOAD_ENABLEDfor future uselaunchSettings.json
所有测试通过后:
- 停止测试主机(Ctrl+C)
- 运行完整的 以确认所有测试在干净构建后仍能通过
dotnet test - 可选择从环境中移除 ,或保留
TESTINGPLATFORM_HOTRELOAD_ENABLED以备将来使用launchSettings.json
Validation
验证清单
- Project uses Microsoft Testing Platform (not VSTest)
- package is installed
Microsoft.Testing.Extensions.HotReload - environment variable is set to
TESTINGPLATFORM_HOTRELOAD_ENABLED1 - Tests run and the host remains active waiting for changes
- Code changes are picked up without manual restart
- 项目使用Microsoft Testing Platform(而非VSTest)
- 包已安装
Microsoft.Testing.Extensions.HotReload - 环境变量 已设置为
TESTINGPLATFORM_HOTRELOAD_ENABLED1 - 测试运行后主机保持活跃状态等待变更
- 代码变更无需手动重启即可被识别
Common Pitfalls
常见陷阱
| Pitfall | Solution |
|---|---|
Using | Hot reload requires |
| Project uses VSTest, not MTP | Hot reload requires MTP. Migrate to MTP first or use VS Test Explorer hot reload |
| Forgetting to set the environment variable | Set |
| Expecting Test Explorer integration | Console mode only — no VS/VS Code Test Explorer support |
| Making unsupported code changes (rude edits) | Some changes (adding new types, changing method signatures) require a restart. Stop and re-run |
| 陷阱 | 解决方案 |
|---|---|
使用 | Hot reload需要使用 |
| 项目使用VSTest而非MTP | Hot reload需要MTP。先迁移到MTP,或使用VS Test Explorer hot reload |
| 忘记设置环境变量 | 运行前设置 |
| 期望与Test Explorer集成 | 仅支持控制台模式——不支持VS/VS Code Test Explorer |
| 进行不支持的代码变更(粗暴编辑) | 某些变更(添加新类型、修改方法签名)需要重启。停止并重新运行测试主机 |