Search Results for

    Show / Hide Table of Contents

    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

    1. 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.
    2. Tests with [Order] are started before any tests without the attribute.
    3. Ordered tests are started in ascending order of the order value.
    4. Among tests with the same order value, or among tests without the attribute, execution order is indeterminate.
    5. Tests do not wait for prior tests to finish. When using parallel execution, a test may start while earlier tests are still running.
    6. Negative order values are valid and will run before positive values.

    See Also

    • Parallelizable Attribute
    • NonParallelizable Attribute
    • Edit this page
    In this article
    Back to top Generated by DocFX | Copyright (c) 2018- The NUnit Project - Licensed under CC BY-NC-SA 4.0