Microsoft's Enterprise Library (Ent Lib) was developed using the oft spoken of Test Driven Development methodology (TDD). But of course if you are new to TDD or are aware of it but have not used it yet, you'll want to download and install nUnit to try it out. You can find NUnit here http://www.nunit.org and then navigate to their download page. All of the Ent Lib code has sub folders labelled "Test" which contain the test code that NUnit looks for. NUnit is pretty easy to set up and try out, especially if you load one of the QuickStarts that come with Ent Lib into Visual Studio.NET. Give it a shot and check it out. Its a pretty cool concept and in practical real world scenarios, it has helped my projects come together more quickly. It has been especially helpful on projects with large numbers of developers as it reduces the number of errors in unit testing. And when errors are present, it is easy for other developers to fix them. I'll talk a lot more about TDD and Ent Lib in specifics in future blogs.
Once you find NUnit, you can start setting up your own classes that can be used by it. Setting up a class for the TDD model is quite simple. Below I have shown part of the SqlDatabaseFixture class from the Ent Lib DAAB (located in the Sql\Tests folder). You really only have to follow a few steps within the code.
- Surround your test class with the compilation directive
- Reference the NUnit.Framework and include it in a using statement
- Decorate your test class with the [TestFixture] attribute (NUnit looks for these attributes)
- Decorate your test method with the [Test] attribute and make sure they are declared as void
- Use one or more Assert statements to look for valid conditions (or invalid) Go ahead and give it a whirl in your code.
Here is the image of NUnit running the test fixture SqlDatabaseFixture for Ent Lib's DAAB QuickStart.
<IMG src="/photos/jpapa/images/60947/original.aspx" border=0>