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.
tSQLt.RunAll
User runs
EXEC tSQLt.RunAllto run all teststSQLt.Private_RunMethodHandleris calledtSQLt.Private_Initis calledtSQLt.Private_CleanTestResultis called. The SP removes prior test results, deleting rows fromtSQLt.TestResult.
tSQLt.Private_RunAllis called. (The name of this SP was passed intotSQLt.Private_RunMethodHandleras parameter@RunMethod).tSQLt.Private_RunCursoris called. This SP obtains a cursor to iterate over test classes, running tests in each returned class.tSQLt.Private_GetCursorForRunAllcalled. (The name of this SP was passed intotSQLt.Private_RunCursoras 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_RunTestClassis called for each test class returned by the cursor.tSQLt.Private_GetSetupProcedureNameis called to identify the class's setup procedure. This returns[test_class].[setup]ifsetupexists in thetest-classschema.'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_RunTestis 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 calledNotice that
setupis called repeatedly – before each test in the class is executed, the class'ssetupSP 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_OutputTestResultsis calledtSQLt.DefaultResultFormatteris called to output results formatted as text. The SP selects tests fromtSQLt.TestResultand echoes them to the console usingRAISERROR(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.
tSQLt.Run
User runs
EXEC tSQLt.Runto run a specified test or classtSQLt.Private_RunMethodHandleris calledtSQLt.Private_Initis called (as above)tSQLt.Private_Runis called. (The name of this SP was passed intotSQLt.Private_RunMethodHandleras parameter@RunMethod).tSQLt.Private_GetLastTestNameIfNotProvidedreturns either the name of the test specified or that of the test last executed bytSQLt.Run(providing a default if no test name is specified).tSQLt.Private_SaveTestNameForSessionsaves the name of the test (to provide the default value for the next execution oftSQLt.Run, if any).Function
tSQLt.Private_ResolveNamereturns properties of the test, in particular whether a single test or a test class has been specified.If a test class,
tSQLt.Private_RunTestClassis calledIf a single test,
tSQLt.Private_GetSetupProcedureNameis called, followed bytSQLt.Private_RunTest
Finally,
tSQLt.Private_OutputTestResultsis called.