1. Create a project
2. Reference nunit. framework and reference the. dll file you want to test.
3. Write a testCode
Let's take my example (vs2005 as an example ):
Everybody needs to install nunit first, what I install is NUnit-Net-2.0 2.2.7, its:Http://www.nunit.org/
(1) simple use case code:
Using system;
Using system. Collections. Generic;
Using system. text;
Using nunit. Framework;
Namespace demonunit
{
[Testfixture]
Public class testunit
{
Private int;
Private int B;
[Setup]
Public void startvalue ()
{
A = 1;
B = 2;
}
[Test]
Public void add ()
{
Int sum = A + B;
Assert. areequal (3, sum );
}
}
}
After running nunit, a green bar appears, proving that the test is successful.
(2) Test the method use case code in your own DLL file:
Using system;
Using system. Collections. Generic;
Using system. text;
Using nunit. Framework;
Namespace testnunit
{
[Testfixture]
Public class nunitstart
{
[Testfixturesetup]
Public void Init ()
{
Dal. sqlhelper. connstring = "Server = (local); uid = sa; Pwd =; database = test ;";
}
[Test]
Public void testgetsubjectdetaillist ()
{
Int num = Dal. Questionnaire. gettypelist (1). count;
Assert. areequal (4, num );
}
}
}
Dal is the dll I want to test. DLL file. sqlhelper is a class in the DLL file. data-related operations are mainly used as third-party components. You can find them online. questionnaire is a class file with the gettypelist (INT) method. The returned result is a collection, Dal. questionnaire. gettypelist (1 ). count is the number of records in the set.
Assert. areequal (4, num); "4" indicates the expected number of records, and num indicates the actual number of records.
This is a bit messy. Please forgive me.