Basic unit test knowledge I. Definition of unit test Unit Testing is testingProgramA test of a minimum functional unit. This test is atomic, that is It is an inseparable test and is the smallest functional unit (purely for personal understanding, please correct me if there is any error) !). Unit Testing is automated.CodeTo call the method or class to be tested, and then verify that the method or Class Based on This is the definition of unit test described in. Net unit test ART. Ii. unit test features 1. atomicity, that is, testing of only one minimum function, which cannot be separated. 2. Automatic and repeatable 3. Permanent: After being written, it can be used at any time in the future. And anyone can use it. Iii. Unit Test Tools 1. nunit: it should have evolved from the Unit. 2. Visual Studio: You can also create a unit test project in. 3. resharper: it makes it easier to write and test. I prefer the resharper and nuit tools. Iv. unit test demo The purpose of this example is to calculate the total price of two items. A product parent class. The other two classes are computer and book, which are inherited from the product. The tool used for testing is nuit. You need to reference nunit. Framework. dll in the project. The code for these classes is as follows: Product class: 1 Using System; 2 Using System. Collections. Generic; 3 Using System. LINQ; 4 Using System. text; 5 6 Namespace Gaojie. Operator. Domain 7 { 8 Public Class Product 9 { 10 Private Int Price; 11 Public Static Product Operator + (Product producta, product productb) 12 { 13 Product productc = New Product (); 14 15 Productc. Price = producta. Price + Productb. price; 16 17 Return Productc; 18 } 19 20 Public Int Price 21 { 22 Get { Return Price ;} 23 Set {Price = Value ;} 24 } 25 } 26 } Book class: 1 Using System; 2 Using System. Collections. Generic; 3 Using System. LINQ; 4 Using System. text; 5 6 Namespace Gaojie. Operator. Domain 7 { 8 Public Class Book: Product 9 { 10 11 } 12 } Computer: View code 1 Using System; 2 Using System. Collections. Generic; 3 Using System. LINQ; 4 Using System. text; 5 6 Namespace Gaojie. Operator. Domain 7 { 8 Public Class COMPUTER: Product 9 { 10 } 11 } Next, we will write the Test method: 1 Using System; 2 Using System. Collections. Generic; 3 Using System. LINQ; 4 Using System. text; 5 Using Nunit. Framework; 6 Using Gaojie. Operator. domain; 7 8 Namespace Gaojie. Operator. Test 9 { 10 [Testfixture] 11 Class Operatortest 12 { 13 [Test] 14 Public Void Test () 15 { 16 VaR Computer = New Computer (); 17 Computer. Price = 55 ; 18 19 VaR Book = New Book (); 20 Book. Price = 100 ; 21 22 Product = New Product (); 23 Product = computer + Book; 24 25 Assert. areequal (computer. Price + Book. price, product. Price ); 26 } 27 } 28 } Now open Nuit and import the project. Then click the run button to see that the test has passed. V. Summary I personally think this nuit write unit test is better. It can be used to test whether some functions are correct. Of course, we can also use the console, winform, and Asp.net to test whether this function is correct. Appendix:"Assertion Method": Assert. areequal () test whether the specified value is equal. If the value is equal, the test passes; Assert. Inconclusive () indicates an unverified test; Assert. istrue () test whether the specified condition is true. If it is true, the test passes; Assert. isfalse () test whether the specified condition is false. If it is false, the test passes; Assert. isnull () test whether the specified object is a null reference. If it is null, the test passes; Assert. isnotnull () test whether the specified object is not empty. If not, the test passes; |