Http://www.pconline.com.cn/pcedu/empolder/net/cs/0509/699905_3.html
Nunit
Nunit is open for. NET Framework generation Source code Unit test framework. Nunit enables you to write tests in your preferred language to test applications. Program . When you write Code Unit testing is a good way to test the code function. It also provides a regression testing method for applications. The nunit application provides a framework for compiling unit tests and a graphical interface for running these tests and viewing results.
Compile nunit Test
As an example, I will test the hashtable class function in the. NET Framework to determine whether two objects can be added and then retrieved. My first step is to add a reference to the nunit. Framework Assembly, which grants me access to nunit framework attributes and methods. Next, I will create a class and mark it with the testfixture property. This attribute enables nunit to know that the class contains nunit test:
Using system;
Using system. collections;
Using nunit. Framework;
Namespace nunitexample
{
[Testfixture]
Public class hashtabletest {
Public hashtabletest (){
}
}
}
Next, I will create a method and mark it with the [test] property so that nunit knows that this method is a test. Then, I will create a hashtable and add two values to it, and then use assert. areequal method to check whether I can retrieve the same value as the value I added to hashtable, as shown in the following code: [test]
Public void hashtableaddtest ()
{
Hashtable ht = new hashtable ();
Ht. Add ("key1", "value1 ");
Ht. Add ("key2", "value2 ");
Assert. areequal ("value1", HT ["key1"], "wrong object returned! ");
Assert. areequal ("value2", HT ["key2"], "wrong object returned! ");
}
This will confirm that I can first add a value to hashtable and then retrieve the corresponding value-this is a simple test, but it can present nunit functionality. There are many test types and various assert methods that can be used to test each part of the code.
To run this test, I need to generate a project, open the generated assembly in the nunit application, and then click the run button. Figure 5 shows the result. When I saw the big green stripe, I felt excited and dizzy, because it made me know that the test has passed. This simple example shows how convenient and powerful nunit and unit testing are. Because you can write a unit test that can be saved and re-run the unit test whenever you change the code, not only can you detect defects in the Code more easily, and ultimately deliver better applications.
Figure 5 nunit
nunit is an open source project: http://www.nunit.org /. There is also an excellent nunit Visual Studio. Net plug-in that allows you to run unit tests directly from Visual Studio. You can find it in http://sourceforge.net/projects/nunitaddin. For more information about nunit and its position in test-driven development, see Article : "Test-driven C #: improve the design and flexibility of your project with extreme programming techniques "