Use nunit2.1 to implement. Net test-driven development (TDD)

Source: Internet
Author: User

Use nunit2.1 to implement. Net test-driven development (TDD)Spgoal (original)

The example below is very simple. It is to implement the four arithmetic operations of two integers. TDD advocates testing first, that is, writing test cases and then running Code I just got an nunit2.1. I can't wait to try it ...... 1 Initial test case Using 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. (The method for loading test cases is: first run nunit-Gui V2.1 Program In the menu file-open-select the DLL file under the bin directory of the project directory where the test case is located .)

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 test Think 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! 5 Summary This is just a simple example. I tried it because I used to use JUnit. net nunit is easy to use, and it is really not bad :) test priority is only part of the content of test-driven development, as well as restructuring and other steps, so some of the questions in this article are not real-name, please forgive me. 6. Learn more about nunit: http://www.nunit.org/files/nunit-v21/nunit-v2.1.4.msi. Article Are: [1] nunit Cookbook (. NET unit test tool) http://www.csdn.net/develop/Read_Article.asp? Id = 14908 [2] using unit test tool nunit http://www.csdn.net/develop/Read_Article.asp in. NET environment? Id = 22482 [3] nunit Quick Start http://www.csdn.net/develop/Read_Article.asp? Id = 23530 http://www.csdn.net/develop/Read_Article.asp? Id = 23531 [4] nunit plug-in http://www.csdn.net/develop/Read_Article.asp on vs. NET 2003? Id = 26568

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.