Welcome Guest!
Create Account | Login
Locator+ Code:

Search:
FTPOnline Channels Conferences Resources Hot Topics Partner Sites Magazines About FTP RSS 2.0 Feed

Click here to receive your FREE subscription to Visual Studio Magazine

email article
printer friendly
more resources

Test .NET Code Automatically With NUnit
NUnit helps you unit test your code quickly and easily. And it's free.
by Bill Wagner

Posted July 22, 2002

ADVERTISEMENT
NUnit is a set of classes you can use to create and execute automatic unit tests on your .NET classes. To supplement information I cover here, go to the NUnit Web site for whitepapers and articles about the value of unit testing (see Resources).

You can download the built classes for NUnit, or the source. I prefer the source route. When I went to the site, the latest version was beta 2, and there were changes I needed to make so NUnit would build and run under RC1.

First, you need to generate your own key file for NUnit because the source does not include a key file. Generate the correct keyfile type like this:

sn -k NUnit.key

Next, you need to change several prototypes because the NUnit project contains the old version of Dispose. The new prototype should be:

virtual void Dispose (bool Disposing);

Now you can build NUnit, run it as is, and use the sample tests. Run the NUnitGUI project to load the tests, and select the Browse button to load an assembly. To use the samples, select the SampleMoney.dll assembly. Once you have loaded an assembly, the TypeName box shows you the types of assemblies that already have defined tests. Click on the Run button to execute all the tests, and view the results in the bottom of the window.

Running the samples is useful, but you probably want to know how to create and execute tests with your own code. To illustrate how to use NUnit, I'll write one test suite for the source count utility I wrote earlier. NUnit uses reflection to find test methods in your unit test code. To create a test suite, simply create a class that executes tests for you. You must derive this class from TestCase, which is part of NUnit. Any test method in your new class must be public and start with "test". The test method also should contain a void return type and take no parameters. Here are the two test methods for my suite:

public void testComments () {
	Assert ("Checking Comment lines", 
		testObj.SingleLineComments == 2);
}

public void testDocs () {
	Assert 
	("Testing Documentation lines", 
	testObj.DocumentationComments == 3);
}
Back to top













Java Pro | Visual Studio Magazine | Windows Server System Magazine
.NET Magazine | Enterprise Architect | XML & Web Services Magazine
VSLive! | Thunder Lizard Events | Discussions | Newsletters | FTPOnline Home