Nunit Unit Testing

Source: Internet
Author: User
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 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.
In the add method, we define a ticket object and add 100 tickets to it. Then we can use:
Assertion. assertequals (100, ticket. amount );
To test whether the amount attribute of ticket is 100.
Next, we will add a method for testing the token to tickettest:
[Test]
Public void Merge ()
{
Ticket = new ticket ();
Ticket. Add (1, 100 );
Ticket. Digest ();
Ticket. Digest ();
Ticket. Digest ();
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 add method is also completed:
Public void Merge ()
{
Amount-= 1;
}
Test again and the 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 as above:
[Test]
[Expectedexception (typeof (exception)]
Public void excpetiontesting ()
{
Ticket = new ticket ();
Ticket. Add (3 );
Ticket. Digest ();
Ticket. Digest ();
Ticket. Digest ();
Ticket. Digest ();
}

[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, let's modify the merge method to make it:
Public void Merge ()
{
If (amount-1 <0)
Throw new exception ("amount cannot be 0 ");
Amount-= 1;
}
Compile and test. 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.

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.