Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to exclude certain tests in the Visual Studio Test Runner?

I have attributes on certain tests that I ideally don't want to run on every build. Most of my tests are normal unit tests and I do want them to run on every build.

So: how can I exclude a test by category or project type?

For example, I'd like to exclude CodedUItests:

[CodedUITest] public class SearchViewTests 

...or exclude tests in a given TestCategory:

[TestMethod] [TestCategory("Database Integration")] public void ContactRepositoryGetByIdWithIdExpectCorrectContact() 

I particularly want to exclude the coded UI tests as they disrupt my ability to continue working, whereas all the other tests will happily run in the background without disturbing me.

Originally this question was about Visual Studio 2012, so I'd prefer solutions that work in that version and higher.

like image 637
Fenton Avatar asked Sep 25 '12 11:09

Fenton


People also ask

How do I ignore a test in Visual Studio?

If you want to enable or disable unit tests in Microsoft Visual Studio 2010, you can use the “Ignore” attribute. This can be used to ignore ore exclude integration tests in a nightly continuous integration build.

How do you exclude a project from test coverage?

To exclude test code from the code coverage results and only include application code, add the ExcludeFromCodeCoverageAttribute attribute to your test class. To include assemblies that aren't part of your solution, obtain the . pdb files for these assemblies and copy them into the same folder as the assembly .

How do I run a specific test in Visual Studio?

If Test Explorer is not visible, choose Test on the Visual Studio menu, choose Windows, and then choose Test Explorer (or press Ctrl + E, T). As you run, write, and rerun your tests, the Test Explorer displays the results in a default grouping of Project, Namespace, and Class.


2 Answers

TL;DR version:

Test explorer showing -Trait:"CategoryName" filter

Other answers have commented on workarounds and use of the more recent Traits options. However, none quite tell you how to specifically exclude tests for a trait. To do so, simply use a - (minus) to negate a filter in the search box, e.g.:

-Trait:"DatabaseIntegration" 

This will exclude all tests with that trait. The MSDN documentation on these features has the following explanation:

To exclude a subset of the results of a filter, use the following syntax:

FilterName:"Criteria" -FilterName:"SubsetCriteria"

For example,

FullName:"MyClass" - FullName:"PerfTest"

returns all tests that include "MyClass" in their name except those tests that also include "PerfTest" in their name.

like image 127
Jeroen Avatar answered Sep 19 '22 01:09

Jeroen


The only "solution" (or better workaround) I found to work is to specify a "FullName" filter. Basically I usually structure my solution like

  • ProjectA
  • ProjectA.UnitTests
  • ProjectA.IntegrationTests

and so on. Now I can specify a filter in the Test Explorer like FullName: "UnitTests" which seems to work.
(I'd expected to be able to use a regex within the search name but it doesn't seem to be supported.)

enter image description here

like image 21
Juri Avatar answered Sep 21 '22 01:09

Juri