Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NUnit Test Run Order

People also ask

What order does NUnit run tests?

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. Tests do not wait for prior tests to finish. If multiple threads are in use, a test may be started while some earlier tests are still being run.

Do NUnit tests run in parallel?

The NUnit 3.0 framework can run tests in parallel within an assembly. This is a completely separate facility from Engine Parallel Test Execution, although it is possible to use both in the same test run. By default, no parallel execution takes place.

What is the use of order attribute in NUnit?

To order tests explicitly, NUnit provides an OrderAttribute . Tests with this attribute are started before tests without. The order value is used to determine the order to run the unit tests.

Does SetUp run before every test NUnit?

Inheritance. The SetUp attribute is inherited from any base class. Therefore, if a base class has defined a SetUp method, that method will be called before each test method in the derived class.


I just want to point out that while most of the responders assumed these were unit tests, the question did not specify that they were.

nUnit is a great tool that can be used for a variety of testing situations. I can see appropriate reasons for wanting to control test order.

In those situations I have had to resort to incorporating a run order into the test name. It would be great to be able to specify run order using an attribute.


NUnit 3.2.0 added an OrderAttribute, see:

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

Example:

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


    [Test, Order(2)]
    public void TestB() { ... }

    [Test]
    public void TestC() { ... }
}

Your unit tests should each be able to run independently and stand alone. If they satisfy this criterion then the order does not matter.

There are occasions however where you will want to run certain tests first. A typical example is in a Continuous Integration situation where some tests are longer running than others. We use the category attribute so that we can run the tests which use mocking ahead of the tests which use the database.

i.e. put this at the start of your quick tests

[Category("QuickTests")]

Where you have tests which are dependant on certain environmental conditions, consider the TestFixtureSetUp and TestFixtureTearDown attributes, which allow you to mark methods to be executed before and after your tests.


Wanting the tests to run in a specific order does not mean that the tests are dependent on each other - I'm working on a TDD project at the moment, and being a good TDDer I've mocked/stubbed everything, but it would make it more readable if I could specify the order which the tests results are displayed - thematically instead of alphabetically. So far the only thing I can think of is to prepend a_ b_ c_ to classes to classes, namespaces and methods. (Not nice) I think a [TestOrderAttribute] attribute would be nice - not stricly followed by the framework, but a hint so we can achieve this