Skip to content

TestCaseData

jnm2 edited this page Jun 14, 2017 · 10 revisions

Although any object implementing ITestCaseData may be used to provide extended test case information, NUnit provides the TestCaseData class for this purpose. The following example returns TestCaseData instances from a data source in a separately defined class.

[TestFixture]
public class MyTests
{
    [Test,TestCaseSource(typeof(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 specifing the reason for doing so.

  • Ignore(string) causes the test case to be ignored and specifies the reason, which is required.

  • Returns specifies the expected result to be returned from the method, which must have a compatible return type.

  • 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.

Clone this wiki locally