Nunit Notes

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:

[TestCase(12,3,4)]
[TestCase(12,2,6)]
[TestCase(12,4,3)]
public void DivideTest(int n, int d, int q) {
  Assert.AreEqual( q, n / d );
}

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 );
}

Notes from: Nunit.org