Test-driven development (TDD) of. Net with nunit2.1

Source: Internet
Author: User
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!

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.