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?
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.
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.
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.
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.
Easiest method is right clicking on the method in the editor view and selecting Live Unit Testing and Exclude.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With