-Use nunit for unit testing in. NET Programming

Source: Internet
Author: User
Address: http://www.microsoft.com/china/community/Column/59.mspx

1. What is unit test:

In Program There are many tests in the design process. unit testing is only one of them. unit testing cannot guarantee that the program is perfect. However, in all tests, unit testing is the first step, it is also the most important link. 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.

2. Test first: according to the theory of extreme programming (XP), writing and testing 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 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 );
}
}

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.

3. As required, we have written the following code:

Public   Class Ticket
{
Private   Int Amount;
Public   Int Amount
{
Get
{
Return Amount;
}
}

Public VoidAdd (IntNum)
{
}
}

4. Preliminary Test

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 to see the following figure (Omitted ):

The red color indicates that the test was not successful, but it was expected.

5. Complete the code

  Public   Void Add ( Int Num)
{
Amount + = Num;
}

6. Save and re-compile.

Switch to nunit and click run. You can see that:

The add method has changed to green.

 

 

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.