Search code examples
sql-servert-sqltsqlt

tSQLt - About the Stored proc called 'Setup' - that gets automatically executed prior to test


in the tSQLt framework we can write the stored proc called 'setup' under the test class and this ensures that whenever a test or test suite (class containing many tests) runs, the 'setup' stored proc is executed first and then the test.

If we run the test class containing just one test, then its clear that the setup SP will get exeuted first and then the test. However, let's say the test class has got 5 tests, and we execute them using the RunAll method. In this case, will the setup get executed once before for every test? Or will the setup SP get executed only once for the entire test suite.


Solution

  • From tSQLt doc:

    tSQLt.Run [ [@testName = ] 'test name' ]
    

    1. Providing a test class name executes all tests in that test class. If a SetUp stored procedure exists in that test class, then it is executed before each test.

    and same for RunAll:

    tSQLt.RunAll executes all tests in all test classes created with tSQLt.NewTestClass in the current database. If the test class schema contains a stored procedure called SetUp, it is executed before calling each test case