NUnit TestCase – Array and Execution Order – NUnit Tutorial

In this C# NUnit tutorial, we’ll learn about how to pass same type of objects array into test method and how to control the execution order of test methods.

NUnit TestCase Array

Below is the example of passing array to a test method.

[TestCase(new int[] { 2, 4, 6 })]
public void When_AllNumberAreEven_Expects_AreAllNumbersEvenAsTrue(int[] numbers)
{
    Number number = new Number();

    bool result = number.AreAllNumbersEven(numbers);

    Assert.That(result == true);
}

There is one restriction on array type. Array type must be a constant expression. Array types are limited to below types:

  1. bool
  2. byte
  3. char
  4. short
  5. int
  6. long
  7. float
  8. double
  9. Enum
  10. object

For passing other data types like string, we can use either object type or can use NUnit TestCaseSource. Below is the example of passing strings as object array.

[TestCase(new object[] { "1", "2", "3" })]
public void When_AllNumberAreEven_Expects_AreAllNumbersEvenAsTrue(object[] numbers)
{
    ....
}

TestCaseSource Attribute

TestCaseSource indicates that pass source parameter can be used as a parameter. Below is the syntax of TestCaseSource.

[TestCaseSource(Type sourceType, string sourceName)]

In the source type, we can define the parameter type. In sourceName we provide the name of the data source. In source name, we can provide below names:

  • Static Field / Property / Method Name
  • Property Name
  • Field Name
  • Custom Type implements IEnumerable

Below is the example of passing string array using Custom Type in TestCaseSource attribute.

public class StringArrayTestDataSource : IEnumerable
{
    public IEnumerator GetEnumerator()
    {
        yield return new string[] { "2", "4", "6" };
        yield return new string[] { "3", "4", "5" };
        yield return new string[] { "6", "8", "10" };
    }
}

[TestFixture]
public class EmployeeTests
{
    [TestCaseSource(typeof(StringArrayTestDataSource))]
    public void When_StringArrayAreEvenNumbers_Expects_IsStringArrayOfEvenNumbersAsTrue(string[] numbers)
    {
        Number number = new Number();

        bool result = number.IsStringArrayOfEvenNumbers(numbers);

        Assert.That(result == true);
    }
}

In the above example, we have create a new class StringArrayTestDataSource that implements interface IEnumerable. In the GetEnumerator method, we returns our string arrays. We have applied TestCaseSource attribute and pass typeof(StringArrayTestDataSource) as parameter. In the test method parameter we have used string array parameter. 

NUnit TestRunner will pick a string array from GetEnumerator method and pass one by one into the test method.

NUnit TestCase Execution Order

Sometimes we need to execute test methods in a particular order. These test method are dependent on each other. For that, NUnit provides the Order attribute. Below is the example.

[TestCase]
[Order(1)]
public void When_AgeGreaterAndEqualTo60_Expects_IsSeniorCitizeAsTrue()
{
    ....
}

[TestCase]
[Order(2)]
public void When_AgeLessThan60_Expects_IsSeniorCitizeAsFalse()
{
    ....
}