22

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?

Community
  • 1
  • 1
Brent Arias
  • 29,277
  • 40
  • 133
  • 234

5 Answers5

21

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

scar80
  • 1,642
  • 2
  • 18
  • 36
  • Does this not affect the order in the test explorer window? – Denis535 Jun 04 '19 at 14:45
  • Old question, but I would add that if tests are in different classes, the order won't be correct. If you want to organize the tests in different classes to avoid a long class, you can use the partial class feature. – Suciu Eus Sep 20 '21 at 12:51
10

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.

Merlyn Morgan-Graham
  • 58,163
  • 16
  • 128
  • 183
5

Just an update for NUnit 2.5.1. According to documentation there are cases that even alphabetical order is not supported.

NUnit TestCaseAttribute

Order of Execution

In NUnit 2.5, individual test cases are sorted alphabetically and executed in that order. With NUnit 2.5.1, the individual cases are not sorted, but are executed in the order in which NUnit discovers them. This order does not follow the lexical order of the attributes and will often vary between different compilers or different versions of the CLR.

As a result, when TestCaseAttribute appears multiple times on a method or when other data-providing attributes are used in combination with TestCaseAttribute, the order of the test cases is undefined.

hammer7kg
  • 49
  • 1
  • 5
2

Try to use NameParameters argument to pass the TestName with a string you wish, in order the TestCase() to be ordered by TestName.

    [TestCase(..., TestName = "1stTest")]
    [TestCase(..., TestName = "2ndTest")]
ageroh
  • 93
  • 1
  • 3
0

for Nuint you can use following code .

[TestMethod]
    [Priority(2)]
Aman Sharma
  • 311
  • 1
  • 8
  • 22