Use nunit for unit testing in. NET Programming
Introduction: for example, an event that may happen to you will be closer to reality. Fortunately, we now have a very common task for programmers: You go to work on the first day of today, your project manager will give you a bunch of non-thick documents, telling you that today's task is to write one 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. To put it simply, unit testing is to test whether the code writer has produced the expected results based on the methods it imagined. 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 ticket = new Ticket();ticket.Add(100);Assert.AreEqual(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 Sell(){Ticket ticket = new Ticket();ticket.Add(100);ticket.Sell();ticket.Sell();ticket.Sell();Assert.AreEqual(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 Sell(){}}
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 ticket = new Ticket();ticket.Add(3);ticket.Sell();ticket.Sell();ticket.Sell();ticket.Sell();}
[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. S