Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Live unit testing exclude tests

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?

like image 855
Alex M Avatar asked Nov 07 '17 11:11

Alex M


People also ask

What do you have to avoid in tests in unit testing?

Avoid Test Interdependence You, therefore, cannot count on the test suite or the class that you're testing to maintain state in between tests. But that won't always make itself obvious to you. If you have two tests, for instance, the test runner may happen to execute them in the same order each time.

Can we skip unit testing?

Unit tests are powerful tools for developers, who can use them repeatedly to quickly and automatically fast check if the units of code work as they should. However, writing them takes time, and since you can deliver an application without unit tests, some entrepreneurs unwisely consider them unnecessary.

Should unit tests be isolated?

Embracing isolation when testing has those benefits and more: better code, faster development and more reliable releases. Developers are already isolating their code, even if it isn't something they worry about when running unit tests.

What is live unit testing?

Live Unit Testing executes your unit tests automatically and in real time as you make code changes. This lets you refactor and change code with greater confidence. Live Unit Testing automatically executes all impacted tests as you edit your code to ensure that your changes do not introduce regressions.


2 Answers

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

like image 188
adsamcik Avatar answered Nov 23 '22 17:11

adsamcik


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.

like image 33
Zorgarath Avatar answered Nov 23 '22 19:11

Zorgarath