Simple implementation of. NET Test-driven development (TDD) with NUnit2.1

Source: Internet
Author: User
Tags arithmetic assert mul
Simple implementation of. NET Test-driven development (TDD) with NUnit2.1
The following example is very simple, that is, to achieve the arithmetic of two integers, TDD advocates testing first, that is, first write test cases, and then write the running code, just a NUnit2.1, can't wait to try ...
1 Initial test Cases
Using System;
Using Nunit.framework;
Namespace NetShop
{
<summary>
Arithmetic TESTCLS test Case
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 (10,5));
}
}
}

2 Compiling this test case is, of course, not a pass, because the Testcls class is not established, so the class is created, needless to say, look at the code:
Using System;

Namespace NetShop
{
<summary>
A simple example of arithmetic
</summary>
public class Testcls
{
Public Testcls ()
{
}
Addition
public int Add (int a,int b)
{
return 0;
}
Subtraction
public int Sub (int a,int b)
{
return 0;
}
Multiplication
public int Mul (int a,int b)
{
return 0;
}
Division
public double Div (int a,int b)
{
return 0;
}
}
}

The compilation passes, but all test cases fail because the method inside is not implemented.
(The method to load a test case is to run the Nunit-gui V2.1 program, and then in the menu file-open-select the DLL file in the bin directory of the project directory where the test case resides.) )

3 then write the implementation of the subtraction four functions:
Using System;

Namespace NetShop
{
<summary>
A simple example of arithmetic
</summary>
public class Testcls
{
Public Testcls ()
{
}
Addition
public int Add (int a,int b)
{
return a+b;
}
Subtraction
public int Sub (int a,int b)
{
return a-b;
}
Multiplication
public int Mul (int a,int b)
{
return a*b;
}
Division
public double Div (int a,int b)
{
return a/b;
}
}
}

Then the test passed!


4 Finding test cases that fail the test
Think of it as if you missed some test cases that could make a program go wrong? is a zero divisor, so modify the Testdiv test case
[Test]
public void Testdiv ()
{
Assert.AreEqual (2,TC. Div (10,5));
Assert.AreEqual (0,TC. Div (10,0));//Except in the case of 0
}
Sure enough, run NUnit, a mistake ^_^ (this person has a problem, a mistake also laugh-_-b)



So modify the Testcls class code
Division
public double Div (int a,int b)
{
if (b!=0)
{
return a/b;
}
Else
{
return 0;
}
}
After compiling, then run NUnit, all through!

5 Summary
This is just a simple example, because of the previous use of JUnit feel pretty good, so want to try. NET NUnit is good, and sure enough: the test priority is only a part of the test-driven development, as well as the refactoring and other steps, so the title of this article is somewhat unworthy, please forgive me.

6 Related information
NUnit Download Address: Http://www.nunit.org/files/nunit-v21/Nunit-V2.1.4.msi
Relevant Chinese articles are:
[1] NUnit Cookbook (. NET Unit test tool) http://www.csdn.net/develop/Read_Article.asp?Id=14908
[2] using unit test tools in the. NET Environment NUnit http://www.csdn.net/develop/Read_Article.asp?Id=22482
[3] NUnit QuickStart http://www.csdn.net/develop/Read_Article.asp?Id=23530
http://www.csdn.net/develop/Read_Article.asp?Id=23531
[4] NUnit the plugin on vs.net 2003 http://www.csdn.net/develop/Read_Article.asp?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.