Use nunit for unit testing in. NET Programming)

Source: Internet
Author: User

From: http://www.microsoft.com/china/community/Column/59.mspx

Columns

Use nunit for unit testing in. NET Programming
Lu Yan

Introduction: for example, an event that may happen to you will be closer to the actual situation. Fortunately, we have one Program The engineer seems to be a very common task: When you go to work on the first day of today, your project manager will give you a bunch of not-too-thick documents, telling you that today's task is compiled according to the requirements in the document. net class, probably because the task is not complex, so he looks very casual. It is very special for you to complete the task well today. After you take it, you will quickly skip the project introduction in the previous section, because you know that it is not important to you, it seems like a ticket sales system project. Soon, you found the key point you need to pay attention to: Class requirement instructions. You read it in detail and it doesn't feel complicated. The class name ticket has a read-only int-type public attribute named amount. There are two other methods: the name is volume, the function is to subtract one from amount, indicating that a ticket is sold. Of course, the ticket can be a negative number. If yes, an exception is thrown to indicate the cause. The other is add, which has an int-type parameter. The function is to add the value of this parameter to amount. It may be a result of a ticket receipt. You are not very concerned about it, anyway, this program is very simple. You can hide your inner ecstasy, open the computer, call out the editor, and start preparing to write the program. "Hey, wait". The project manager doesn't know when to switch back. "I want to know how you plan to perform unit testing. What I care about most is this ". "What is unit test? "You turned your head and looked down at the disappointed project manager. What is unit testing: There are many tests in the program design process. unit testing is only one of them. unit testing cannot ensure that the program is perfect, but in all tests, unit testing is the first and most important step. Unit testing is a self-testing task by programmers. Simply put, unit testing is a test. Code Whether the writer performed the execution in the way it imagined produced the expected results. The importance of unit testing already exists. Article After a lot of in-depth analysis, I will not go into details here. Nunit is an automated unit testing framework for net. It helps you easily complete unit testing. Like the well-known JUnit, nunit is a member of the xunit family. It's: http://www.nunit.org. Test first: "what? Write test first? "You must be surprised, right! Write test code first. According to the theory of extreme programming (XP), writing test is the process of designing the software. It is more important than the code that actually completes the function. Write the test first, and then complete the Code. In this way, the day when all the tests pass is the day when the program is completed. First, we will introduce the nunit. Framework. dll file provided by nunit into the project and create a class named tickettest:
 
[Testfixture] public class tickettest {[test] public void add () {Ticket = new ticket (); ticket. add (100); assertion. assertequals (100, ticket. amount );}}
Note that the attribute [testfixture] and [test] must be added as required by nunit. In this way, the test framework can know which classes or methods need to be tested. We defined a ticket object in the add method and added 100 tickets to it. Then we can use assertion. assertequals (100, ticket. amount); to test whether the amount attribute of ticket is indeed 100. Next, we will add a method for testing the token to tickettest:
 
[Test] public void Merge () {Ticket = new ticket (); ticket. add (100); ticket. break (); ticket. break (); ticket. partition (); assertion. assertequals (97, ticket. amount );}
Here, we sold three tickets in one breath after adding 100 tickets, and then checked whether we have 97 remaining tickets. Now, the tests for these two methods have been completed. Let's take a look at the test results and write the following code as required:
 
Public class ticket {private int amount; Public int amount {get {return amount;} public void add (INT num) {} public void Merge (){}}
Note that this code is only used to complete the class structure. The implementation of the method is empty for the moment. Then compile the code into a DLL dynamic connection library file: unittest. dll. We run the nunit graph testing tool, open the compiled DLL file, and click the "run" button to see the following picture: It is very eye-catching Red, indicating that the test was not successful, however, this is what we expected. Next, we will complete our add method implementation code to the ticket class just now: Public void add (INT num) {amount + = num;} Save and re-compile. Switch to nunit and click run. You can see that the add method has turned green, and then the merge method has been completed: Public void Merge () {amount-= 1;} and then test, the result turns into: Ah, it turns into a beautiful green. Now everyone understands the importance of environmental protection. :) Can I submit the task? Wait, don't worry. Another exception is not tested. If our amount is smaller than 0, an exception will occur. How can we test the exception? See. Test exception: Write the test code first, as shown above:
 
[Test] [expectedexception (typeof (exception)] public void excpetiontesting () {Ticket = new ticket (); ticket. add (3); ticket. break (); ticket. break (); ticket. break (); ticket. else ();}
[Expectedexception (typeof (exception)] indicates the exception we want to capture. If no exception is caught, the test fails. The code below is very easy to understand. We have added three tickets but sold four tickets. This is not a stock market. We cannot close the positions in the future. :) Compile and run the program. We can see the following test screen: In the Ticket Class, modify the compile method to make it public void Merge () {If (amount-1 <0) throw new exception ("amount cannot be 0"); amount-= 1;} compile and test again. The result is as follows: Well, here we will complete our unit test, everyone has a basic understanding of how to perform unit tests in C. In addition, nunit is not only for C #. In fact, you can use nunit in any. NET language to test your unit. The methods are the same. Conclusion: unit testing seems a little troublesome, but it provides a security point of view for programmers and gives them more confidence in their own programs, the first security protection network is provided for the application software while reducing the time required for frequent debugging at the development stage. Therefore, unit testing is an important means to improve development efficiency and software quality. Using unint, we can. NET programming process is very convenient for unit testing, its graphical interface and a simple and powerful test framework provides us with a very comfortable and interesting test environment, it makes programmers feel that unit testing is not boring, and it can even become a pleasure after getting used to it. After reading this article, if you are the poor programmer in the introduction, you will be able to easily face your project manager and hand in a reassuring code answer.

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.