Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Order NUnit Tests

Tags:

More than once the question has been asked on SO. But the only answers that are given read "you should not need to order your unit tests, it is bad because" or "you can avoid that if..."

I already know it is bad, why it is bad, and techniques to avoid it. But that is not what I want to know. I'd like to know if it is possible to order the execution of NUnit tests, other than an alphabetical order. To be blunt: I actually want state to propogate from one test to the next. Trust me that I have a clever reason for this, that defies the usual philosophy.

MSTest has the "ordered test" capability, which is very useful in certain cases. I'd like to have that ability in NUnit. Can it be done?

like image 229
Brent Arias Avatar asked Jun 06 '11 05:06

Brent Arias


People also ask

How do I order test cases in NUnit?

There is no facility in NUnit to order tests globally. Tests with an OrderAttribute argument are started before any tests without the attribute. Ordered tests are started in ascending order of the order argument. Among tests with the same order value or without the attribute, execution order is indeterminate.


2 Answers

Update for NUnit 3.2.0 - now it support OrderAttribute.

The OrderAttribute may be placed on a test method to specify the order in which tests are run. Example:

public class MyFixture {     [Test, Order(1)]     public void TestA() { ... }       [Test, Order(2)]     public void TestB() { ... }      [Test]     public void TestC() { ... } } 

https://github.com/nunit/docs/wiki/Order-Attribute

like image 84
scar80 Avatar answered Oct 25 '22 08:10

scar80


The work-around (hack) is to alphabetize your test case names. See this thread:

https://bugs.launchpad.net/nunit-3.0/+bug/740539

Relying on alphabetical order is a workaround that you can use but it is not documented and supported beyond the visual order of the display. In theory it could change at any time. In practice it won't change until NUnit 3.0, so you're pretty safe using it as a workaround

This quote is from Charlie Poole, the main dev on NUnit.

It also seems they have a scheme cooking to support ordered tests in NUnit 3, though how they will do so is still under discussion.

like image 45
Merlyn Morgan-Graham Avatar answered Oct 25 '22 08:10

Merlyn Morgan-Graham