views:

103

answers:

1

Hi All,

MSTest has a [ClassCleanup()] attribute, which needs to be static as far as I can tell. I like to run through after my unit tests have run,and clean up my database. This all works great, however when I go to our build server and use our Nant build script, it seems like the unit tests are run with NUnit. NUnit doesn't seem to like the cleanup method to be static. It therefore ignores my tests in that class. What can I do to remedy this? I prefer to not use [TestCleanUp()] as that is run after each test. Does anyone have any suggestions? I know [TestCleanup()] aids in decoupling, but I really prefer the [ClassCleanup()] in this situation. Here is some example code.

  ////Use ClassCleanup to run code after all tests have run
    [ClassCleanup()]
    public static void MyFacadeTestCleanup()
    {

        UpdateCleanup();

    }


    private static void UpdateCleanup()
    {
        DbCommand dbCommand;
        Database db;

        try
        {

            db = DatabaseFactory.CreateDatabase(TestConstants.DB_NAME);
            int rowsAffected;

            dbCommand = db.GetSqlStringCommand("DELETE FROM tblA WHERE biID=@biID");
            db.AddInParameter(dbCommand, "biID", DbType.Int64, biToDelete);
            rowsAffected = db.ExecuteNonQuery(dbCommand);
            Debug.WriteLineIf(rowsAffected == TestConstants.ONE_ROW, string.Format("biId '{0}' was successfully deleted.", biToDelete));

        } catch (SqlException ex) {



        } finally {
            dbCommand = null;
            db = null;

            biDelete = 0;

        }
    }

Thanks for any pointers and yes i realize I'm not catching anything. I need to get passed this hurdle first.

Cheers, ~ck in San Diego

A: 

Your build server is ignoring your tests because MSTest uses a different set of attributes to specify tests to what NUnit uses. This is more likely the issue you're having, if it's not seeing any of your tests.

For example: MSTest uses [TestClass] and [TestMethod] to specify a test fixtures and tests, while NUnit uses [TestFixture] and [Test].

Also, NUnit's equivalent to [ClassCleanup] is [TestFixtureTearDown], and it isn't static.

Bear in mind that if your tests absolutely have to run on MSTest and NUnit, you can decorate the tests with attributes for both frameworks and it will work (to a certain degree, anyway). However, to get ClassCleanup behaviour with both, you'd need something like this:

[ClassCleanup]
public static void MSTestClassCleanup()
{
    CommonCleanup();
}

[TestFixtureTearDown]
public void NUnitTearDown()
{
    CommonCleanup();
}

public static void CommonCleanup()
{
    // Actually clean up here
}
Nathan Tomkins