Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run a script before UnitTesting

The Test Initialize script in the SQL Server Unit Tests runs before each test methods and Test Clean up runs after each test methods.

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 Run like this

 -- 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

How could I add a script that would run in the very beggining of the project and also a script that will run at the very end of the project?

So it would run like this

 -- Pre MasterUnitTest
 -- 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
 -- Post MasterUnitTest
like image 296
Karlx Swanovski Avatar asked Oct 21 '22 01:10

Karlx Swanovski


1 Answers

The SQL Database Unit Tests generated by the Visual Studio designers are actually common MSUnit test classes.

If you want to execute some setup (Pre MasterUnitTest) and teardown (Post MasterUnitTest) logic you can just restructure the tests that get generated by Visual Studio and nest them inside a class that will run the setup and teardown logic. Here is an example:

[TestClass]
public class NestedUnitTests
{
    [ClassInitialize()]
    public static void ClassInit(TestContext context) {
        Debug.WriteLine("Init before all individual Test Classes");
    }

    [TestMethod]
    public void Init()
    { }

    [TestClass]
    public class NestedFooSchemaTestClass
    {
        [TestMethod]
        public void Test1InClass()
        {
            Debug.WriteLine("Test Class Foo Schema - Method 1");
            Assert.AreEqual(true, true);
        }
    }

    [TestClass]
    public class NestedBarSchemaTestClass
    {
        [TestMethod]
        public void Test2InClass()
        {
            Debug.WriteLine("Test Class Foo Bar - Method 2");
            Assert.AreEqual(true, true);
        }
    }

    [ClassCleanup()]
    public static void ClassCleanup() {
        Debug.WriteLine("Clean after all Test Classes executed");
    }
}

When i run this with a TestRunner i get:

Init before all individual Test Classes
Test Class Foo Schema - Method 1
Test Class Foo Bar - Method 2
Clean after all Test Classes executed

Note: The only thing you will not be able to achieve with this approach is to execute Class Cleanup before another Class Initializes (e.g. Class Cleanup of Foo before Class Initialize of Bar). The Test Cleanup for all classes will run at the end. This is because the Test Classes are bulk unloaded by the runtime after all tests finish running, and that is intentional, since the tests are supposed to be isolated.

If you want full control over the execution an alternative approach would be to run your tests via a custom script that uses MSTest command line utitilites. That way you could run a Pre-script (or pre-tests), then your main tests dll, and finally a Post-script (or post-tests).

Here is a link with more info how to use MSTest from the command-line:

http://msdn.microsoft.com/en-us/library/jj155804.aspx

like image 154
Faris Zacina Avatar answered Nov 03 '22 19:11

Faris Zacina