sql-serverunit-testingsql-server-2012tddtsqlt

What order the test cases run in the tSQLt?


I have 30 test cases in the 'EmployeeSchema'. I want to know in which order the test cases run in the tSqlt? If I know the order of execution of test cases, then I alter the execution order of my test cases one after another. Kindly advise.

-- Runs all the tests on MyTestClass
EXEC tSQLt.Run 'EmployeeSchema';

Solution

  • When running all tests within a test class, tSQLt.Run will run the tests in "random" order, meaning tSQLt leaves the order up to SQL Server and how it retrieves the data. Although, it shouldn't matter which order they run in since each test should be independent in and of itself.

    If for some reason you want to run individual tests in a different order you will need to run tSQLt.Run separately for each individual test in whichever order you want. You can't specify a particular order when you call tSQLt.Run for an entire test class.

    Note: thanks to @Sebastian Meine for correcting me

    To confirm it is random execution plan dependent, here is the cursor declaration from tSQLt.Private_RunTestClass that pulls the list of tests within the requested test class. There is no ORDER BY so SQL Server will return the test names in whichever order it sees fit.

    DECLARE testCases CURSOR LOCAL FAST_FORWARD 
        FOR
     SELECT tSQLt.Private_GetQuotedFullName(object_id)
       FROM sys.procedures
      WHERE schema_id = @TestClassId
        AND LOWER(name) LIKE 'test%';