Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MSTest, when does [ClassCleanup] invocation happen when having test classes inheritance

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?

like image 804
Paul Avatar asked Mar 27 '26 23:03

Paul


1 Answers

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

like image 88
Paul Avatar answered Mar 29 '26 11:03

Paul



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!