活动
在 Microsoft.Testing.Platform 中使用 dotnet test
本文介绍如何在使用 dotnet test
时使用 Microsoft.Testing.Platform
运行测试,以及可用于配置通过 Microsoft.Testing.Platform 运行测试时生成的 MSBuild 输出的各种选项。
本文介绍如何使用 dotnet test
在使用 的解决方案 (Microsoft.Testing.Platform
) 中运行所有测试。
dotnet test 命令是一种从解决方案、项目或已生成的程序集运行测试的方法。 Microsoft.Testing.Platform 会连接到此基础结构,以提供统一的方法来运行测试,尤其是在从 VSTest 迁移到 Microsoft.Testing.Platform
时。
Microsoft.Testing.Platform
提供一个 兼容层 (VSTest Bridge) ,可以无缝使用 dotnet test
。
可以通过运行以下代码来运行测试:
dotnet test
此层通过 VSTest 运行测试,并将其与 VSTest 测试框架适配器级别集成。
默认情况下,dotnet test
使用 VSTest 行为来运行测试。 可以通过在项目文件中指定 <TestingPlatformDotnetTestSupport>true</TestingPlatformDotnetTestSupport>
设置,在 dotnet test
中启用对 Microsoft.Testing.Platform
的支持。
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
<OutputType>Exe</OutputType>
<EnableMSTestRunner>true</EnableMSTestRunner>
<!-- Add this to your project file. -->
<TestingPlatformDotnetTestSupport>true</TestingPlatformDotnetTestSupport>
</PropertyGroup>
<!-- ... -->
</Project>
备注
强烈建议在 Directory.Build.props
中设置 TestingPlatformDotnetTestSupport
属性。 这样,就不必将其添加到每个测试项目文件中,并且不会冒险引入未设置此属性的新项目,最终导致解决方案中某些项目是 VSTest,而另一些项目是 Microsoft.Testing.Platform,但这种情况下可能无法正常工作,并且不受支持。
重要
尽管 TestingPlatformDotnetTestSupport
设置为 true
,但 dotnet 测试 中定义的大多数命令行选项仍面向 VSTest,不会影响基于 Microsoft.Testing.Platform
的测试。 若要向 Microsoft.Testing.Platform
提供参数,需要使用在 dotnet test 中Microsoft.Testing.Platform 命令行参数所述的方法之一。
下表描述了 Microsoft.Testing.Platform
支持的所有 dotnet test
命令行选项:
-a|--arch <ARCHITECTURE>
--artifacts-path <ARTIFACTS_DIR>
-c|--configuration <CONFIGURATION>
-f|--framework <FRAMEWORK>
-e|--environment <NAME="VALUE">
--interactive
--no-build
--nologo
--no-restore
-o|--output <OUTPUT_DIRECTORY>
--os <OS>
-r|--runtime <RUNTIME_IDENTIFIER>
-v|--verbosity <LEVEL>
支持这些参数,因为它们链接到生成步骤,并且独立于所使用的测试平台。
可以通过以下方式之一提供用于调用测试应用程序的参数:
Microsoft.Testing.Platform
从版本 1.4(MSTest 版本 3.6 随附)开始,可以在命令行上的双划线--
后面添加选项:.NET CLIdotnet test -- --minimum-expected-tests 10
在命令行上使用
TestingPlatformCommandLineArguments
MSBuild 属性:.NET CLIdotnet test -p:TestingPlatformCommandLineArguments="--minimum-expected-tests 10"
或者在项目文件中:
XML<PropertyGroup> ... <TestingPlatformCommandLineArguments>--minimum-expected-tests 10</TestingPlatformCommandLineArguments> </PropertyGroup>
MSBuild 集成提供可在项目文件中或通过命令行上的全局属性指定的选项,例如 -p:TestingPlatformShowTestsFailure=true
。
这些可用选项为:
默认情况下,测试失败将汇总到 .log 文件中,每个测试项目的单个失败都会报告给 MSBuild。
要显示每个失败测试的错误,请在命令行中指定 -p:TestingPlatformShowTestsFailure=true
,或将 <TestingPlatformShowTestsFailure>true</TestingPlatformShowTestsFailure>
属性添加到项目文件中。
在命令行上:
dotnet test -p:TestingPlatformShowTestsFailure=true
或在项目文件中:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
<OutputType>Exe</OutputType>
<EnableMSTestRunner>true</EnableMSTestRunner>
<TestingPlatformDotnetTestSupport>true</TestingPlatformDotnetTestSupport>
<!-- Add this to your project file. -->
<TestingPlatformShowTestsFailure>true</TestingPlatformShowTestsFailure>
</PropertyGroup>
<!-- ... -->
</Project>
默认情况下,基础测试可执行文件写入的所有控制台输出都会被捕获并对用户隐藏。 其中包括横幅、版本信息和格式化测试信息。
若要将此信息与 MSBuild 输出一起显示,请使用 <TestingPlatformCaptureOutput>false</TestingPlatformCaptureOutput>
。
此选项不会影响测试框架捕获 Console.WriteLine
写入的用户输出的方式或写入控制台的其他类似方式。
在命令行上:
dotnet test -p:TestingPlatformCaptureOutput=false
或在项目文件中:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
<OutputType>Exe</OutputType>
<EnableMSTestRunner>true</EnableMSTestRunner>
<TestingPlatformDotnetTestSupport>true</TestingPlatformDotnetTestSupport>
<!-- Add this to your project file. -->
<TestingPlatformCaptureOutput>false</TestingPlatformCaptureOutput>
</PropertyGroup>
<!-- ... -->
</Project>
重要
上述所有示例在 csproj 文件中添加 EnableMSTestRunner
、TestingPlatformDotnetTestSupport
和 TestingPlatformCaptureOutput
等属性。 但是,强烈建议在 Directory.Build.props
中设置这些属性。 这样,就不必将其添加到每个测试项目文件中,并且不会冒险引入未设置这些属性的新项目,最终导致解决方案中某些项目是 VSTest,而另一些项目是 Microsoft.Testing.Platform,但这种情况下可能无法正常工作,并且不受支持。
其他资源
培训
模块
C#-testning i Visual Studio - Training
Börja testa dina C#-appar med hjälp av testverktygen i Visual Studio. Lär dig att skriva tester, använda Test Explorer, skapa testpaket och tillämpa det röda, gröna refaktormönstret för att skriva kod.