tSQLt call stack

A tSQLt test run iterates over test classes, for each test calling setup (if defined), running the test and collecting the results. This article gives an overview of the tSQLt call stack, illustrating where and how various configuration properties are used.

  • User runs EXEC tSQLt.RunAll to run all tests

    • tSQLt.Private_RunMethodHandler is called

      • tSQLt.Private_Init is called

        • tSQLt.Private_CleanTestResult is called. The SP removes prior test results, deleting rows from tSQLt.TestResult.

      • tSQLt.Private_RunAll is called. (The name of this SP was passed into tSQLt.Private_RunMethodHandler as parameter @RunMethod).

        • tSQLt.Private_RunCursor is called. This SP obtains a cursor to iterate over test classes, running tests in each returned class.

          • tSQLt.Private_GetCursorForRunAll called. (The name of this SP was passed into tSQLt.Private_RunCursor as parameter @GetCursorCallback).

            • The SP selects test class names from view tSQLt.TestClasses.

              The view returns schemas with the extended property 'tSQLt.TestClass' set.

          • tSQLt.Private_RunTestClass is called for each test class returned by the cursor.

            • tSQLt.Private_GetSetupProcedureName is called to identify the class's setup procedure. This returns [test_class].[setup] if setup exists in the test-class schema.

              'setup' is the only supported name for the setup procedure (case-insensitive, independent of collation).

            • Tests in the test class are identified.

              The class's tests are stored procedures in the test class schema with names that begin with 'test'.

            • tSQLt.Private_RunTest is called for each test identified in the class – each call to this SP is the execution of a single test.

              • A database transaction is opened

              • [test_class].[setup] is called

                Notice that setup is called repeatedly – before each test in the class is executed, the class's setup SP runs.

              • The test SP is executed

              • The transaction is rolled back

                This is the core of tSQLt's isolation guarantees.

              • The results are added to table tSQLt.TestResult

          • tSQLt.Private_OutputTestResults is called

            • tSQLt.DefaultResultFormatter is called to output results formatted as text. The SP selects tests from tSQLt.TestResult and echoes them to the console using RAISERROR (with severity dependent on test outcome).

Collection of test results in tSQLt.TestResult allows other formatters to be called after tSQLt.RunAll is complete. tSQLt.XmlResultFormatter returns results in an XML document in JUnit XML format suitable for consumption in a CI build environment like Azure Pipelines or Jenkins.

  • User runs EXEC tSQLt.Run to run a specified test or class

    • tSQLt.Private_RunMethodHandler is called

      • tSQLt.Private_Init is called (as above)

      • tSQLt.Private_Run is called. (The name of this SP was passed into tSQLt.Private_RunMethodHandler as parameter @RunMethod).

        • tSQLt.Private_GetLastTestNameIfNotProvided returns either the name of the test specified or that of the test last executed by tSQLt.Run (providing a default if no test name is specified).

        • tSQLt.Private_SaveTestNameForSession saves the name of the test (to provide the default value for the next execution of tSQLt.Run, if any).

        • Function tSQLt.Private_ResolveName returns properties of the test, in particular whether a single test or a test class has been specified.

          • If a test class, tSQLt.Private_RunTestClass is called

          • If a single test, tSQLt.Private_GetSetupProcedureName is called, followed by tSQLt.Private_RunTest

        • Finally, tSQLt.Private_OutputTestResults is called.