1. Prepare testdriven and nunit.
Unit TestProgramPersonnel is basically a required skill. The phrase "the treasure of a thousand miles and the ant nest" is also applicable to programmers. One Daniel said, "dummies can write programs that make machines understand, and real smart people can write programs that make people understand ". Unit Testing can significantly improve the quality of your programs. When a project becomes very large, good unit testing can also improve the quality of the project. Of course, it can also bring people a sense of accomplishment. Okay, let's get started with unit testing.
Download testdriven: http://www.testdriven.net/default.aspx
Download nunit: http://www.nunit.org/index.php? P = download
Nunit is well known and has powerful functions. Although nunit supports GUI and console, It is not intuitive enough to use it. We have to switch the window constantly to see the test results. Testdriven is very convenient. It is well integrated with Visual Studio. net2003 and 2005, so it can be tested on demand. It can greatly improve the efficiency of our testing. After testdriven is installed, you can see a small icon below.
We can test the entire test file and a single function, which is very convenient. The following describes the attributes and parameters supported by testdriven.
2 Some Important attributes supported by testdriven
Testdriven can support most nunit-supported attributes, but some attributes cannot. In fact, for unit testing, you only need to know more than 10 commonly used attributes to perform a good test. The following describes the most common and important attributes.
Before starting the test, remember to reference the nunit. Framework DLL, and then
Using Nunit. Framework;
2.1 [testfixture] attributes
This attribute is usually used to modify the test class, indicating that this class is used for testing. It is generally placed on the class declaration, as shown below
[Testfixture]
// This class is used to execute unit tests.
Public Class Testsimplecalculator
{
// Something
}
2.2 [testfixturesetup] attributes
This attribute is usually used to modify a method, indicating that this method runs before all test methods, similar to constructors. It is useful to initialize some objects.
[Testfixture]
Public Class Unittestdemo
{
Simplecalculator mymath;
// Run before all test methods are run.
[Testfixturesetup]
Public Void Initfixture ()
{
Mymath = New Simplecalculator ();
}
}
2.3 [testfixtureteardown] attributes
This attribute is also used to modify the method. It runs after all the test methods are run. You can use it to release some resources.
[Testfixture]
Public Class Unittestdemo
{
Simplecalculator mymath;
// Run after all test methods are run
[Testfixtureteardown]
Public Void Initfixture ()
{
// Release some resources
Mymath. Dispose ();
}
}
2.4 [setup] Properties
This attribute is used to modify the method, indicating that it will run before each test method is run. You can use it to reset some variables, so that each method has a good initial value before running.
[Testfixture]
Public Class Testsimplecalculator
{
Simplecalculator mymath;
Private Double A;
Private Double B;
//Run before any test method is run. It can be used to reset some variables.
[Setup]
Public VoidInit ()
{
A= 3.0;
B= 5.0;
}
}
2.5 [teardown] attributes
This attribute is usually used to modify the method, indicating that this method will run once after each test method is run. It can be used to clean up some variables or environments.
[Testfixture]
Public Class Testsimplecalculator
{
Simplecalculator mymath;
Stringbuilder Sb;
[testfixturesetup]
Public void initfixture ()
{< br> mymath = New simplecalculator ();
Sb = New stringbuilder ();
}
//Run after each test method is completed. It can be used to clear some temporary variables.
[Teardown]
Public VoidTeardown ()
{
SB. Remove (0, SB. Length );
}
}
2.6 [test] attributes
This attribute is most useful because it indicates that this is a test method.
[Testfixture]
Public Class Testsimplecalculator
{
Simplecalculator mymath;
Private Double A; // A = 3.0
Private Double B; // B = 5.0
// This is a test method
[Test]
Public Void Add ()
{
Assert. areequal (, 3.0 ); // Returns true
Assert. areequal (B, 5.0 ); // Returns true
A = Mymath. Add (A, B );
Assert. areequal (, 7.0 , " The specified CT result is 7, and the actual result is 8 " ); // False is returned and an error message is printed.
}
}
2.7 [expectedexception (typeof (onesupportedexception)] attribute
This attribute is actually very useful. It indicates that this function will throw an expected exception. Exception Handling is inevitable in a project. If the exception handling mechanism is not good, it will bring considerable confusion to the program. Maybe your program is full of try and catch, but it does not catch the exception you want. Chaos is a disaster for programmers.
[Test]
[Expectedexception ( Typeof (Invalidoperationexception)]
Public Void Expectanexception ()
{
Throw New Invalidcastexception (); // This part throws unexpected exceptions, so the test method fails.
}
2.8 [ignore ("name")] attributes
This attribute is also very useful. It indicates that this test method will be ignored. Maybe yourCodeAfter some upgrades, the previous test methods are no longer important, but you still want to keep them. You can mark them as ignore and put them in a single file or region for archival purposes.
[Test]
[Ignore ( " Ignored Test " )]
[Expectedexception ( Typeof (Invalidoperationexception)]
Public Void Ignoredtest ()
{
Throw New Exception (); // If you can run this test method, this method will not pass the test, but it is ignored now.
}
2.9 [Platform ("supportedplatform")] attributes
This property is also quite practical, it indicates that this test method will run on the specified platform. As we all know, there are several versions of. NET Framework and various versions of Windows. Different versions have different support for some class libraries or APIs. For example, some WMI query statements cannot pass the test on win2000. Some Class Libraries cannot be found in. net1.1. If a platform is specified, everything becomes well-organized.
[Test]
[Platform ( " Net-1.1 " )]
// For more supported platforms, see nunit documentation
Public Void Dotnetoneonetests ()
{
Assert. areequal ( " This case run on. net1.1 " , " This method will not be executed " ); // This test method runs only on the. net1.1 platform.
}
2.10 [category ("nameofcategory")] attributes
This attribute is also good. But cannot be used in testdriven. It indicates that we can classify some tests into a category. We can name this category and specify whether to test the category. Suppose you have a function that takes a long time to run, and you certainly don't want to run it every time. Then you can classify it into a certain category and exclude it from the test scope in the nunit GUI.
[Test]
[Category ( " Long " )]
// This test method belongs to the long type. We can choose whether to run this type of test method in the nunit GUI, but testdriven.net cannot use this property.
Public Void Verylongtest ()
{
Assert. areequal ( " This test will consum a very long time " , " No, it will be completed in 0.1 seconds " );
}
2.11 [explicit] attributes
This property is similar to ignore, but it is also different. If this attribute is specified, it will not run during testing. However, if you specify it (for example, place the mouse over this method and select runtest), the test method will run. It is also very useful. It is a good choice for some tests that you want to avoid temporarily.
[Test, explicit]
Public Void Explicittest ()
{
Assert. areequal ( 1 , 2 ); // This test method is automatically ignored unless we manually select it in the nunit GUI or place the cursor over it and then run testdriven.net.
}
3. Conclusion
In fact, nunit has more powerful functions than I listed above. However, it is sufficient for programmers to understand some common attributes for unit tests. Testdriven supports most attributes and is very convenient to use. In addition, testdriven can also provide the ncover tool for analysis. You can create a project for your application, and create a project for your test code. The test and development are synchronized. A good unit test can certainly improve the program quality, and may not delay too much time and delay the project progress. Unit Testing is so simple and useful. I hope these attributes will help you and improve your program level.