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 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.

  1. Surround your test class with the compilation directive
  2. Reference the NUnit.Framework and include it in a using statement
  3. Decorate your test class with the [TestFixture] attribute (NUnit looks for these attributes)
  4. Decorate your test method with the [Test] attribute and make sure they are declared as void
  5. Use one or more Assert statements to look for valid conditions (or invalid)
  6. Go ahead and give it a whirl in your code.

    SqlDatabaseFixture<FONT color=#000000 size=2><FONT color=#0000ff size=2>#if UNIT_TESTS
    <FONT color=#0000ff size=2>using System.Data;
    <FONT color=#0000ff size=2>using System.Data.SqlClient;
    <FONT color=#0000ff size=2>using Microsoft.Practices.EnterpriseLibrary.Configuration;
    <FONT color=#0000ff size=2>using Microsoft.Practices.EnterpriseLibrary.Data.Tests;
    <FONT color=#0000ff size=2>using NUnit.Framework;

    <FONT color=#0000ff size=2>namespace Microsoft.Practices.EnterpriseLibrary.Data.Sql.Tests
    <FONT color=#0000ff size=2>public class SqlDatabaseFixture
        <FONT color=#0000ff size=2>private static ConfigurationContext context = <FONT color=#0000ff size=2>new TestConfigurationContext();

        <FONT color=#0000ff size=2>public void ConnectionTest()
        SqlDatabase sqlDatabase = <FONT color=#0000ff size=2>new SqlDatabase();
        sqlDatabase.ConfigurationName = "NewDatabase";
        sqlDatabase.Initialize(<FONT color=#0000ff size=2>new DatabaseConfigurationView(context));

        IDbConnection connection = sqlDatabase.GetConnection();
        Assert.IsTrue(connection <FONT color=#0000ff size=2>is SqlConnection);
        DBCommandWrapper cmd = sqlDatabase.GetSqlStringCommandWrapper("Select * from Region");
        cmd.CommandTimeout = 60;
        Assert.AreEqual(cmd.CommandTimeout, 60);

    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>