How to Use nunit for unit testing

Source: Internet
Author: User

This articleArticleI learned the notes from the book unit test C #, so the content is not original.

Nunit is a free open source (http://www.nunit.org) product that provides a set of test frameworks and a test runProgram(Test runner ).
Note: Test Tunner knows how to find the [test] Method in the classes and classes that have the [testfixture] attribute.


How to install nunit:

Method 1:Download the nunit C # source code and compile it on your own, and install it on your computer;

Method 2:Use the Microsoft Installer (MSI) file.
Note: MSI is only built in Windows 2000 or later, Windows NT or earlier versions. You need to search for "Microsoft Installer redistributable" in www.microsoft.com ".

Use test Tunner in three ways:

1. nunit Gui
It provides an independent nunit graphical tool.
Enable Visual Studio support from the personalized settings in the menu tool/options.

2. nunit command line
You need to configure the path runtime environment.

3. Visual Studio plug-in
There are several plug-ins that can integrate nunit into Visual Studio, such as http://www.mutantdesign.co.uk/nunit-addin/

Simple Example

1. Create a new project of class library type in
Testnunit. csproj

II,CodeClass: Create a code class file named largest. CS

Public   Class CMP
{
Public   Static   Int Largest ( Int [] List)
{
Int Index;
Int Max = Int32.minvalue; // If max = 0, an error occurs when the list element is negative.

// When the list length is 0, that is, when the list array is empty, a runtime exception is thrown.
If (List. Length =   0 )
{
Throw   New Argumentexception ( " Largest: Empty list " );
}

For (Index =   0 ; Index < List. length; index ++ )
{
If (List [Index] > Max) max = List [Index];
}
Return Max;
}
}

Iii. Test class: Create a test class file named testlargest. CS

Note: The test code uses nunit. Framework. Therefore, you must add a reference pointing to nunit. Framework. DLL to compile the code.

Using Nunit. Framework;
[Testfixture]

Public   Class Testlargest
{
[Test]
Public   Void Largestof3 ()
{
Assert. areequal ( 9 , CMP. largest ( New   Int [] { 7 , 8 , 9 })); // Test "9" at the last digit
Assert. areequal ( 9 , CMP. largest ( New   Int [] { 7 , 9 , 8 })); // Test "9" in the middle
Assert. areequal ( 9 , CMP. largest ( New   Int [] { 9 , 7 , 8 })); // Test "9" first
Assert. areequal ( 9 , CMP. largest ( New   Int [] { 9 , 9 , 8 })); // Repeated "9" in the test"
Assert. areequal ( 9 , CMP. largest ( New   Int [] { 9 })); // Only "9" element exists in the test list.

// Negative number in the test list. If the int max of the largest method in CMP is 0, an error is detected.
Assert. areequal ( - 7 , CMP. largest ( New   Int [] { - 9 , - 8 , - 7 }));
}

[Test, expectedexception ( Typeof (Argumentexception)]
Public   Void Testempty ()
{
// Whether to throw an exception when the list array is empty (the length is 0)
CMP. largest ( New   Int [] {});
}
}

Generate a solution (shortcut: Ctrl + Shift + B)

Recommended articles:
C # unit test
Basic unit test knowledge
Use watin to perform unit tests on ASP. NET pages

Additional:
Unit Test path C #-use nunitBook original code download: http://www.pragmaticprogrammer.com/sk/ut/

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.