6

我知道可以通过右键单击测试项目并选择“Live Unit Testing”上下文菜单来从 Live Unit Testing 中排除整个测试项目。

但在我的解决方案中,我有一些长时间运行/资源密集型测试,我想排除这些测试。是否可以排除个别测试?

4

2 回答 2

22

最简单的方法是右键单击编辑器视图中的方法并选择 Live Unit Testing and Exclude。Visual Studio 2017 中的菜单

您也可以使用属性以编程方式执行此操作。

对于 xUnit:[Trait("Category", "SkipWhenLiveUnitTesting")]

对于 NUnit:[Category("SkipWhenLiveUnitTesting")]

对于 MSTest:[TestCategory("SkipWhenLiveUnitTesting")]

Microsoft 文档中的更多信息

于 2017-11-07T11:59:48.283 回答
2

添加到 adsamcik 的答案中,您可以通过以下方式以编程方式排除整个项目(如集成测试项目):

通过 .NET Core 的 csproj 文件:

// xunit
  <ItemGroup>
    <AssemblyAttribute Include="Xunit.AssemblyTrait">
      <_Parameter1>Category</_Parameter1>
      <_Parameter2>SkipWhenLiveUnitTesting</_Parameter2>
    </AssemblyAttribute>
  </ItemGroup>

// nunit
  <ItemGroup>
    <AssemblyAttribute Include="Nunit.Category">
      <_Parameter1>SkipWhenLiveUnitTesting</_Parameter1>
    </AssemblyAttribute>
  </ItemGroup>

// mstest
  <ItemGroup>
    <AssemblyAttribute Include="MSTest.TestCategory">
      <_Parameter1>SkipWhenLiveUnitTesting</_Parameter1>
    </AssemblyAttribute>
  </ItemGroup>

或通过 assemblyinfo.cs:

// xunit
[assembly: AssemblyTrait("Category", "SkipWhenLiveUnitTesting")] 

// nunit
[assembly: Category("SkipWhenLiveUnitTesting")]

// mstest
[assembly: TestCategory("SkipWhenLiveUnitTesting")]

我想出了如何通过这个 SO answer在 .net core 的 csproj 文件中添加程序集属性。这些属性来自MS 的文档

于 2020-09-27T02:03:36.850 回答