Author:
James avery
translation:
Lucentoff
Source: http://msdn.microsoft.com/msdnmag/issues/04/07/musthavetools/default.aspx declaration: The copyright belongs to the original author, reproduced please indicate the source!
NUnit
NUNIT is an open source unit test framework established for .NET Framework. NUnit allows you to write a test program in your code to test application-specific features. Unit testing is an excellent test code functionality, which also provides a regression test method for the application. NUNIT provides a framework for the write unit test and provides a graphical interface to run test and view results.
Write a NUNIT test
For example, I want to test the functionality of the HashTable class in the .NET Framework to determine if the two objects can be added to the HashTable and retrieve. First, I want to add a reference to the NUNIT.FRAMEWORK assembly to access the characteristics and methods of NUnit Framework. Next, create a class and mark the TestFixTure feature. This feature allows NUnit to know this class contains NUnit tests: use system;
Using system.collections;
Using nunit.framework;
Namespace NUNITEXAMPLE
{
[TestFixTure]
Public class hashtabletest {
Public hashtabletest () {
}
}
}
Create a method and mark the [TEST] feature to let NUnit know that this method is a test. Then, create a HashTable and add two values, and finally use the assert.Arequal method to see if the same value can be retrieved to havehtable, the code is as follows: [Test]
Public void hashtableaddtest ()
{
Hashtable ht = new hashtable ();
Ht.Add ("Key1", "Value1");
HT.Add ("Key2", "Value2");
Assert.Arequal ("Value1", HT ["Key1"], "Wrong Object Returned!");
Assert.Arequal ("Value2", HT ["Key2"], "Wrong Object Returned!");
}
Although the above test is simple, it is possible to show the function of NUnit. There are many test types and various ASSERT methods in NUnit, which can be used for each part of the test code. To run the test, you need to create a project, open the generated assembly in NUnit, then click the "Run" button, the results are shown in Figure 5. Figure 5 NUNIT test running results When seeing a green status bar, I feel very excited and very kind, because it indicates that the test passed. This simple example shows the ease of use and powerful features of NUnit. Writing can be saved and the unit tests that can be re-run whenever you change the code not only makes it easier to detect code defects and allow you to deliver better applications. NUNIT is an open source project, download URL: http://www.nunit.org/. There is also a good NUnit Visual Studio .NET plugin that allows you to run unit testing directly from the Visual Studio. This plugin can be found at http://sourceforge.net/projects/nunitaddin. To learn more about NUNIT and its application in test-driven development, read the article in MSDN? Magazine in April 2004 "Test-Driven C #: Improve The Design and Flexibility of Your Project with extreme programming techiques" "C # test driver development: use extreme programming technology to improve project design and flexibility"). [
The following is the translator to add, taken from:
http://www.nunit.org/]
NUNIT includes two ways of operation: NUnit-Console.exe and a graphical user interface mode (NUnit-gui.exe).
Console mode: Run in a command line, automatically save the test results as an XML format, allowing reports or results to process. This method is very useful for testing automation and integration into other systems.
Graphical user interface mode: This mode is a Windows Form Application, which shows the test results in a graphical interface. The test cannot run marked as a yellow circle. The test successfully marked as a green circle, and it is marked red if all tests fails.