How to Ignore in MSTests

Viewed 2324

I'm sorry this seems like such a basic question but I can't find the answer anywhere, including the MS Docs which talk about it but don't give an actual example.

I just want to ignore some tests. Here are some things that don't seem to work:

[TestMethod]
[Ignore]
public void TestStartAcquireEmpty()
{

}

[TestMethod]
[IgnoreAttribute]
public void TestStartAcquireEmpty()
{

}

[Ignore]
[TestMethod]
public void TestStartAcquireEmpty()
{

}

[IgnoreAttribute]
[TestMethod]
public void TestStartAcquireEmpty()
{

}

If I use [Ignore] without [TestMethod] the test does disappear from the test explorer. But what I want is to get the yellow triangle in the test explorer.

1 Answers

To see the yellow triangle for a test it is necessary to run the test first.
The triangle appears then in the test explorer when a test method has the [Ignore] attribute or asserts with Assert.Inconclusive().


The MSTest Test Explorer is a unit test runner with the capability to discover test methods automatically (and a few more features).

Use Test Explorer to run unit tests from Visual Studio or third-party unit test projects. You can also use Test Explorer to group tests into categories, filter the test list, and create, save, and run playlists of tests.
https://docs.microsoft.com/de-de/visualstudio/test/run-unit-tests-with-test-explorer?view=vs-2019

Test methods are discovered when the are within a public class with the [TestClass] attribute and have the [TestMethod] attribute. They need to be public with return type void.
Discovered test are shown in the test explorer with a blue exclamation mark icon that symbols that the test didn't ran by now (If you deactivate the real time test discovery you need to compile the test project first to see the test methods in the test explorer).

If a test method has the [Ignore] attribute there is metadata added at compile time. That metadata is examined at runtime like most other attributes (see https://docs.microsoft.com/en-us/dotnet/api/system.attribute?view=netcore-3.1#remarks).

Therefore the attribute is examined at runtime it's necessary to run the test first to see the outcome in the test explorer.


If you want to see the outcome of test immediately you may try Visual Studio Live Unit Testing:
https://docs.microsoft.com/de-de/visualstudio/test/live-unit-testing?view=vs-2019

Related