SQL Server UnitTest Execution Order

How do I customize the order in which SQL Server Unit tests are run?

So for example I have this structure

UnitTests          -- Main Project
  - FooSchema      -- Test Class
     - SprocFoo1   -- Individual Unit Tests / Test Methods 
     - SprocFoo2
  - BarSchema
     - SprocBar1
     - SprocBar2

      

The test runs like sometimes:

 -- Test Initialiaze for TestClass FooSchema
    -- Pre-Test   -- for SprocFoo1
    -- Test       -- for SprocFoo1
    -- Post-Test  -- for SprocFoo1
    -- Pre-Test   -- for SprocFoo2
    -- Test       -- for SprocFoo2
    -- Post-Test  -- for SprocFoo2
 -- Test Cleanup for TestClass FooSchema
 -- Test Initialiaze for TestClass BarSchema
    -- Pre-Test   -- for SprocBar1
    -- Test       -- for SprocBar1
    -- Post-Test  -- for SprocBar1
    -- Pre-Test   -- for SprocBar2
    -- Test       -- for SprocBar2
    -- Post-Test  -- for SprocBar2
 -- Test Cleanup for TestClass BarSchema

      

and sometimes like this:

 -- Test Initialiaze for TestClass BarSchema
    -- Pre-Test   -- for SprocBar1
    -- Test       -- for SprocBar1
    -- Post-Test  -- for SprocBar1
    -- Pre-Test   -- for SprocBar2
    -- Test       -- for SprocBar2
    -- Post-Test  -- for SprocBar2
 -- Test Cleanup for TestClass BarSchema
 -- Test Initialiaze for TestClass FooSchema
    -- Pre-Test   -- for SprocFoo1
    -- Test       -- for SprocFoo1
    -- Post-Test  -- for SprocFoo1
    -- Pre-Test   -- for SprocFoo2
    -- Test       -- for SprocFoo2
    -- Post-Test  -- for SprocFoo2
 -- Test Cleanup for TestClass FooSchema

      

How do I configure FooSchema to always run first?

+3


source to share


2 answers


I'm not into this particular topic (SQL Server), but the whole point of unittesting is that unittest should always stand on its own: that is, without any dependencies (or at least the least possible). Because of this, I believe that you cannot change the order in which you run unittests, simply because it shouldn't be mandatory in the first place.



So, if your UnixService BarSchema objects have a dependency on FooSchema unittests, you are probably better off converting your unittests to BarSchema.

+1


source


The SQL Server unit tests are regular MSUnit test classes.

The first option is to use the MSUnit Ordered Test . This is a special element in Visual Studio with a designer that allows you to specify the exact order of your SQL Server test methods (or any other MSUnit test).

You can right click on the Visual Studio test project and add a custom test in two clicks.

Ordered tests are great if you want the designer to maintain the order of your tests, and if you want the test classes to be isolated , which is more convenient.

If you don't like ordered tests and want more control, then you can use "MSHnit test nesting" and keep all your MSUnit tests in the root container. Test class:



[TestClass]
public class RootContainer
{
    [TestClass]
    public class NestedFooSchemaTestClass
    {
        [ClassInitialize()]
        public static void ClassInit(TestContext context)
        {
            Debug.WriteLine("-- Test Initialize for TestClass FooSchema");
        }

        [TestInitialize()]
        public void TestInitialize()
        {
            Debug.WriteLine("  -- Pre-Test");
        }

        [TestMethod]
        public void Test1InClass()
        {
            Debug.WriteLine("  -- Test");
            Assert.AreEqual(true, true);
        }

        [TestMethod]
        public void Test2InClass()
        {
            Debug.WriteLine("  -- Test");
            Assert.AreEqual(true, true);
        }

        [TestCleanup()]
        public void TestCleanup()
        {
            Debug.WriteLine("  -- Post-Test");
        }

        [ClassCleanup()]
        public static void ClassCleanup()
        {
            Debug.WriteLine("-- Test Cleanup for TestClass FooSchema");
        }
    }

    [TestClass]
    public class NestedBarSchemaTestClass
    {
        [ClassInitialize()]
        public void ClassInit(TestContext context)
        {
            Debug.WriteLine("-- Test Initialize for TestClass BarSchema");
        }

        [TestInitialize()]
        public void TestInitialize()
        {
            Debug.WriteLine("  -- Pre-Test");
        }

        [TestMethod]
        public void Test1InClass()
        {
            Debug.WriteLine("  -- Test");
            Assert.AreEqual(true, true);
        }

        [TestCleanup()]
        public void TestCleanup()
        {
            Debug.WriteLine("  -- Post-Test");
        }

        [ClassCleanup()]
        public void ClassCleanup()
        {
            Debug.WriteLine("-- Test Cleanup for TestClass BarSchema");
        }
    }
}

      

When you run this code, you get:

-- Test Initialize for TestClass FooSchema
   -- Pre-Test
   -- Test
   -- Post-Test
   -- Pre-Test
   -- Test
   -- Post-Test
-- Test Initialize for TestClass BarSchema
   -- Pre-Test
   -- Test
   -- Post-Test
-- Test Cleanup for TestClass FooSchema
-- Test Cleanup for TestClass BarSchema

      

The order is almost what you want. The only difference is that Test Cleanup for classes will work like a package at the end. This is because test classes are unloaded by weight after all tests are complete, and therefore it is impossible to control the exact timing of Cleanup execution. This is intentional, as tests need to be isolated .

If you still need complete control over execution, an alternative approach would be to run your tests with a custom script that uses the MSTest command line utilities.

0


source







All Articles