NUnit Test Attribute Execution Order

I went looking to find the order in which NUnit executes the test attributes/events.  Oddly enough, I couldn't find it.  (Maybe I'm not googling it properly.)

Anyway, here's the NUnit attribute event order:

  1. TestFixtureSetUp
  2. SetUp
  3. Test
  4. TearDown
  5. Repeat steps 2, 3, and 4 for each test that's being run in this fixture.
  6. TestFixtureTearDown

-Ben

posted @ Friday, June 23, 2006 12:59 PM

Print

Comments on this entry:

# re: NUnit Test Attribute Execution Order

Left by subdigital@gmail.com at 6/26/2006 7:36 AM
Gravatar
You might want to point out here that steps 2-4 are repeated for EVERY test.

That's something that a lot of people new to NUnit sometimes don't get.

# re: NUnit Test Attribute Execution Order

Left by Benjamin Day at 6/26/2006 7:48 AM
Gravatar
Excellent idea, Subdigital. (see the new step #5)

-Ben

# re: NUnit Test Attribute Execution Order

Left by TunaBomber at 7/12/2006 9:24 AM
Gravatar
Each test should be able to run alone and independent of whether the rest passed, failed, or even was run. Junit intentionally randomizes the tests. Nunit runs them alphabetically in the console app but I am not sure about the console command.

Ideally, you have your setup and teardown setup the environment needed for each test so you do not have to rely on the outcome of another test.

See this link...
http://discuss.develop.com/archives/wa.exe?A2=ind0308d&L=dotnet-cx&D=1&P=1382

# re: NUnit Test Attribute Execution Order

Left by PlunderBunny at 1/30/2007 5:20 AM
Gravatar
Here's an example of one instance where I need to rely on the test order - I adapted the example from the singleton generic posted on the code project (http://www.codeproject.com/cs/design/GenericSingletonPattern.asp) to make four unit tests for the singleton. Because the singleton object remains between the tests, they must run in the order they appear. If there was a way to delete the singleton object in a TearDown, this would solve the problem, but I'm not aware of any way to do this (disclaimer - I am a novice C# and .Net programmer).
It would be nice if we could specify the test order by numbering the test attributes, i.e. [Test1] ... [Test2] etc.

# re: NUnit Test Attribute Execution Order

Left by Benjamin Day at 1/30/2007 5:25 AM
Gravatar
Hey PlunderBunny,

It sounds like you'd like the "Ordered Test" in Visual Studio Team System's unit testing framework. As far as I know there isn't a way to specify the test execution order in NUnit.

In that singleton pattern implementation, there's almost definitely a variable somewhere that's marked as "static". That's really the key to the singleton pattern. If you set that variable to null, it will reset the singleton. That might help you out.

-Ben

# re: NUnit Test Attribute Execution Order

Left by Andy Morris at 7/5/2007 5:22 AM
Gravatar
NUnit executs Fixtures and tests in alphabetical order:

FixtureA
TestA
TestB
FixtureB
TestA
TestB

The Testdriven.NET add in runs them in the order that they are in the code.

This can catch you out if you have "Guard Tests" that check for missing resources etc before tests that would take a long time or produce a lot of spurious errors.

# re: NUnit Test Attribute Execution Order

Left by Kristofer Krause at 6/12/2008 4:23 PM
Gravatar
Correct Andy. NUnit executes test in alphabeticaly order. This is good to know. Sometimes I need to be able to "create a scenario" in one test... if it passes... have the next test "test deleting" it.

# re: NUnit Test Attribute Execution Order

Left by Pavel at 4/12/2009 9:48 PM
Gravatar
Well, TunaBomber, that's the theory. But in reality, the impact on productivity of that is just too much of a cost for keeping a purist approach. I just tired of having to add massive set-up and tear-down to each test in order to compensate for a lack of support for ordered execution in NUnit GUI. So went searching for a way around... At least I learned the diff between SetUp and TestFixtureSetUp - thanks Ben.

# re: NUnit Test Attribute Execution Order

Left by Rus Davies at 9/1/2010 12:51 PM
Gravatar
PlunderBunny commented about singleton classes being persistent between tests.

Actually, this needn't be so. You could, for example, include something like the following in your singleton class:

#if TEST
public void Clobber()
{
_instance = null;
}
#endif

You can then call Clobber() on your singleton from Setup() or TearDown().

Your comment:



 (will not be displayed)


 
 
 
Please add 1 and 1 and type the answer here:
 

Live Comment Preview:

 
«September»
SunMonTueWedThuFriSat
2930311234
567891011
12131415161718
19202122232425
262728293012
3456789