1
Initial test caseUsing system; using nunit. framework; namespace netshop {// <summary> // test case of the testcls operation // edit by spgoal /// </Summary> [testfixture] public class testcase {public testcase () {} private testcls TC; [setup] public void setup () {Tc = new testcls ();} [test] public void testadd () {assert. areequal (10, TC. add (5, 5); assert. areequal (7, TC. add (3, 4);} [test] public void testsub () {assert. areequal (1, TC. sub (5, 4);} [test] public void testmul () {assert. areequal (10, TC. mul (2, 5);} [test] public void testdiv () {assert. areequal (2, TC. div ));}}}
2
Compiling this test case is not acceptable because testcls
The class is not created, so it is unnecessary to create the class. Check the Code:Using system; namespace netshop {// <summary> // simple example of the four arithmetic operations /// </Summary> public class testcls {public testcls () {}// add public int add (int A, int B) {return 0 ;}// subtract public int sub (int A, int B) {return 0 ;} // multiply public int MUL (int A, int B) {return 0;} // divide public double Div (int A, int B) {return 0 ;}}} the compilation is successful, but all test cases fail because the methods are not implemented. (To load a test case, run the nunit-Gui V2.1 program first, and then select the DLL file in the bin directory of the project directory where the test case is located in file-open .)
3
The following describes the implementation of four functions:Using system; namespace netshop {// <summary> // simple example of the four arithmetic operations /// </Summary> public class testcls {public testcls () {}// add public int add (int A, int B ){
Return A + B;} // Subtract public int sub (int A, int B ){
Return A-B;} // Multiply public int MUL (int A, int B ){
Return a * B;} // Division Public double Div (int A, int B ){
Return A/B;} The test passed!
4
Find test cases that fail the testThink of some test cases that may cause program errors. Do you think of them? If the divisor is zero, modify testdiv test case [test] public void testdiv () {assert. areequal (2, TC. Div ));
Assert. areequal (0, TC. Div (10, 0 ));// Except for 0} Sure enough, run nunit, and the error occurred ^_^ (this person has a problem and laughed when the error occurred-_-B)
Modify the testcls code // Division Public double Div (int A, int B ){
If (B! = 0)
{
Return A/B;
}
Else
{
Return 0;
}} After compilation, run nunit again. All passed!