NUnit. the unit test framework written by NET. It is a member of the xUnit system. In the xUnit system, there are also JUnit for Java and CPPUnit for C ++, at the beginning, most NUnit and xUnit systems adopt the same approach. They only convert Smalltalk or Java versions, but in. after NET2.0, it added some special practices. The official website of NUnit is: http://www.nunit.org/ The latest version is 2.6.2. Each NUnit download and installation version provides two download modes: the installation file and the installation-free mode, which are *. msi format and *. zip format. The former needs to be installed before use, and will create some shortcuts during the installation process and register NUnit dll to GAC, so that the NUnit dll will be added when writing the NUnit test class later.. Net Framework. If the downloaded zip file is used, the shortcut is not created and the dll is registered. when compiling the unit test class, you must manually specify the NUnit dll path. NUnit can be run in three ways: Command Line and graphical user interface. Take the NUnit2.5.10 installed on Zhou Gong's current computer as an example. The installation path is C: \ Program Files (x86) \ NUnit 2.5.10, which has three directories: bin, doc, and samples. Under the doc directory is the software documentation (English), and under the samples directory is some sample code. If the installation-free mode is used, run the files in the bin directory to run NUnit. The files in the bin directory are net-1.1 and net-2.0, respectively.. net. The following describes how to start NUnit in different ways: Run nunit-console.exe in command line mode. Graphical user interface mode: Run nunit.exe. Parallel (parallel) mode: Run pnunit-launcher.exe. Note :. net2.0 NUnit is compiled using the/platform: anycpu parameter. We know that this result is a 32-bit program compiled by JIT on x86 systems, on x64 systems, JIT is compiled into 64-bit programs. If NUnit is used to test a 32-bit program on the x64 system, the problem may occur. To avoid this problem, you can use nunit-agent-x86.exe/nunit-x86.exe to test because/platform: x86 is used as the compilation parameter during compilation. Is the GUI for running NUnit: NUnit's common Attribute tags are all attributes that can be used as classes or methods. They are all System. the direct or indirect subclass of the Attribute class, which has the following: Category: used to classify the test. On the main interface, you can see the Tests/Categories tabs. If you mark the Category attribute for the method, you can see it on the Categories tab. Combinatorial: used to Test possible combinations in future testing. For example, the following code: [Test, Combinatorial] public void MyTest ([Values (1, 2, 3)] int x, [Values ("A", "B")] string s) {string value = x + s; Assert. greater (2, value. length);} The test will actually test 6 cases: MyTest (1, "A")/MyTest (1, "B")/MyTest (2, "") /MyTest (2, "B")/MyTest (3, "A")/MyTest (3, "B "). Culture: Set the language environment during the test. This is useful in some language-sensitive scenarios. For example, the strings obtained by DateTime. ToString () in different language environments are different. Description: Specifies the Description of the test. If you select to generate an XML file for the test result, the Description is displayed in the XML file. ExpectedException: indicates that an Exception will be thrown during the test. Explicit: If the tested class or method uses this Attribute, this class or method must be selected on the interface for execution when NUnit testing with GUI is used. Explicit: ignore a test class or method. Maxtime: the maximum number of milliseconds to run the test method. If the execution time of a program exceeds the specified value, the test fails. Random: used to specify how to randomly generate parameters for testing. The following code: [Test] public void TestDemo1 ([Values (1, 2, 3)] int x, [Random (-10, 10, 2)] int y) {Assert. greater (x + y, 0);} indicates that the TestDemo1 method will generate 6 tests, 1, 2, 3 is used as the value of parameter x and the random number y between-10 and 10 respectively to form 6 tests. Range: Specifies the parameter method. The following method is used: [Test] public void TestDemo2 ([Range (0, 11, 4)] int x) {Assert. areEqual (x % 3, 0);} indicates that the increment starts from 0. The step size is 4 and is not greater than 11. Repeat: Number of times the test will be repeated. RequiresMTA: multi-threaded apartment ). RequiresSTA: indicates that a single-threaded apartment is required for testing ). SetUp: Initialize each method before it starts. Before NUnit 2.5, each class must have only one instance method with the SetUp attribute. However, after NUnit 2.5, there is no limit on the number of times and the instance method. TearDown: opposite to SetUp, it is the method executed after each test method is executed. Before NUnit 2.5, each class must have only one instance method with the SetUp attribute. However, after NUnit 2.5, there is no limit on the number of times and the instance method. Test: used to mark the method to be tested. It can only be used to mark instance methods before NUnit 2.5, and can be used to mark static methods after NUnit 2.5. TestCase: The marking method has parameters and provides the parameters required for testing. The following code: [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);} will perform three tests, equivalent to: [Test] public void DivideTest () {Assert. areEqual (4,12/3);} [Test] public void DivideTest () {Assert. areEqual (6, 12/2);} [Test] public void DivideTest () {Assert. areEqual (/4);} TestFixture: indicates that a class may have [Test]/[SetUp]/[TearDown] methods, but this class cannot be abstract. Class. TestFixtureSetUp: indicates the method that is executed before all test methods in the class are executed. Before NUnit 2.5, this tag can only be used by one instance method at most in the class. After NUnit 2.5, multiple methods can be marked, and instance methods are not limited to static methods. TestFixtureTearDown: indicates the method to be executed after all the test methods in the class are executed. Before NUnit 2.5, this tag can only be used by one instance method at most in the class. After NUnit 2.5, multiple methods can be marked, and instance methods are not limited to static methods. Timeout: Mark the maximum execution time of the method to be tested. If it exceeds the specified time, the execution will be canceled and marked as a test failure. Values: a series of parameters marked as test methods. The previous code examples are useful. NUnit Assertions are the core of all xUnit-based unit test series. NUnit provides rich Assertions through the NUnit. Framework. Assert class. Specifically, NUnit provides a total of 11 categories of assertions: Equality Asserts: Used to Assert whether an object is equal, mainly manifested in the overloading of two methods: Assert. areEqual () and Assert. areNotEqual () is a type of overload. The overload parameters include common basic numeric types (such as int, float, and double) and reference types (such as object as parameters ). identity Asserts: used to determine whether an object of the reference type is an asserted object of the same reference and whether the asserted object exists in a collection, such as Assert. areSame, Assert. areNotSame and Assert. contains. Condition Asserts: Used to Assert certain conditions, such as Assert. isTrue, Assert. true, Assert. isFalse, Assert. false, Assert. isNull, Assert. null, Assert. isNotNull, Assert. notNull, Assert. isNaN, Assert. isEmpty and Assert. isNotEmpty. Comparisons Asserts: used for Assert between values and types that implement the IComparable interface. greater (Greater than), Assert. greaterOrEqual (greater than or equal to), Assert. less than or equal to Assert. lessOrEqual (less than or equal ). Type Asserts: used for determining between types, such as determining whether an instance is of a certain Type or inheriting from a certain Type, such as Assert. isInstanceOfType, Assert. isNotInstanceOfType, Assert. isAssignableFrom, Assert. isNotAssignableFrom. A generic method is added after NUnit 2.5, such as Assert. isInstanceOf <T>, Assert. isNotInstanceOf <T>, Assert. isAssignableFrom <T>, Assert. isNotAssignableFrom <T> .. Exception Asserts: Exception assertions, such as Assert. Throws/Assert. Throws <T>, Assert. DoesNotThrow, and Assert. Catch/Assert. Catch <T>. Utility Methods: Used to precisely control the test process. There are four Methods in total: Assert. Pass, Assert. Fail, Assert. Ignore, and Assert. Inconclusive. Assert. pass and Assert. fail is the opposite. The former means that the test will be terminated immediately and the test result will be marked as a successful pass, and the latter means to terminate the test immediately and mark the test result as a test failure. Assert. Ignore indicates ignoring the test. This tag can be used to identify the test method or test class. StringAssert: used for string assertions. The methods provided include StringAssert. Contains, StringAssert. StartsWith, StringAssert. EndsWith, StringAssert. AreEqualIgnoringCase, and StringAssert. IsMatch. CollectionAssert: the methods provided for collection assertions include CollectionAssert. allItemsAreInstancesOfType and CollectionAssert. allItemsAreNotNull, CollectionAssert. allItemsAreUnique and CollectionAssert. areEqual, CollectionAssert. areEquivalent, CollectionAssert. areNotEqual, CollectionAssert. areNotEquivalent, CollectionAssert. contains, CollectionAssert. doesNotContain, CollectionAssert. isSubsetOf, CollectionAssert. isNotSubsetOf, CollectionAsser T. IsEmpty, CollectionAssert. IsNotEmpty, and CollectionAssert. IsOrdered. FileAssert: used for file-Related assertions. It mainly provides two methods: FileAssert. AreEqual and FileAssert. AreNotEqual. DirectoryAssert: used for folder assertions. The following methods are provided: DirectoryAssert. AreEqual, DirectoryAssert. assert, DirectoryAssert. IsEmpty, DirectoryAssert. IsNotEmpty, DirectoryAssert. IsWithin, and DirectoryAssert. Assert. The first time NUnit is used to open NUnit, it will be a blank interface, as shown in: first we need to create an NUnit project, click [File]-> [New Project]. A dialog box for saving the NUnit Project is displayed. Select the appropriate path and enter the appropriate name (note that the File suffix is. nunit), and then click the Save button to create an NUnit test project. We can open this project again later. At this time, this NUnit project does not contain any unit test cases. We need to create a project that contains the test cases. Open Visual Studio and create a class library project (in a real project, we usually add a class library project to the current solution to solve the dll reference problem). Then we need to add NUnit references, this depends on whether we use the installation method or the installation-free method. Generally, we only need to add the nunit. framework (the corresponding dll is unit. framework. dll) reference is enough. The sample code used by Zhou Gong is as follows: using System; using System. collections. generic; using System. linq; using System. text; using NUnit. framework; namespace UnitTestDemo {[TestFixture] public class NUnitTestDemo {private IList <int> intList = new List <int> (); [SetUp] [Category ("NA")] public void BeforeTest () {Console. writeLine ("BeforeTest");} [TestFixtureSetUp] [Category ("NA")] public void BeforeAllTests () {Console. WriteLine ("BeforeAllTests");} [TearDown] [Category ("NA")] public void AfterTest () {Console. writeLine ("AfterTest");} [TestFixtureTearDown] [Category ("NA")] public void AfterAllTests () {Console. writeLine ("AfterAllTests");} [Test] [Category ("NA")] public void Test1 () {Console. writeLine ("Test1");} [Test] [Category ("NA")] public void Test2 () {Console. writeLine ("Test2");} [Test] public void Tes TFloat () {float value = 0.999999999999999999999999999999f; // value = 0.9999999999999999999999999999; Console. writeLine ("float value:" + value); Assert. areEqual (value, 1f); Console. writeLine ("TestFloat");} [Test] public void TestDouble () {double value = 0.999999999999999999999999999999d; Console. writeLine ("double value:" + value); Assert. areEqual (value, 1d); Console. writeLine ("Test2");} [Test] publ Ic void TestDecimal () {decimal value = 0.9999999999999999999999999999 M; Console. writeLine ("decimal value:" + value); Assert. areEqual (value, 1 M); Console. writeLine ("Test2");} [Test, Repeat (3)] public void TestIntList2 () {Assert. areEqual (0, intList. count);} [Test] public void TestIntList1 () {intList. add (1); Assert. areEqual (1, intList. count);} [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);} [Test, Combinatorial, Description ("This is used for show Combinatorial")] public void MyTest ([Values (1, 2, 3)] int x, [Values ("A", "B")] string s) {string value = x + s; Assert. greater (2, value. length);} [Test] public void TestDemo1 ([Values (1, 2, 3)] int x, [Random (-10, 10, 2)] int y) {Assert. greater (x + y, 0 );} [Test] public void TestDemo2 ([Range (0, 11, 4)] int x) {Assert. AreEqual (x % 3, 0) ;}} compile the project to generate the dll. You can click [Project]-> [Add Assembly...] On the NUnit main interface. to add the compiled dll. After loading, the interface is shown as follows: Click the [Run] button on the interface to start the test. Note that this method is used to test all the test methods. If you only want to test a few methods, you can select the check box before the check box (by default, the check box does not appear, click [Tools]-> [Setting] to open the Setting page, click [Tree Display] under [GUI], and select "Show CheckBoxes ). If we only want to test a method separately, it is simpler-simply double-click the test method. Sometimes we will use some configuration information in the config file during the test, such as in the app. config/web. config stores the database connection string information and its configuration information, so that the app can be read during NUnit testing. config/web. the configuration information saved in config. We need to configure NUnit. For demonstration, we have the following information: Project name: UnitTestDemo Project location: D: \ BlogCode \ UnitTestDemo \ Project compilation mode (Debug/Release ): to demonstrate how to Test the data saved in the config file, Debug has compiled three Test cases based on the Code. The Code is as follows: [Test] public void Test0_BKJIABlog () {StringAssert. areEqualIgnoringCase (ConfigurationManager. appSettings ["51 ctoBlog"]," http://zhoufoxcn.blog.51cto.com ");} [Test] public void Test0_CSDNBlog () {StringAssert. AreEqualIgnoringCase (ConfigurationManager. deleettings [" CSDNBlog "]," http://blog.csdn.net/zhoufoxcn ");} [Test] public void Test0_SinaWeiBo () {StringAssert. AreEqualIgnoringCase (ConfigurationManager. deleettings [" SinaWeiBo "]," http://weibo.com/zhoufoxcn ");} At the same time, add the following data to the ettings node of the app. config file: <ettings> <add key =" 51 ctoBlog "value =" http://zhoufoxcn.blog.51cto.com "/> <Add key =" CSDNBlog "value =" http://blog.csdn.net/zhoufoxcn "/> <Add key =" SinaWeiBo "value =" http://weibo.com/zhoufoxcn "/> </AppSettings> if you do not set NUnit, we will get the error result, as shown in: At this time, we can follow the steps below to configure, click [Project]-[Edit...] open the following interface: In the interface, set ApplicationBase to the path of the dll to be tested, in this example: D: \ BlogCode \ UnitTestDemo \ bin \ Debug (note that NUnit is automatically changed to the relative path if the full path is copied to the text box), because the current project is a class library project named UnitTestDemo, therefore, the corresponding config file name is UnitTestDemo. dll. config, fill it in the text box behind the Configuration File Name, and then click the [Run] button again to see that the test is successful. Summary as a member of the xUnit system, NUnit has indeed brought a lot of convenience to. Net developers for unit testing. In the early days, we used NUnit for unit testing. But there are also some shortcomings, such as: 1. in the xUnit system, JUnit generates an instance when testing each method, while in NUnit, A TestFixture generates only one instance, in this way, changes to the instance data in the unit test class may affect other test methods (this is not the case if an instance is generated every time, like JUnit ). 2. in the early days, most people thought that, like JUnit, [SetUp] and [TearDown] would only be executed once before and after all tests, the actual situation is that each test is executed once before and after the test. To achieve the effect of [SetUp] and [TearDown] In JUnit, only the attributes TestFixtureSetUp and TestFixtureTearDown can be added. In addition, there are some shortcomings and shortcomings.