NUnit Tutorial
Tags

NUnit TestCase C# Tutorial

In our last tutorial, we learned about NUNit TestFixture attribute. In this tutorial, we learn about NUnit TestCase attribute and its usage.

TestCase attribute is used for two purposes.

  1. It marks a method to Test Method.
  2. Pass arguments / parameters to Test Method.

Test Method

When we assign TestCase attribute to any method that lets NUnit test runner to discover this method as test method and so that NUnit test runner can execute it later.

Below is the example of NUnit TestCase.


namespace EmployeeService
{
    public class Employee
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }

        public bool IsSeniorCitizen()
        {
            if(Age >= 60)
            {
                return true;
            }
            return false;
        }
    }
}

using NUnit.Framework;

namespace EmployeeService.Tests
{
    [TestFixture]
    public class EmployeeTests
    {
        [TestCase]
        public void When_AgeGreaterAndEqualTo60_Expects_IsSeniorCitizeAsTrue()
        {
            Employee emp = new Employee();
            emp.Age = 60;

            bool result = emp.IsSeniorCitizen();

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

NUnit TestCase Arguments / Parameters

TestCase arguments are used when we have to use same test case with different data.

For example, in the above case, we fixed the age of employee as 60. For different ages we have to write different test cases. But by using the TestCase parameters we can use same test method for different ages.


[TestCase(60)]
[TestCase(80)]
[TestCase(90)]
public void When_AgeGreaterAndEqualTo60_Expects_IsSeniorCitizeAsTrue(int age)
{
    Employee emp = new Employee();
    emp.Age = age;

    bool result = emp.IsSeniorCitizen();

    Assert.That(result == true);
}

In this example, we have use three TestCase attributes on same method with different parameters. NUnit framework will create three different test cases using these three parameters.

NUnit TestCase Arguments

NUnit TestCase ExpectedResult

In the above example, we have fixed the result to true that means we can only check the above test case with positive parameters. But by using ExpectedResult property, we can also specify different results for different parameters. Below is the example:


[TestCase(29, ExpectedResult = false)]
[TestCase(0, ExpectedResult = false)]
[TestCase(60, ExpectedResult = true)]
[TestCase(80, ExpectedResult = true)]
[TestCase(90, ExpectedResult = true)]
public bool When_AgeGreaterAndEqualTo60_Expects_IsSeniorCitizeAsTrue(int age)
{
    Employee emp = new Employee();
    emp.Age = age;

    bool result = emp.IsSeniorCitizen();

    return result;
}

In this example, we change the return type of method to bool data type and also change the last line of test case method to return statement. In the parameters, we specify the ExpectedResult as bool data type matching return type of test method.

Author Property

We can specify author name in the test method who has written the test case. Below is the example:


[TestCase(Author = "Michael")]
public void When_AgeGreaterAndEqualTo60_Expects_IsSeniorCitizeAsTrue()
{
    ...
}

[TestCase(Author = "George")]
public void When_AgeGreaterAndEqualTo100_Expects_IsSeniorCitizeAsTrue()
{
    ...
}

For executing tests, right click on any test method and choose GroupBy -> Traits.

NUnit GroupBy Traits

By choosing this option, test explorer categorized test methods according to different properties of TestCase. Below is the example of Author property group by:

NUnit Trait Example

TestName property

TestName property is used when we have to use different name than the specified test method name. Below is the example:


[TestCase(TestName = "EmployeeAgeGreaterAndEqualTo60_Expects_IsSCitizenAsTrue")]
public void When_AgeGreaterAndEqualTo60_Expects_IsSeniorCitizeAsTrue()
{
        ...
}

[TestCase(TestName = "EmployeeAgeGreaterThan100_Expects_IsSCitizenAsTrue")]
public void When_AgeGreaterThan100_Expects_IsSeniorCitizeAsTrue()
{
    ...
}

Ignore TestCase

Sometimes we need to ignore our test case reason may be code is not yet complete. So we can use Ignore property to mark test case as ignore. This still show test method in Test Explorer but test explorer will not execute it.


[TestCase(Ignore = "Code is not complete yet.")]
public void When_AgeGreaterAndEqualTo60_Expects_IsSeniorCitizeAsTrue()
{
    ....
}

In next tutorial, we learn more about TestCase properties, like how to pass array to TestCase method and control the execution order.