mtp-hot-reload

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

MTP 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

输入参数

InputRequiredDescription
Test project pathNoPath to the test project (.csproj). Defaults to current directory.
Failing test name or filterNoSpecific 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
run-tests
skill:
  • SDK 10+:
    global.json
    "test": { "runner": "Microsoft.Testing.Platform" }
  • SDK 8/9:
    <TestingPlatformDotnetTestSupport>true</TestingPlatformDotnetTestSupport>
    in
    .csproj
    ,
    Directory.Build.props
    , or
    Directory.Packages.props
  • Other signals:
    <Sdk Name="MSTest.Sdk">
    ,
    <EnableMSTestRunner>true</EnableMSTestRunner>
    , TUnit package reference
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">
    <EnableMSTestRunner>true</EnableMSTestRunner>
    、TUnit包引用
如果项目使用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
Microsoft.Testing.Extensions.HotReload
package:
shell
dotnet add <project-path> package Microsoft.Testing.Extensions.HotReload
Note: When using
Microsoft.Testing.Platform.MSBuild
(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.Extensions.HotReload
包:
shell
dotnet add <project-path> package Microsoft.Testing.Extensions.HotReload
注意:当使用
Microsoft.Testing.Platform.MSBuild
(由MSTest、NUnit和xUnit runner自动引入)时,安装其NuGet包后会自动注册该扩展——无需修改代码。

Step 3: Enable hot reload

步骤3:启用hot reload

Hot reload is activated by setting the
TESTINGPLATFORM_HOTRELOAD_ENABLED
environment variable to
1
.
Option A — Set it in the shell before running tests:
shell
undefined
Hot reload通过设置环境变量
TESTINGPLATFORM_HOTRELOAD_ENABLED
1
来激活。
选项A — 运行测试前在Shell中设置:
shell
undefined

PowerShell

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
dotnet test
) to use hot reload in console mode:
shell
dotnet run --project <project-path>
To filter to specific failing tests, pass the filter after
--
:
FrameworkFilter syntax
MSTest
dotnet run --project <path> -- --filter "FullyQualifiedName~TestMethodName"
NUnit
dotnet run --project <path> -- --filter "FullyQualifiedName~TestMethodName"
xUnit v3
dotnet run --project <path> -- --filter-method "*TestMethodName"
TUnit
dotnet run --project <path> -- --treenode-filter "/*/*/ClassName/TestMethodName"
The test host will start, run the tests, and remain running waiting for code changes.
直接运行测试项目(而非通过
dotnet test
)以在控制台模式下使用hot reload:
shell
dotnet run --project <project-path>
要筛选特定的失败测试,在
--
后传递筛选条件:
测试框架筛选语法
MSTest
dotnet run --project <path> -- --filter "FullyQualifiedName~TestMethodName"
NUnit
dotnet run --project <path> -- --filter "FullyQualifiedName~TestMethodName"
xUnit v3
dotnet run --project <path> -- --filter-method "*TestMethodName"
TUnit
dotnet run --project <path> -- --treenode-filter "/*/*/ClassName/TestMethodName"
测试主机将启动、运行测试,并保持运行状态等待代码变更。

Step 5: Iterate on the fix

步骤5:迭代修复

  1. Edit the source code (test code or production code) in your editor
  2. The test host detects the changes and re-runs the affected tests automatically
  3. Review the updated results in the console
  4. 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.
  1. 在编辑器中修改源代码(测试代码或生产代码)
  2. 测试主机会检测到变更并自动重新运行受影响的测试
  3. 在控制台中查看更新后的结果
  4. 重复上述步骤直到所有目标测试通过
重要提示:Hot reload目前仅支持控制台模式。不支持在Visual Studio或Visual Studio Code的Test Explorer中使用。

Step 6: Finalize

步骤6:最终确认

Once all tests pass:
  1. Stop the test host (Ctrl+C)
  2. Run a full
    dotnet test
    to confirm all tests pass with a clean build
  3. Optionally remove
    TESTINGPLATFORM_HOTRELOAD_ENABLED
    from the environment or keep
    launchSettings.json
    for future use
所有测试通过后:
  1. 停止测试主机(Ctrl+C)
  2. 运行完整的
    dotnet test
    以确认所有测试在干净构建后仍能通过
  3. 可选择从环境中移除
    TESTINGPLATFORM_HOTRELOAD_ENABLED
    ,或保留
    launchSettings.json
    以备将来使用

Validation

验证清单

  • Project uses Microsoft Testing Platform (not VSTest)
  • Microsoft.Testing.Extensions.HotReload
    package is installed
  • TESTINGPLATFORM_HOTRELOAD_ENABLED
    environment variable is set to
    1
  • 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_ENABLED
    已设置为
    1
  • 测试运行后主机保持活跃状态等待变更
  • 代码变更无需手动重启即可被识别

Common Pitfalls

常见陷阱

PitfallSolution
Using
dotnet test
instead of
dotnet run
Hot reload requires
dotnet run --project <path>
to run the test host directly in console mode
Project uses VSTest, not MTPHot reload requires MTP. Migrate to MTP first or use VS Test Explorer hot reload
Forgetting to set the environment variableSet
TESTINGPLATFORM_HOTRELOAD_ENABLED=1
before running
Expecting Test Explorer integrationConsole 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
陷阱解决方案
使用
dotnet test
而非
dotnet run
Hot reload需要使用
dotnet run --project <path>
直接在控制台模式下运行测试主机
项目使用VSTest而非MTPHot reload需要MTP。先迁移到MTP,或使用VS Test Explorer hot reload
忘记设置环境变量运行前设置
TESTINGPLATFORM_HOTRELOAD_ENABLED=1
期望与Test Explorer集成仅支持控制台模式——不支持VS/VS Code Test Explorer
进行不支持的代码变更(粗暴编辑)某些变更(添加新类型、修改方法签名)需要重启。停止并重新运行测试主机