ClassCleanup in MSTest is static, but the build server uses nunit to run the unit tests. How can i a
Posted
by Kettenbach
on Stack Overflow
See other posts from Stack Overflow
or by Kettenbach
Published on 2010-05-19T23:26:28Z
Indexed on
2010/05/19
23:30 UTC
Read the original article
Hit count: 309
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
© Stack Overflow or respective owner