Today, I accidentally saw nunit, which is the unit test tool in DOTNET. I have heard of the unit test tool JUnit in Java and thought about it. net should also exist, but it has not been put into action to access relevant information. It seems that this "don't have to go deep into understanding" habit will be changed in the future, or how can it make progress? Haha
The following is an article about how to use nunit.ArticleIs in MicrosoftCommunitySee, here reference.
----------------------------------------------------------
Use nunit to perform unit tests in. NET programming.Http://www.microsoft.com/china/community/Column/59.mspxBy 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. Many articles have made a lot of in-depth analysis on the importance of unit testing. 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 (1, 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. Run nunit's graph testing tool, open the compiled DLL file, and click the "run" button. The following figure is displayed: The red color indicates that the test was not successful, but it was 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 is green, and the merge method is also completed: Public void Merge () {amount-= 1;}. Then, the test result is: Ah, it turned beautiful green. Now everyone has realized 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. The following test screen is displayed: In the Ticket Class, modify the authorization 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: Now, even if we have finished our unit test, we have a basic understanding of how to perform unit test 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.