NUnit Use guide-nunit Quick start_ Practical Tips

Source: Internet
Author: User
Tags assert
NUnit is an open source unit test framework that is generated for the. NET Framework. NUnit enables you to test your application's specific functionality by writing tests in your favorite language. When you first write code, unit testing is a good way to test the functionality of your code, and it provides a way to test your application for regression. The NUnit application provides a framework for writing unit tests, as well as a graphical interface for running these tests and viewing the results.
Download Address: Http://sourceforge.net/projects/nunitaddin
Version used in this case: Http://www.cnblogs.com/Files/young18/nunit2.3.rar

NUnit Quick Start

Original document: Http://www.nunit.org
Translation: YOUNG.J
Note: This example is found in the earliest version of NUnit and is not a good example in test-driven development, but it illustrates the most basic way to use NUnit.

Now let's start with our example. Assuming we start writing a banking application, we have a base class-account,account primarily responsible for the increase in funding, revocation and transfer, and the following is the code for that class

1NamespaceBank
2{
3 PublicClassAccount
4{
5PrivateFloatBalance
6PublicvoidDeposit (FloatAmount
7{
8Balance+=Amount
9}
10
11PublicvoidWithdraw (FloatAmount
12{
13Balance-=Amount
14}
15
16PublicvoidTransferfunds (Account destination,FloatAmount
17{     }
18
19PublicFloatBalance
20 {       
              &NBSP Get {  return  balance;}      
        }
n     
a
Before we write a class-accounttest that needs testing, our first test method is Transferfunds
1NamespaceBank
2{
3UsingNunit.framework;
4
5[Testfixture]
6PublicClassAccounttest
7 {
8[Test]
9PublicvoidTransferfunds ()
10{
11Account Source=NewAccount ();
12Source. Deposit (200.00F);
13Account Destination=NewAccount ();
14Destination. Deposit (150.00F);
15Source. Transferfunds (Destination,100.00F);
assert.areequal (250.00F, destination. Balance);
assert.areequal (100.00F, source. Balance);
/ }
}
}
The first thing to do now is to declare that the class has a [Testfixture] property that shows that the class contains the test code (this property can be inherited), that the class must be a public class and that its derivation has no restrictions, and that the class must have a default constructor, of course.
The only one method of this class,-transferfunds, has a [test] property that shows that he is a test method that returns void with no parameters, in which we have to initialize the test object, and the Assert class defines a collection of methods. Use it to detect setting conditions, in our example, we use the AreEqual method to ensure that the transfer of the next two accounts has a correct residual fund (these are some overloaded methods, in this case the version contains the following parameters, the first parameter is the expected value, the second is true),
Compile and run this example, assuming you compile your code for Bank.dll, run the NUnit Gui, select File->open menu item, load the DLL file you just compiled, click Run, and we can see that the test strip turns red-our test fails, in the Errors and failures panels display one side of the information:
Transferfunds:expected <250> but was <150>
This phenomenon is what we expected, the test failed because we did not implement Transferfunds method, now we start to let it work, modify your Transferfunds method as follows:
1public void transferfunds (account destination, float amount)
2{
3 destination. Deposit (amount);
4 Withdraw (amount);
5}
Now we compile the code again and run in the GUI, how can we see the test strip turn green! Test success!
We add some error detection in our account code, set a minimum value for balance. To protect the capital overdraft in turn
1private float minimumbalance = 10.00F;
2public float minimumbalance
3{
4 Get {return minimumbalance;}
5}
Add an exception that indicates overdraft;
1public class Insufficientfundsexception:applicationexception
2{
3}
Add a test method to the Accounttest class
1[Test]
2[ExpectedException (typeof(insufficientfundsexception))]
3PublicvoidTransferwithinsufficientfunds ()
4{
5     account source  =   New  account ();
The  6     source. Deposit ( 200.00F );
 7     account  destination  =   new  account ();
The  8     destination. Deposit ( 150.00F );
The  9     source. Transferfunds (destination,  300.00F );
Ten "
The test property of this method has a [ExpectedException] property, which indicates that the test code expects a certain type of exception, if the exception does not appear in the execution, the car is failed, now compiles the code, launches the NUnit Gui, this is the test strip turns red, Tip Error Message:
Transferwithinsufficentfunds:insufficientfundsexception was expected
Let's reconfigure the account's code, let it throw an exception, and modify the Transferfunds method by following the example below.
1PublicvoidTransferfunds (Account destination,Float  amount)
2 {
3 4 &NBSP;&NBSP;&NBSP;&NBSP; if (balance - amount   minimumbalance)
5          throw   New   Insufficientfundsexception ();
6     withdraw (amount);
7
Compile, run test-the test strip turns green and succeeds, but let's take a look at this code, we just write the error in the transfer operation we can see, now let's write a test to confirm our indeterminate error and add the following test method
1[Test]
2PublicvoidTransferwithinsufficientfundsatomicity ()
3{
4Account Source=NewAccount ();
5Source. Deposit (200.00F);
6Account Destination=NewAccount ();
7Destination. Deposit (150.00F);
8Try
9{
10Source. Transferfunds (Destination,300.00F);
11}
a      catch (insufficientfundsexception expected)
     {
    }
    assert.areequal ( 200.00F , source. Balance);
    assert.areequal ( 150.00F , destination. Balance);
 
   compile run-red test strip, we calculated the wrong 300 yuan, the code shows the correct result is 150 yuan, but the account shows that is 450, then how to fix the error, can add a section of the minimum fund testing before the fund processing? We can do some patching in the catch block, or rely on our managers to fix the object's rules, we need to answer these questions in many ways, but not now, so what can we do in the meantime? Move it out? One of the best ways to ignore it is to add the following attributes to your test method
1 [Test]
2 [ignore" ( decide how to implement transaction  Management )]
3 public   void  transferwithinsufficientfundsatomicity ()
4 {
5 &NBSP;&NBSP;&NBSP;&NBSP; //  code is  the same
6 }
Compile test code-yellow, click "Tests not Run" and we can see the bank. Accounttest.transferwithinsufficientfundsatomicity () is in the Neglected Test list.
The above is a few commonly used simple methods, in turn to explain the use of NUnit, in the future unit, we will explain the use of NUnit in-depth!

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.