Test case Order (Unit Test or Functional Test) by Display Name

XUnit Test case Order by Display Name

Today in this article we shall cover how to execute and Order Test cases by Display Name

As we already learned other techniques for ordering the test cases.

Ordering on test cases can be achieved by any of the below mechanisms,

  • Alphabetical – Order test method based on test name’s Alphabetical order

Please see for more details: How to Order Unit Test cases by Alphabetical – Guidelines

  • Priority– Order test method based on test name’s using priority order set by the user providing better control on execution.

Please see for more details: How to Order Unit Test cases by Priority – Guidelines

DisplayName – Order test method based on test name’s DisplayName order we shall cover this in today’s article,

Implement the ITestCaseOrderer and provide an ordering mechanism.

With this, we will order the Test cases using the collection name.

using System.Collections.Generic;
using System.Linq;
using Xunit;
using Xunit.Abstractions;

namespace OrechstrationService.Project
{
    public class DisplayNameOrderer : ITestCollectionOrderer
        {
            public IEnumerable<ITestCollection> OrderTestCollections(
                IEnumerable<ITestCollection> testCollections) =>
                testCollections.OrderBy(collection => collection.DisplayName);
        }

}

TestCaseOrderer now can be defined using DisplayNameOrderer as below. Please note to add below two attributes on the test cases namespace used for the test project,

[assembly: CollectionBehavior(DisableTestParallelization = true)]
[assembly: TestCollectionOrderer("OrechstrationService.Project.DisplayNameOrderer", "OrechstrationService.Project")]

One can define the collection name using the Collection attribute as below.

Here you can decorate the test class with the collection names, Ex. Saving Account TestCollection

Example

[Collection("Saving Account TestCollection")]

[Collection("Credit Account TestCollection")]

[Collection("Bank AccountTest Collection")]

Here below is a complete example,

using Xunit;


// Need to turn off test parallelization so we can validate the run order
[assembly: CollectionBehavior(DisableTestParallelization = true)]
[assembly: TestCollectionOrderer("OrechstrationService.Project.DisplayNameOrderer", "OrechstrationService.Project")]
namespace OrechstrationService.Project.Http
{

    [Collection("Saving Account TestCollection")]
    public class HttpHelperClassTests1
    {
        public static bool SavingAccountTestCollection;

        [Fact]
        public static void SavinggAccountController_OnGetAccount_Valid_HTTPClient()
        {
            //Arrange

            //Act

            //Assert
            Assert.True(HttpHelperClassTests2.CreditAccountTestCollection);
            Assert.True(HttpHelperClassTests3.BankAccountTestCollection);
            SavingAccountTestCollection = true;


        }
    }

    [Collection("Credit Account TestCollection")]
    public class HttpHelperClassTests2
    {
        public static bool CreditAccountTestCollection;


        [Fact]
        public static void CreditController_OnGetAccount_Valid_Typed_HTTPClient()
        {

            //Arrange

            //Act

            //Assert
            Assert.False(HttpHelperClassTests1.SavingAccountTestCollection);    
            Assert.True(HttpHelperClassTests3.BankAccountTestCollection);
            CreditAccountTestCollection = true;
        }

    }

    [Collection("Bank AccountTest Collection")]
    public class HttpHelperClassTests3
    {
        public static bool BankAccountTestCollection;

        [Fact]
        public static void BankController_OnGetAccount_Valid_Typed_HTTPClient()
        {

            //Arrange

            //Act

            //Assert

            Assert.False(HttpHelperClassTests1.SavingAccountTestCollection);
            Assert.False(HttpHelperClassTests2.CreditAccountTestCollection);
            BankAccountTestCollection = true;

        }

    }
}

Once executed test cases execution order will be as per the collection name order alphabetically,

Order Test cases by Display Name

References :

Do you have any comments or ideas or any better suggestions to share?

Please sound off your comments below.

Happy Coding !!



Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.



Leave a Reply

Your email address will not be published. Required fields are marked *