locked
Sequence of multiple ClassInitialize/ClassCleanup methods RRS feed

  • Question

  • I am running tests through a solution with 10+ test projects each having a various number of test classes. I have observed that every ClassInitialize method (in any class or project) are executed before any test (in any class or project). The same goes for every ClassInitialize method (in any class or project) seems to be executed after all test (in any class or project). I would have expected the TestIniitalize method to be run before the first test in that particular class and TestCleanup after the last test in same class regardless of tests in other classes and projects.

    Is this some sort of optimization because there is actually no way of telling which order the tests are run in?
    Wednesday, January 13, 2010 11:00 AM

Answers

  • Hello again,

    For multiple instances of ClassInitializes, you are right. Here's basically why this is happening: ClassInitialize will be run the first time a test from the class is scheduled to run. ClassCleanup will be run whenever we're done running tests for the test RUN. Until the RUN is completely done, and the test class disposed, we can't know to run class cleanup, even if we are done running all tests in that specific class.

    Hope this helps

    David Gorena Elizondo
    [MSFT] Visual Studio Team Test
    Thursday, January 21, 2010 1:24 AM

All replies

  • Hello,

    The order of how mstest runs things may be confusing; you might want to take a look at the following post from one of our program managers. Let me know if this doesn't make sense:
    http://blogs.msdn.com/nnaderi/archive/2007/02/17/explaining-execution-order.aspx

    Thanks,
    David Gorena Elizondo
    [MSFT] Visual Studio Team Test
    Wednesday, January 13, 2010 10:31 PM
  • Hi David

    Great link!

    Although it seems that nnaderi with his post are a bit wrong in how ClassInitialize/ClassCleanup is ordered. This is exactly what jisotupa notes in the third and forth comment:

    1. All ClassInitialize seems to be run before all tests
    2. All ClassCleanup seems to be run after all test have been run

    It is not a problem for me, I am just trying to understand why.
    Monday, January 18, 2010 10:15 AM
  • Hello again,

    For multiple instances of ClassInitializes, you are right. Here's basically why this is happening: ClassInitialize will be run the first time a test from the class is scheduled to run. ClassCleanup will be run whenever we're done running tests for the test RUN. Until the RUN is completely done, and the test class disposed, we can't know to run class cleanup, even if we are done running all tests in that specific class.

    Hope this helps

    David Gorena Elizondo
    [MSFT] Visual Studio Team Test
    Thursday, January 21, 2010 1:24 AM