Testcaseattribute (nunit 2.5)
The testcase feature has two effects, including marking the parameters used by a method and providing built-in data during the call. This example uses different datasets for three times:
[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 );}
Note: the. NET feature limits the parameter type. nunit will try to convert the provided value with convert. changetype () before providing it to the test force.
The testcase feature can be used multiple times in a test method. The testcase feature can also use factoriesattribute and other features to provide data.
By using the result parameter, the test case can be simpler.
[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 preceding example, nunit checks whether the return value of the method is equal to the value of the provided result parameter.
Testcaseattribute also provides other column attributes:
Description: describes the test case details.
Expectedexception: Specifies the exception to be thrown.
Expectedexceptionname: Specifies the full name of the exception to be thrown.
Expectedmessage: specifies the information that is expected to throw an exception.
Explict: Set to true to mark the test case as display execution, and use reason to explain the reason
Ignore: Set to true to specify ignore test cases and use reason to interpret the reason
Ignorereason: Specify to ignore test cases and reasons, equivalent to using ignore & reason at the same time. Note: This feature will be removed in a later version.
Matchtype:MessagematchEnumeration type, specifying the expected test information (for details, see expectedexceptionattribute)
Reason: Specifies the reason for not executing the test case, used in combination with ignore or explicit features
Result: Specifies the expected return value of the test method. The return value type must be compatible.
Testname: specify a name for the test case. If not specified, the name is automatically generated based on the test method and parameters.
Execution sequence:
Nunit2.5 is sorted and executed in a few alphabetic order. From nunit2.5.1, test cases are not sorted and executed in the order of release. The sequence of this discovery is not arranged according to the feature dictionary order and will vary according to different compilers and CLR versions.
The result is that in a test method, the testcase feature appears multiple times or other features that provide data are used together with the testcase feature. The sequence of test cases is unclear.