locked
unit test dependencies RRS feed

  • Question

  • I have been trying to find a way to control the sequence of execution of unit tests: don't start unit test B until unit test A has run.  The reason is that unit test B is ill-behaved and alters the test database, so if unit B starts running, unit A will see all sorts of entries in the database that it isn't expecting.  (Why not make unit test B well-behaved?  Because it is devilishly complicated, and I'll make the case for why dependency runs are useful, regardless.)

    Yes, there is something called "ordered test" but having a dozen or more tests all collapse down to a single line in the test run output isn't spectacularly useful.  (Not that I can see how to get that single line to appear, anyway.)

    So, I tried to sequence them like this:

    unit test class A sets a public static boolean (let's call it classADone) true when it completes, in the ClassCleanup method.

    unit test class B, in the ClassInitialize method:

    while (classA.classADone == false)
    {
        Thread.Sleep(30000);
    }

    Taking advantage of Visual Studio's starting of test classes in alphabetical sequence lets class A test get started before class B test.  The class B test hits the while loop, and waits around.  Unfortunately, the class A test takes a long time to run--and it seems that it never sees classADone turn true, even though I know that it does turn true.  I'm guessing that for some reason, the class A unit test never runs to completion because the class B unit test spends a lot of time sleeping.  (Why would that matter?)

    Anyway, my desire for a dependency isn't that weird.  I would think that it would be very useful if you could make a unit test class dependent on either completion, or (let's get really adventuresome here), successful completion of another unit test.
    Wednesday, January 21, 2009 10:52 PM

All replies

  • Chuck - 2 things:

    1. Could you pls provide some more detail around what in the ordered test result view you would like to change? The ordered test result shows a single line result per each test case inside the ordered test. But if you double click that, it takes you to a detailed result view. Does that not help you?

    2. Are you trying to run your unit tests in MTA mode? If you are sure that test 1 in A starts before test 2 in B due to alphabetic placing, not sure why you would want to put the conditionals in the initialize methods at all. More so, the order of class init/class cleanup is not really meant for such use - so I wouldn't really recommend putting it to such applications.

    Ordered test is meant precisely for the situation you describe, so I would be eager to help get ordered tests working for you.

    - Anu

    http://blogs.msdn.com/anutthara
    Thursday, January 22, 2009 8:31 AM
  • 1.  I did not know that you could double click on the ordered test result to see the details.

    2. What is MTA mode?  The reason for conditionals is that I need for all the tests in A to finish before starting the first test in B.  The reason is that the tests in A change the state of the test database, and I want to recreate the test database before starting the tests in B.  RIght now, A starts, then B starts before all the tests in A are complete.
    Thursday, January 22, 2009 3:05 PM