docs/articles/nunit/writing-tests/attributes/testcase.md
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:
[!code-csharpBasicTestCase]
[!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. The method may optionally be marked with the Test Attribute as well.
By using the named parameter ExpectedResult this test set may be simplified further:
[!code-csharpTestCaseWithExpectedResult]
In the above example, NUnit checks that the return value of the method is equal to the expected result provided on the attribute.
TestCaseAttribute supports a number of additional named parameters:
Types to be used when targeting a generic test method. (NUnit 4.1+)Ignore Attribute Usage, by Example[!WARNING] When using the
Ignoreparameter (and others, see below), note that this has to be a named parameter. It is easy to accidentally add anotherIgnoreattribute after theTestCaseattribute. That will be the same as adding it separately, and it will apply to the complete fixture. This may apply to other named parameters, with names equal to other attributes, like theExplicitandCategoryparameters.
Correct example usage:
[!code-csharpTestCaseWithIgnore]
<!-- cspell:disable-next-line -->[!WARNING] Wrong way! Below, we demonstrate an incorrect approach.
(1) Adding it on the same line is the same as adding it on a separate line (3), both results in the fixture being ignored (2).
Thanks to Geir Marius Gjul for raising this question again.
Explicit Attribute Usage, by ExampleExplicit, used correctly, looks like the following:
[!code-csharpTestCaseWithExplicit]
Note that adding the Reason is optional, and Visual Studio TestExplorer will not even show it.
Category Attribute Usage, by ExampleCategories can be applied to a single TestCase the same way, as a named parameter. Otherwise, it will apply to the whole fixture. Be sure what you're asking for!
[!code-csharpTestCaseWithCategory]
Individual test cases are executed in the order in which NUnit discovers them. This order does not necessarily 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.