Skip to content

Controlling execution order of unit tests #3162

@shayan0v0n

Description

@shayan0v0n

Summary

I am suggesting an enhancement to allow developers to set priority or execution order for methods within a single test class. Currently, there is no built-in way to control the execution order of test methods in a class, which can be critical for certain test scenarios.

At present, we have a test class with multiple test methods as shown below:

    [TestClass]
    public class ExampleTests
    {
        [TestMethod]
        public async Task Get_Return_3()
        {
                   //tests
        }

        [TestMethod]
        public async Task Get_Return_1()
        {
                   //tests
        }

        [TestMethod]
        public async Task Get_Return_2()
        {
                   //tests
        }
    }
}

In this scenario, there is no control over the execution order of the test methods. The .NET documentation provides a way to order unit tests using the Priority attribute, but it is somewhat cumbersome and not very intuitive.
https://learn.microsoft.com/en-us/dotnet/core/testing/order-unit-tests?pivots=mstest

I propose adding a simple attribute that allows setting the execution priority for each test method within a class. Here is an example of how it might look:

    [TestClass]
    public class ExampleTests
    {
        [TestMethod]
        [Priority(1)]
        public async Task Get_Banner_Return_3()
        {
                   //tests
        }

        [TestMethod]
        [Priority(2)]
        public async Task Get_Banner_Return_1()
        {
                   //tests
        }

        [TestMethod]
        [Priority(3)]
        public async Task Get_Banner_Return_2()
        {
                   //tests
        }
    }
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Needs: DesignNeeds research and proposal for how the feature will be implemented.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions