通过


运行和调试测试

系统会以可直接运行(或调试)的可执行文件的形式生成 Microsoft.Testing.Platform 测试项目。 没有额外用于运行测试的控制台或命令。 如果出现错误,则应用退出时会使用非零退出代码,这是大多数可执行文件的典型情况。 有关已知退出代码的详细信息,请参阅 Microsoft.Testing.Platform 退出代码

小窍门

可以使用命令行选项忽略特定的退出代码--ignore-exit-code

还可以使用 TestingPlatformCommandLineArguments MSBuild 属性设置应用于项目文件中特定测试项目的命令行选项。 一个常见的使用场景是测试项目将所有测试忽略掉,这通常会导致测试会话以退出码8退出(因为测试会话运行了零个测试)。 在这种情况下,可以在项目文件中的 PropertyGroup 下添加以下内容:

<TestingPlatformCommandLineArguments>$(TestingPlatformCommandLineArguments) --ignore-exit-code 8</TestingPlatformCommandLineArguments>

重要

默认情况下,Microsoft.Testing.Platform 会收集遥测数据。 有关选择退出的详细信息和选项,请参阅 Microsoft.Testing.Platform 遥测

使用 dotnet publish 发布测试项目并直接运行应用是运行测试的另一种方法。 例如,执行 ./Contoso.MyTests.exe。 在某些情况下,也可以使用 dotnet build 生成可执行文件,但可能需要考虑边缘情况,例如Native AOT

使用 dotnet run

dotnet run 命令可用于生成和运行测试项目。 这是运行测试的最简单方法,尽管有时速度最慢。 在本地编辑和运行测试时,使用 dotnet run 是可行的,因为它可确保在需要时重新生成测试项目。 dotnet run 还将在当前文件夹中自动查找项目。

dotnet run --project Contoso.MyTests

有关 dotnet run 的详细信息,请参阅 dotnet run

使用 dotnet exec

dotnet execdotnet 命令用于执行(或运行)已生成的测试项目,这是直接运行应用程序的替代方法。 dotnet exec 需要已生成的测试项目 dll 的路径。

dotnet exec Contoso.MyTests.dll

或者

dotnet Contoso.MyTests.dll

注释

提供测试项目可执行文件(*.exe)的路径导致错误:

Error:
  An assembly specified in the application dependencies manifest
  (Contoso.MyTests.deps.json) has already been found but with a different
  file extension:
    package: 'Contoso.MyTests', version: '1.0.0'
    path: 'Contoso.MyTests.dll'
    previously found assembly: 'S:\t\Contoso.MyTests\bin\Debug\net10.0\Contoso.MyTests.exe'

有关 dotnet exec 的详细信息,请参阅 dotnet exec

使用 dotnet test

Microsoft.Testing.Platform 提供包含 vstest.console.exedotnet test 的兼容性层,以确保可以在启用新执行方案时像以前一样运行测试。

dotnet test Contoso.MyTests.dll

另请参阅