TestCaseAttribute (NUnit 2.5)

TestCaseAttribute serves the dual purpose of marking a method with parameters as a test method and providing inline data to be used when invoking that method. Here is an example of a test being run three times, with three different sets of data:

public void DivideTest(int n, int d, int q)
  Assert.AreEqual( q, n / d );

Note: Because arguments to .NET attributes are limited in terms of the Types that may be used, NUnit will make some attempt to convert the supplied values using Convert.ChangeType() before supplying it to the test.

TestCaseAttribute may appear one or more times on a test method, which may also carry other attributes providing test data, such as the FactoriesAttribute. The method may optionally be marked with the TestAttribute as well.

By using the named parameter Result this test set may be simplified further:

[TestCase(12,3, Result=4)]
[TestCase(12,2, Result=6)]
[TestCase(12,4, Result=3)]
public int DivideTest(int n, int d)
  return( n / d );

In the above example, NUnit checks that the return value of the method is equal to the expected result provided on the attribut

TestCaseAttribute supports a number of additional named parameters, which may be used as follows:

Sets the description property of the test
Specifies a the Type of an exception that should be thrown by this invocation
Specifies a the FullName of an exception that should be thrown by this invocation
Specifies the message text of the expected exception
Set to true in order to make the individual test case Explicit. Use Reason to explain why.
Set to true in order to ignore the individual test case. Use Reason to explain why.
Causes this test case to be ignored and specifies the reason. Equivalent to Ignore combined with Reason. This attribute will be removed in a later release.
A MessageMatch enum value indicating how to test the expected message (See ExpectedExceptionAttribute)
Specifies the reason for not running this test case. Use in conjunction with Ignore or Explicit.
The expected result to be returned from the method, which must have a compatible return type.
Provides a name for the test. If not specified, a name is generated based on the method name and the arguments provided.

Order of Execution

In NUnit 2.5, individual test cases are sorted alphabetically and executed in that order. Beginning with NUnit 2.5.1, the individual cases are not sorted, but are executed in the order in which NUnit discovers them. This order does not follow the lexical order of the attributes and will often vary between different compilers or different versions of the CLR.

As a result, when TestCaseAttribute appears multiple times on a method or when other data-providing attributes are used in combination with TestCaseAttribute, the order of the test cases is undefined.