Search code examples
c#.netattributesnunittdd

NUnit Test Run Order


By default nunit tests run alphabetically. Does anyone know of any way to set the execution order? Does an attribute exist for this?


Solution

  • 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.