This section describes how test cases are created and executed, and provides the reference pages for the various methods to manage and execute test cases.
We’re going to answer two important questions in this section, “What happens when you create a test class?” and “What happens when a test class gets executed?”
What happens when you create a test class?
Not much, actually. When you create a test class using tSQLt.NewTestClass, a schema is created. That schema is created with an extended property so that tSQLt can later figure out which schemas are test classes. Note: If there is already a schema with the same name as the one you are trying to create, it is dropped first.
What happens when a test class gets executed?
If you execute tSQLt.RunTestClass, tSQLt does the following things:
1. It looks at all the stored procedures in the test class (schema) that start with the word “test”. These are all considered to be all the test cases for that test class.
2. For each of the test cases:
a. A record is created indicating that the test case is being executed in the tSQLt.TestResult table.
b. tSQLt starts a transaction.
c. If there is a stored procedure named SetUp on the test class, it is executed.
d. The test case stored procedure is executed.
e. The transaction is rolled-back.
f. The record in tSQLt.TestResult is updated accordingly if the test case succeeded, failed or threw an error.
3. The test results are displayed in the console.
If you execute tSQLt.RunAll, tSQLt first looks at all the schemas in the database for ones marked as test classes. Then, it follows steps 1 and 2 above for each test class. The test results are displayed after running all test classes.
Create test classes using tSQLt.NewTestClass. Execute test classes using tSQLt.RunTestClass or tSQLt.RunAll. When individual test cases are executed, they are wrapped in a transaction which is rolled-back. Before each test case, SetUp is called if it exists on the test class.