3

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?

Paul
  • 1,879
  • 1
  • 23
  • 44

4 Answers4

6

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

FoxDeploy
  • 12,569
  • 2
  • 33
  • 48
Paul
  • 1,879
  • 1
  • 23
  • 44
3

However Nunit works different than MSTest. A ClassCleanup method should execute first before executing next class initialize in MSTest.

2

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?

Austin Salonen
  • 49,173
  • 15
  • 109
  • 139
  • The [ClassCleanup] methods are required to be static. – Paul Dec 02 '11 at 17:24
  • 1
    It seems like the problem/confusion in this space comes from the fact that the truth is somewhere between these 2 things. Yes it MUST be after all tests in the class are complete, but there's no guarantee it's before any other tests are started. It sounds like it may be ASAP, or much later. – eddie.sholl Jul 14 '14 at 04:31
0

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.

piebie
  • 2,652
  • 21
  • 30