Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Skip the SetUp method only for a particular test in NUnit?

I have a test where I do not need to run the SetUp method (attributed with [SetUp]) before running the test. I need the SetUp method to be run for other tests.

Is there a different attribute that can be used or a non-attribute-based way to achieve this?

like image 363
Naresh Avatar asked Jul 28 '09 13:07

Naresh


People also ask

How do I skip a NUnit test?

IgnoreAttribute (NUnit 2.0) 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.

Does SetUp run before every test NUnit?

In unit testing frameworks, Setup is called before each and every unit test within your test suite.

What is SetUp method in NUnit?

This attribute is used inside a TestFixture to provide a common set of functions that are performed just before each test method is called. SetUp methods may be either static or instance methods and you may define more than one of them in a fixture.

What is SetUp and teardown in NUnit?

Setup methods (both types) are called on base classes first, then on derived classes. If any setup method throws an exception, no further setups are called. Teardown methods (again, both types) are called on derived classes first, then on the base class.


2 Answers

You could also add a category and inspect the category list in your setup:

public const string SKIP_SETUP = "SkipSetup"; 

[SetUp]
public void Setup(){
   if (!CheckForSkipSetup()){
        // Do Setup stuff
   }
}

private static bool CheckForSkipSetup() {
    ArrayList categories = TestContext.CurrentContext.Test
       .Properties["_CATEGORIES"] as ArrayList;

    bool skipSetup = categories != null && categories.Contains( SKIP_SETUP );
    return skipSetup ;
}

[Test]
[Category(SKIP_SETUP)]
public void SomeTest(){
    // your test code
}
like image 133
Mike Parkhill Avatar answered Sep 30 '22 17:09

Mike Parkhill


You should create a new class for that test which has only the setup (or lack of setup) that it needs.

Alternatively, you could unfactor the setup code into a method that all the other tests call, but I don't recommend this approach.

like image 35
Michael Haren Avatar answered Sep 30 '22 17:09

Michael Haren