Order
OrderAttribute is used on test methods or fixtures to specify the order in which tests are run within their containing suite. Tests are started in ascending order of the order value.
Constructor
OrderAttribute(int order)
| Parameter | Type | Description |
|---|---|---|
order |
int |
The order value. Tests are started in ascending order of this value. Lower values run first. |
Applies To
| Test Methods | Test Fixtures (Classes) | Assembly |
|---|---|---|
| ✅ | ✅ | ❌ |
Note
For fixtures, Order orders fixtures within their containing namespace. For test methods, it orders tests within their containing fixture.
Examples
Basic Ordering
[TestFixture]
public class OrderedTests
{
private static int _executionOrder;
[OneTimeSetUp]
public void Setup() => _executionOrder = 0;
[Test, Order(1)]
public void FirstTest()
{
_executionOrder++;
Assert.That(_executionOrder, Is.EqualTo(1));
}
[Test, Order(2)]
public void SecondTest()
{
_executionOrder++;
Assert.That(_executionOrder, Is.EqualTo(2));
}
[Test, Order(3)]
public void ThirdTest()
{
_executionOrder++;
Assert.That(_executionOrder, Is.EqualTo(3));
}
[Test]
public void UnorderedTest()
{
// Tests without Order run after all ordered tests
_executionOrder++;
Assert.That(_executionOrder, Is.EqualTo(4));
}
}
Ordering Fixtures
[TestFixture, Order(1)]
public class FirstFixture
{
[Test]
public void Test() => Assert.Pass();
}
[TestFixture, Order(2)]
public class SecondFixture
{
[Test]
public void Test() => Assert.Pass();
}
Using Gaps for Future Insertions
[TestFixture]
public class OrderWithGapsTests
{
// Using gaps allows inserting tests later without renumbering
[Test, Order(10)]
public void InitializationTest() => Assert.Pass();
[Test, Order(20)]
public void ProcessingTest() => Assert.Pass();
[Test, Order(30)]
public void CleanupTest() => Assert.Pass();
}
Notes
- Ordering is local to the containing suite. For test methods, ordering applies within the fixture. For fixtures, it applies within the namespace. There is no facility to order tests globally.
- Tests with
[Order]are started before any tests without the attribute. - Ordered tests are started in ascending order of the
ordervalue. - Among tests with the same
ordervalue, or among tests without the attribute, execution order is indeterminate. - Tests do not wait for prior tests to finish. When using parallel execution, a test may start while earlier tests are still running.
- Negative order values are valid and will run before positive values.