Udostępnij za pośrednictwem


Using Test Conditions in Database Unit Tests

In a typical database unit test, a Transact-SQL (T-SQL) test script runs and returns an instance of the ExecutionResult class. The instance of this class contains a DataSet, the execution time, and the rows affected by the script. All of this information is collected during execution of the script. These results can be evaluated within the T-SQL script by using the RAISERROR function, or they can be evaluated by using test conditions. Visual Studio Team System Database Edition provides a set of predefined test conditions for you to use.

Predefined Test Conditions

The following table lists the predefined test conditions that you can add by using the Test Conditions pane in the Database Unit Test designer.

Test Condition

Test Condition Description

Empty ResultSet

Fails if the result set returned from the T-SQL script is not empty.

Execution Time

Fails if the T-SQL test script takes longer than expected to execute. The default execution time is 30 seconds.

The execution time applies to the test script test only, not to the pre-test script or the post-test script.

Inconclusive

Always produces a test with a result of Inconclusive. This is the default condition added to every test. This test condition is included to indicate that test verification has not been implemented. Delete this test condition from your test after you have added other test conditions.

Not Empty ResultSet

Fails if the result set is empty. You can use this test condition or the EmptyResultSet with the T-SQL @@RAISERROR function in your test script to test whether an update worked correctly. For example, you can save pre-update values, run the update, compare post-update values, and raise an error if you do not get the expected results.

Row Count

Fails if the result set does not contain the expected number of rows.

Scalar Value

Fails if a particular value in the result set does not equal the specified value. The default Expected value is null.

Note

The Execution Time test condition specifies a time limit under which the T-SQL test script must run. If this time limit is exceeded, the test fails. Test results also include a Duration statistic, which differs from the Execution Time test condition. The Duration statistic includes not only the execution time but also the time to connect to the database two times; the time to run any other test scripts, such as the pre-test script and the post-test script; and the time to run the test conditions. Therefore, a test can pass even if its duration is longer than its execution time.

The reported Duration does not include time used for data generation and schema deployment because they occur before the tests are run. To view the test duration, select a test run in the Test Results window, right-click, and choose View Test Results Details.

You can add test conditions to database unit tests by using the Test Conditions pane of the Database Unit Test Designer. For more information, see How to: Add Test Conditions to Database Unit Tests.

You can also edit your test-method code directly to add more functionality. For more information, see Customizing Database Unit Tests in Visual C# or Visual Basic. For example, you can add functionality to a test method by adding Assert statements. For more information, see Using Transact-SQL Assertions in Database Unit Tests.

Extensible Test Conditions

In addition to the six predefined test conditions, you can write new test conditions of your own. These test conditions will be displayed in the Test Conditions pane of the Database Unit Test Designer. For more information, see How to: Create Test Conditions for the Database Unit Test Designer

See Also

Concepts

Using Transact-SQL Assertions in Database Unit Tests

Scripts in Database Unit Tests

Terminology Overview of Database Edition