Search code examples
nunit

Nunit Test Order - Single Test - Multiple Orders


For Nunit Test order, is it possible to have multiple order positions for a test, such as [Test, Order(3, 10, #...)]?

Scenario - I am automating a multi-page online application using end-to-end ordered tests (with Asserts verifying entered info), and I want to verify info on previous pages is still there - saved - when I click Back button (basically, reverse-check the end-to-end starting from last page of application using Back button to navigate). I want to reuse Asserts already there instead of rewriting the same ones again. I have tried the following as an experiment, which generate build error CS1729 - 'type' does not contain a constructor that takes 'number' arguments: [Test, Order(66, 67)] ** [Test, Order(66)(67)] ** [Test, Order(66), Order(67)] ** [Test, Order(66)] [Test, Order(67)]

I Googled this, and I don't see anything that does the above, so I assume it is not possible, but want to confirm. I cannot remove the Order attribute as that's part of the requirement for this test.


Solution

  • No, that's not possible. The OrderAttribute sets a property, which is a simple integer.

    To simulate the effect you want, you may do the following:

    1. Extract all the code of the test in question to a separate method, called by the test. It should have the same signature as the test.
    2. Replicate the test you want to repeat, using a different name.
    3. Assign a different order attribute to each test.

    Assuming a signature of void TestMethod() it will look something like this...

    [Test, Order(66)]
    public void MyTest66()
    {
        MyRealTest();
    }
    
    [Test, Order(67)]
    public void MyTest67()
    {
        MyRealTest();
    }
    
    private void MyRealTest()
    {
        // Your actual test code goes here
    }
    

    Note... this is a really ugly hack but the only guaranteed way I can think of to do what you indicate is required.