Home > Enterprise >  Run selenium NUnit tests in specific order
Run selenium NUnit tests in specific order

Time:01-09

I have a selenium project written with NUnit in C# .NET 6. I have a folder called 'Tests' where there are multiple sub folders and each folder has a lot of classes. Each class has only one Test method. The reason for this is for structuring the project and each class represents one process in the software I'm testing. However, some processes need to be run after some other processes have already ran.

My question is; is there any way to run the classes in a specific order I want? I have tried using

dotnet test --filter

However this did not work. I also tried using NUnit's

Order

attribute but this works only when a class has multiple test methods.

CodePudding user response:

The Order attribute may be placed on a class or a method. From the NUnit docs:

The OrderAttribute may be placed on a test method or fixture to specify the order in which tests are run within the fixture or other suite in which they are contained.

The bold italics in the citation are mine. In your case, the "other suite" containing the fixture class is the namespace in which it is defined.

There is no "global" ordering facility, but if all your tests are in the same namespace, using the OrderAttribute on the fixtures will cause them to run in the order you specify. If it doesn't interfere with any other use of the namespaces you might consider putting them all in one namespace.

A couple of notes:

  1. The OrderAttribute specifies the order in which the tests start. If you run tests in parallel, multiple tests may run at the same time.

  2. It's not advisable to have the tests depend on one another in most cases.

  3. There are lots of reasons not to control the order of tests, which are covered in the answers quoted by other folks. I'm just answering the specific "how-to" question you posed.

  • Related