Nunit testing tool instance, nunit testing tool instance
Preface:
This article mainly introduces the basic use of Nunit, including the details of many existing articles. Due to the recent use of Nunit for testing, I sorted out the articles on the Internet, at the same time, I joined some of my own practices.
NUnit attributes
TestFixture
It marks a class containing test and declares that this class is used for testing. It is generally used before the definition of class;
Test
It is usually placed before the method to test the method. Adding the Description parameter to the function we are testing can add Description information.
TestCase (arguments)
If you want to perform multiple tests by using commas (,), you can use the property Tag Method ([TestCase (1, 2), TestCase (2, 3)]).
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.
Explicit
The property tag test method needs to be executed explicitly on the UI interface. If you do not want to perform a unit test on a method, you can call this feature only when it is selected.
Ignore
The property indicates that a test method or test class is ignored. If the test class is ignored, the test method in the class is also ignored.
ExpectedException (Type)
The property tag test method throws an expected exception at runtime. If yes, the test passes. Otherwise, the test fails.
Category ("")
Attribute tags are used to classify a test (to test only the required categories). You can mark the methods and classes, on the NUnit-GUI page, select the type of the parameter to be included in the Categories tab, and test the test for this type only during Run. If none is selected, all tests are performed by default.
TestFixtureSetUp
The property TAG method is the class-level setting (initialization) method. An Initialization is performed throughout the test class. All test methods share the initialization data, the method for marking this attribute can simply think of it as an initiator, just like the constructor of the row class.
TestFixtureTearDown
The property marking method is a Class-level disassembly method, which is executed once throughout the test class. when all the test methods in the test class are completed, the disassembly method is executed to clear data and release resources. You can think of it as an analysis function, after the test is completed, the resources used during the test are recycled.
TearDown
The property labeling method is a function-level disassembly method. After each test method is executed, the disassembly method is executed. A test class can have only one TearDown/Setup/TestFixtureSetUp/TestFixtureTearDown method. If there are multiple definitions, the test class will be compiled successfully, but these labeled methods will not be run during testing.
SetUp
The property flag method is a function-level setting method. This setting method is executed before each test method is executed.
[Maxtime]/[Timeout]
Attribute indicates the maximum execution time of the Test case. If the former times out, the Test is not canceled, and the latter is forcibly interrupted. Usage: [Test, Maxtime (2000)], [Test, timeout (2000)].
Repeat
How many times the property tag Test method is repeated, for example, [Test, Repeat (5)].
[RequiresMTA]/[RequiresSTA]/[RequiresThread]
The property tag test case must run in the multi-thread, single-thread, and independent thread state.
Values
Attribute tag parameters of the test case. A set of values are passed in as parameters. NUnit splits these values into the corresponding number of sub-tests. When the two parameters of the test case are marked with [Values], NUnit generates two sets of Use Cases with the product of quantity by default, you must use [Sequential] to mark test cases in order to generate a one-to-one subtest case with n (n = the maximum array length in two groups ).
ValueSource
Attribute indicates the parameters of the test case. This parameter specifies the source of the data source. When [ValueSource] is used to specify the data source, this data source must implement the IEnumerable interface, the data source can be a property, a method without parameters, an instance, or a static member.
Nunit common classes and Methods
1. Assert: If the Assert fails, no method is returned and an error is reported. 1) test whether two parameters are equal
Assert. AreEqual;
Assert. AreEqual;
2) test whether two parameters reference the same object
Assert. AreSame;
Assert. AreNotSame;
3) test whether an object is contained by an array or list.
Assert. Contains;
4) test whether an object is larger than another object.
Assert. Greater;
(5) test whether an object is smaller than another object.
Assert. Less;
6) type assertions:
Assert. IsInstanceOfType;
Assert. IsAssignableFrom;
7) conditional test:
Assert. IsTrue;
Assert. IsFalse;
Assert. IsNull;
Assert. IsNotNull;
Assert. IsNaN; is used to determine whether the specified value is a number.
Assert. IsEmpty;
Assert. IsNotEmpty;
Assert. IsEmpty;
Assert. IsNotEmpty;
8) Other assertions:
The Assert. Fail; method provides you with the ability to create a failed test, which is based on tests that are not encapsulated by other methods. It is also useful for assertions for developing your own specific project.
Assert. Pass; force the test to Pass
2. String assert: provides many useful methods for checking string values.
StringAssert. Contains;
StringAssert. StartsWith;
StringAssert. EndsWith;
StringAssert. AreEqualIgnoringCase;
3. CollectionAssert class
CollectionAssert. AllItemsAreInstancesOfType; whether the items in the set are XX-type instances
CollectionAssert. AllItemsAreNotNull: the items in the set are not empty.
CollectionAssert. AllItemsAreUnique; each item in the set is unique
CollectionAssert. AreEqual; two sets are equal
CollectionAssert. AreEquivalent; the two sets are equivalent
CollectionAssert. AreNotEqual; the two sets are not equal.
CollectionAssert. AreNotEquivalent; the two sets are not equivalent.
CollectionAssert. Contains;
CollectionAssert. DoesNotContain; The collection does not contain an object.
CollectionAssert. IsSubsetOf: A set is a subset of another set.
CollectionAssert. IsNotSubsetOf: A set is not a subset of another set.
CollectionAssert. IsEmpty; the set is empty.
CollectionAssert. IsNotEmpty; the set is not empty.
CollectionAssert. IsOrdered; the items in the set have been sorted.
4. FileAssert
FileAssert. AreEqual;
FileAssert. AreNotEqual;
5. DirectoryAssert
DirectoryAssert. AreEqual;
DirectoryAssert. AreNotEqual;
DirectoryAssert. IsEmpty;
DirectoryAssert. IsNotEmpty;
DirectoryAssert. IsWithin;
DirectoryAssert. IsNotWithin;
Download Nunit testing tool:
ReSharper integration:
Http://www.jetbrains.com/resharper/download/
Nunit tool:
Https://github.com/nunit/nunitv2/releases/tag/2.6.4 download the latest release
Nunit tool usage
1. Download and decompress the package, get the NUnit-2.6.4 folder, open the binfile in the folder, such:
2. Select nunit.exeto send messages to the table, and double-click the nunit.exe-shortcut icon on the table to go to The Nunit tool running interface,
3. Click "Open Project Ctrl + O" in the drop-down list of File in the menu and findTestAnd add the objective. dllfile (.exe file) to NUnit.
4. It is the post-load display. Run the test code directly with the unit price Run button.
5. example of successful use case testing:
6. If the test fails, as shown in the figure below:
7. Ignore use cases. If no error use case exists:
8. Hybrid use cases (ignore use cases + error use cases ):
ReSharper unit test
1. The following is a class library to be tested and an Nunit test unit.
2. Right-click the Nunit test unit and select Run Nunit tests.
3. After the selection, the following page is displayed:
The use of this interface is similar to that of the Nunit Ui. At the same time, you may see a Dubug Nunit Tests option. Its function is similar to code debugging. You can debug the test case call process step by step and remember to break points.
Note:
This package needs to be introduced in the test unit. The package source can be found in the Nunit tool you downloaded.
Learning materials:
Http://www.cnblogs.com/kim01/default.aspx? Page = 3 this is a translation of the chapters on the official website. Of course, you can go to the official website to view them.
Test items: