Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ignore IgnoreAttribute

We have MSTest tests which automatically run in hourly production. One of these tests is marked with [Ignore] attribute because it is not yet ready to run it in our production environment.
Now I want to start that test (only) on my local environment because my local environment is ready for that test.
I try to run that test by clicking on ReSharper's icon on the left side of test code or by clicking Run Selected Tests icon in Unit Test Sessions window and nothing happens.

I fix it currently by commenting out the [Ignore] line. But now I need to be aware to remove the comment characters (//) before checking-in the code.

Is there another way to temporarly run an [Ignore]'d test ?

like image 456
brgerner Avatar asked Apr 12 '12 08:04

brgerner


People also ask

How to ignore a unit test c#?

When you add the [Ignore] attribute, the test will be ignored by the test runner. It will show up in Test Explorer with a warning icon and will count as Skipped.

How do I use ignore attribute?

The ignore attribute is an attribute to not run a test or test fixture for a period of time. The person marks either a Test or a TestFixture with the Ignore Attribute. The running program sees the attribute and does not run the test or tests.

How do I ignore test class in Nunit?

By not applying the attribute you're ignoring the fact that there are test methods within this class that aren't being tested for a reason. You should use [TestFixture, Ignore("reason")] to supply the output of the test results with a reason why they're ignored.


3 Answers

Recently when I have encountered problems such as this, I add a new Build Configuration to the visual studio project named something such as "Local Developer Debug" and use the settings from the existing Debug configuration. Then I go to "Project -> MyProjectName Properties -> Build", make sure "Local Developer Debug" is the selected configuration and add "LOCALDEVBUILD" to "Conditional compliation symbols". This allows for the use of preprocessor directives to 'toggle' code at compile time:

#if (!LOCALDEVBUILD)
    [Ignore]
#endif

Not sure if this is what you're looking for... but it allows you to run/utilize specific code depending on the intentions of the build (via the build configuration)... With this method you can leave the test ignored for more 'official' builds, but still execute it at your leisure if you so desire.

like image 187
Skonen Avatar answered Oct 16 '22 12:10

Skonen


You could also use a test category to mark the test methods you do not want to be included in your automated test, e.g.

[TestCategory("IgnoreOnBuild")]

Thus not using the [Ignore] attribute. And combine this with a filter in your build definition, under Basic -> Automated Test -> Test Source -> Test Case Filter:

TestCategory!=IgnoreOnBuild
like image 20
Lasse Normann Avatar answered Oct 16 '22 13:10

Lasse Normann


It looks like there are other ways to enable/disable tests using the testrunconfig file. However, if you use the IgnoreAttribute it compiles in with the code and therefore cannot be easily toggled.

From the documentation:

The Ignore attribute for a unit test resides in the source file of the unit test, together with the other attributes and source code of the test. This means that, if you disable a unit test in the Test Manager window, and later run the test by using the MSTest.exe command-line utility, the test still runs. But if you mark a unit test with the Ignore attribute, compile the assembly, and then run the test by using MSTest.exe, the test does not run. It does not run because the Ignore attribute has become part of the test.

like image 3
Davin Tryon Avatar answered Oct 16 '22 12:10

Davin Tryon