Reputation: 1875
I have hierarchy of functional tests like this
[TestClass]
class BaseClass
{
// specific methods and members relevant to all functional tests are here
// ie how to work with db
}
[TestClass]
class Module1:BaseClass
{
[ClassInitialize]
public static void Module1TestsInit(TestContext context)
{
//create some db data here, which is needed only for Module1
}
[ClassCleanup]
public static void Module1TestsCleanup()
{
//delete Module1 db data
}
}
[TestClass]
class Module2:BaseClass
{
[ClassInitialize]
public static void Module2TestsInit(TestContext context)
{
//create some db data here, which is needed only for Module2
}
[ClassCleanup]
public static void Module2TestsCleanup()
{
//delete Module2 db data
}
}
When the tests are executed I am expecting that [ClassCleanup]
will run when all methods from Module1
are completed and then again when Module2
tests are finished. I have many classes like Module1 with the same base class.
However, all ClassCleanup methods do run only when ALL tests from all modules completes. That is not convenient since I have some conflicting data in the different modules and want to clean up each class results when this class tests finished.
Any thoughts?
Upvotes: 3
Views: 9416
Reputation: 1875
I figured it has nothing to do with inheritance.
http://blogs.msdn.com/b/ploeh/archive/2007/01/06/classcleanupmayrunlaterthanyouthink.aspx
That's just the way MSTest works.
Edit: Original link dead, Way back machine link here
Upvotes: 6
Reputation: 31
However Nunit works different than MSTest. A ClassCleanup method should execute first before executing next class initialize in MSTest.
Upvotes: 3
Reputation: 2672
If it's possible, I would try to split your test classes into 2 files and make two calls to MSTest. We ran into some problems like that here and that seemed to work.
Upvotes: 0
Reputation: 50225
This statement...
all ClassCleanup methods do run only when ALL tests from all modules completes
... conflicts with MSDN for ClassCleanupAttribute (emphasis mine) ...
Identifies a method that contains code to be used after all the tests in the test class have run and to free resources obtained by the test class. This class cannot be inherited.
Since your initialize and cleanup methods are static, they are not inherited from the common base so they should be independent. But since they are static (and I don't use VisualStudio.TestTools so I cannot verify), is that causing the issues you're having?
Upvotes: 2