7

I know it is possible to exclude the whole test project(s) from Live Unit Testing by right clicking on the test project and selecting the "Live Unit Testing" context menu.

But in my solution I have some long running/resource intensive tests, which I would like to exclude. Is it possible to exclude individual tests?

Alex M
  • 2,410
  • 1
  • 24
  • 37
  • By definition Unit tests should test a small independent code fragment. Therefore should be very fast. You maybe misusing unit tests... – Major Nov 22 '17 at 19:33
  • 3
    Thank you for pointing, but the question was on how to exclude the individual test. – Alex M Nov 22 '17 at 21:10

2 Answers2

23

Easiest method is right clicking on the method in the editor view and selecting Live Unit Testing and Exclude. Menu as seen in Visual Studio 2017

You can also do it programatically with attributes.

For xUnit: [Trait("Category", "SkipWhenLiveUnitTesting")]

For NUnit: [Category("SkipWhenLiveUnitTesting")]

For MSTest: [TestCategory("SkipWhenLiveUnitTesting")]

more info at Microsoft docs

adsamcik
  • 1,265
  • 12
  • 14
  • Does the live unit testing recognize "SkipWhenLiveUnitTesting" as a special text that excludes it automatically, or is that a text you use to exclude them by hand? – Zorgarath Sep 26 '20 at 21:53
  • Update: it does appear to automagically exclude the tests :) – Zorgarath Sep 26 '20 at 23:10
3

Adding onto adsamcik's answer, here's how you can exclude an entire project (like an integration tests project) programmatically:

via .NET Core's csproj file:

// 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>

or via assemblyinfo.cs:

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

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

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

I figured out how to add the assembly attribute in .net core's csproj file via this SO answer. The attributes came from MS's documentation.

Zorgarath
  • 979
  • 12
  • 23