【问题标题】:Live unit testing exclude tests实时单元测试排除测试
【发布时间】:2020-09-27 10:03:36
【问题描述】:

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

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

【问题讨论】:

  • 根据定义,单元测试应该测试一个小的独立代码片段。因此应该非常快。您可能会滥用单元测试...
  • 谢谢指点,但问题是如何排除个别测试。

标签: visual-studio-2017 live-unit-tests


【解决方案1】:

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

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

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

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

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

更多信息Microsoft docs

【讨论】:

  • 实时单元测试是否将“SkipWhenLiveUnitTesting”识别为自动排除它的特殊文本,还是您用来手动排除它们的文本?
  • 更新:它似乎会自动排除测试 :)
【解决方案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")]

我想出了如何通过this SO answer 在.net core 的csproj 文件中添加程序集属性。属性来自MS's documentation

【讨论】:

  • 对于 NUnit,我必须在 csproj 文件中使用 &lt;AssemblyAttribute Include="NUnit.Framework.CategoryAttribute"&gt;
猜你喜欢
  • 1970-01-01
  • 2011-08-23
  • 2017-07-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-16
  • 1970-01-01
相关资源
最近更新 更多