0

I remember reading on a forum that C# Unit/Integration/Functional tests run in alphabetically order but that doesn't seem to be the case. I have 1 file that holds all of my tests and I put them from top to bottom in order like this.

  1. A1_PostFirsttoRegisterClaimsAdmin
  2. B2_CreateMasterWalletTest
  3. C3_SendCoinsToProfile
  4. D4_LoginTest

But when I click "Run All Tests," it runs in this order.
enter image description here

So how do I get the tests to run in order?

  • Does this answer your question? [How to set the test case sequence in xUnit](https://stackoverflow.com/questions/9210281/how-to-set-the-test-case-sequence-in-xunit) – Juan Jun 21 '22 at 01:47
  • 6
    Why does it matter what order your tests run in? It's a good idea to write your tests so that they can run in any order and/or only a subset of tests (as determined by the test runner). – gunr2171 Jun 21 '22 at 01:50

1 Answers1

0

For xUnit you have to provide a TestOrderer.

public class AlphabeticalOrderer : ITestCaseOrderer
{
    public IEnumerable<TTestCase> OrderTestCases<TTestCase>(
        IEnumerable<TTestCase> testCases) where TTestCase : ITestCase =>
        testCases.OrderBy(testCase => testCase.TestMethod.Method.Name);
}

Then you have to annotate your test class with an attribute to use this TestOrderer.

[TestOrderer("OrdererTypeName", "OrdererAssemblyName")]
public class MyTestClass
{
  [Fact]
  public void A1_PostFirsttoRegisterClaimsAdmin() { }

  [Fact]
  public void B2_CreateMasterWalletTest() { }
}

You can find more information about the ordering of unit tests in the Microsoft Docs

Zalhera
  • 619
  • 5
  • 18
  • I tried making a file `TestOrderer.cs` in the same directory as `AdminControllerTests.cs` with the `public class AlphabeticalOrderer` code and I added `[TestOrderer("OrdererTypeName", "OrdererAssemblyName")]` in `AdminControllerTests.cs` above the line `public class AdminControllerTests` but it won't compile with an error on `TestOrderer`: `namespace amaranth.Tests` – NextNightFlyer Jun 21 '22 at 23:35
  • You habe to replace "OrdererTypeName" with the full namespace of your custom orderer. So it should be "YourFullNamespace.AlphabeticalOrderer". Then you also have to replace "OrdererAssemblyName" with the name of the assembly where you defined the Orderer class. – Zalhera Jun 22 '22 at 14:40