Search Results for

    Show / Hide Table of Contents

    TestCaseData

    The TestCaseData class provides extended test case information for a parameterized test, although any object deriving from TestCaseParameters may be used. Unlike NUnit 2, you cannot implement ITestCaseData, you must derive from TestCaseParameters.

    [TestFixture]
    public class MyTests
    {
        [TestCaseSource(typeof(MyDataClass), nameof(MyDataClass.TestCases))]
        public int DivideTest(int n, int d)
        {
            return n / d;
        }
    }
    
    public class MyDataClass
    {
        public static IEnumerable TestCases
        {
            get
            {
                yield return new TestCaseData(12, 3).Returns(4);
                yield return new TestCaseData(12, 2).Returns(6);
                yield return new TestCaseData(12, 4).Returns(3);
            }
        }
    }
    

    This example uses the fluent interface supported by TestCaseData to make the program more readable.

    TestCaseData supports the following properties and methods, which may be appended to an instance in any order.

    • Explicit() or Explicit(string) causes the test case to be marked explicit, optionally specifying the reason for doing so.
    • Ignore(string) causes the test case to be ignored and specifies the reason, which is required. Can be chained with Until(DateTimeOffset) to specify a date after which the test should run again.
    • Returns specifies the expected result to be returned from the method, which must have a compatible return type.
    • SetArgDisplayNames(string[]) sets the list of display names to use as the parameters in the test name.
    • SetArgDisplayNames(object[]) sets the list of display names using objects that are automatically formatted.
    • SetCategory(string) applies a category to the test.
    • SetDescription(string) sets the description property of the test.
    • SetName(string) provides a name for the test. If not specified, a name is generated based on the method name and the arguments provided. See Template Based Test Naming.
    • SetProperty(string, string), SetProperty(string, int) and SetProperty(string, double) apply a named property and value to the test.
    • TypeArgs specifies the Types to be used when targeting a generic test method. (NUnit 4.1+)
    • Until(DateTimeOffset) can be chained after Ignore(string) to specify a date after which the test case should no longer be ignored. Once the date passes, the test will run automatically. (NUnit 4+)

    Temporarily ignoring test cases with Until

    The Until method allows you to temporarily ignore a test case until a specific date. This is useful for tests that are known to fail due to external factors (e.g., waiting for an API release, known bug in a dependency) but should automatically start running again after a certain date.

    [TestFixture]
    public class IgnoreUntilTests
    {
        [TestCaseSource(nameof(IgnoredTestCases))]
        public void TestWithTemporaryIgnore(int value)
        {
            Assert.That(value, Is.Positive);
        }
    
        private static IEnumerable<TestCaseData> IgnoredTestCases()
        {
            // This test case runs normally
            yield return new TestCaseData(1);
    
            // This test case is ignored indefinitely
            yield return new TestCaseData(2)
                .Ignore("Known bug - see issue #123");
    
            // This test case is ignored until the specified date
            // After the date passes, the test will run automatically
            yield return new TestCaseData(3)
                .Ignore("Waiting for API v2 release")
                .Until(new DateTimeOffset(2025, 6, 1, 0, 0, 0, TimeSpan.Zero));
    
            // This test was ignored in the past - it now runs because the date has passed
            yield return new TestCaseData(4)
                .Ignore("Was waiting for feature X")
                .Until(new DateTimeOffset(2020, 1, 1, 0, 0, 0, TimeSpan.Zero));
        }
    }
    

    Generic TestCaseData variants

    As of NUnit 4.1, generic versions of TestCaseData are available that automatically set the TypeArgs property based on their type parameters. This provides a more concise syntax when working with generic test methods:

    Class Usage
    TestCaseData<T> Single type parameter
    TestCaseData<T1, T2> Two type parameters
    TestCaseData<T1, T2, T3> Three type parameters
    TestCaseData<T1, T2, T3, T4> Four type parameters
    TestCaseData<T1, T2, T3, T4, T5> Five type parameters
    [TestFixture]
    public class GenericTestCaseDataTests
    {
        [TestCaseSource(nameof(GenericTestCases))]
        public Type GenericMethodWithExplicitType<T>(T input)
        {
            return typeof(T);
        }
    
        private static IEnumerable<TestCaseData> GenericTestCases()
        {
            // Using TestCaseData<T> automatically sets TypeArgs to typeof(T)
            // This ensures the generic method uses 'long'
            yield return new TestCaseData<long>(2L)
            {
                ExpectedResult = typeof(long)
            };
    
            // Another single type parameter example with TestCaseData<T>
            yield return new TestCaseData<int>(42)
            {
                ExpectedResult = typeof(int)
            };
        }
    
        [TestCaseSource(nameof(TwoTypeParameterCases))]
        public void GenericMethodWithTwoTypes<T1, T2>(T1 first, T2 second)
        {
            Assert.That(first, Is.TypeOf<T1>());
            Assert.That(second, Is.TypeOf<T2>());
        }
    
        private static IEnumerable<TestCaseData> TwoTypeParameterCases()
        {
            // TestCaseData<T1, T2> sets TypeArgs = [typeof(T1), typeof(T2)]
            yield return new TestCaseData<string, int>("hello", 42);
            yield return new TestCaseData<double, bool>(3.14, true);
        }
    }
    

    SetArgDisplayNames with objects

    The SetArgDisplayNames method has an overload that accepts object[] instead of string[]. Objects passed to this overload are automatically formatted using NUnit's value formatting logic, which is the same logic used for default test names.

    [TestFixture]
    public class SetArgDisplayNamesObjectTests
    {
        [TestCaseSource(nameof(DisplayNameCases))]
        public void TestWithCustomDisplayNames(Person person, int expectedAge)
        {
            Assert.That(person.Age, Is.EqualTo(expectedAge));
        }
    
        private static IEnumerable<TestCaseData> DisplayNameCases()
        {
            var john = new Person { Name = "John", Age = 30 };
            var jane = new Person { Name = "Jane", Age = 25 };
    
            // SetArgDisplayNames with strings
            yield return new TestCaseData(john, 30)
                .SetArgDisplayNames("John Doe", "30");
    
            // SetArgDisplayNames with objects - they are automatically formatted
            // This is useful when you want the display name to match the object's ToString()
            yield return new TestCaseData(jane, 25)
                .SetArgDisplayNames(jane.Name, jane.Age);
        }
    
        public class Person
        {
            public string Name { get; set; } = "";
            public int Age { get; set; }
        }
    }
    
    • Edit this page
    In this article
    Back to top Generated by DocFX | Copyright (c) 2018- The NUnit Project - Licensed under CC BY-NC-SA 4.0