Learning nunit Testing 1 (simple nunit usage for understanding nunit)

Source: Internet
Author: User
Some documents are required for the computer questions during the interview. (Repost the following information to others)

Objective: To test the DLL or EXE file
Environment: vs2005 + nunit2.2.9
Below is a simple learning process (from simplicity to complexity)

Step 1: create a new class library project in vs2005, add a class, and define a function to take the maximum value of the array. The specific definition is as follows:

public class
class1
{< br> Public int findmax (INT [] ARR)
{< br> int
I;
int max = arr [0];
for (I = 1; I I ++)
{< br> If (ARR [I]> MAX)

{< br> max = arr [I];
}< BR >}< br>
return Max;
}< BR >}

Step 2: Write test code. Add
A new class named class1test. Before writing the code, you need to add an nunit reference to the project. For details, right-click reference and choose add
reference> nunit. and then add a reference: Using
nunit. framework. Of course, the premise for adding a reference is that nunit has been installed. Now we can start code test. [Here I think we should add a test-specific new project in actual applications (mainly test DLL)]

[testfixture]
public class class1test
{< br>
[test]
Public void findmaxtest ()
{< br> int []
arr1 = {1, 3, 10, 4};
int [] arr2 = {3, 10, 4, 1
};
int [] arr3 = {1, 10, 4, 3};
int [] arr4 = {-1,
-3,-5};

Class1 myclass = new class1 ();
Assert. areequal (10,
Myclass. findmax (arr1 ));
Assert. areequal (10,
Myclass. findmax (arr2 ));
Assert. areequal (10,
Myclass. findmax (arr3 ));
Assert. areequal (-1,
Myclass. findmax (arr4 ));
}

}

Let's first parse this class: [testfixture] is an important attribute of nunit. It can be said to be a mark of nunit. It "announces" to the compilation tool: I want to test it. [Test] is generally used before a function, and this function must be of the Public type and has no return value, that is, void.


All right, our classes and test classes have been written (you can also test the function andSource codeWrite it in a file, but I believe that few people will like it ). After compilation, you can get a DLL file.


open nunit, click File> open, and select the DLL file you just compiled. Then, the file is loaded to nunit. On the left of the nunit interface, we can see the test function findmaxtest just compiled, select a project, a file, or a test function, and then click run on the right of the interface. If the green light is displayed, OK, our test passes!

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.