Nunit
Nunit is an open source code unit test framework generated for the. NET Framework. Nunit allows you to write tests in your preferred language to test specific functions of your application. When you write the code for the first time, unit testing is a good way to test the code function. It also provides a regression test method for the application. 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. the areequal method is used 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. The result is displayed. 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 you can 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.
Nunit is an open source project and can be downloaded from a http://www.nunit.org. There is also an excellent nunit Visual Studio. net external program that allows you to run unit tests directly from Visual Studio. You can find it in the http://sourceforge.net/projects/nunitaddin. For more information about nunit and its position in test-driven development, see the article "test-driven C #: improve the design and flexibility of your project with extreme programming techniques "(Msdn?MagazinePublished in April 2004 ).
Go to the. NET programmer's ten essential tools-directory