How to: Create Database Unit Tests for Functions, Triggers, and Stored Procedures

This topic applies to:

Visual Studio Ultimate

Visual Studio Premium

Visual Studio Professional 

Visual Studio Express

Topic applies Topic applies Topic does not apply Topic does not apply

You can write unit tests that evaluate changes to any database object. However, Visual Studio Premium and Visual Studio Ultimate include some additional support for database functions, triggers, and stored procedures. When you create tests for these objects, Transact-SQL code stubs are automatically generated for you to customize.

Important

Even though you can run database unit tests in Visual Studio Professional, you must have Visual Studio Premium or Visual Studio Ultimate if you want to create or modify database unit tests.

To create a database unit test from a function, trigger, or stored procedure

  1. Open your database project in Solution Explorer.

  2. On the View menu, click Database Schema View.

  3. In Schema View, open the project folder that contains the objects that you want to test. For example, open the Stored Procedures folder.

  4. Right-click the object that you want to test, and click Create Unit Tests.

    The Create Unit Tests dialog box appears, displaying the folders and objects in your database project. The check box for the object that you right-clicked is selected.

  5. (Optional) To create unit tests for additional objects, select their check boxes under Current Selection.

  6. In Project, specify the test project to which you want to add this test.

    - or -

    If no test project exists or if you want to create a new test project, select Create a new Language test project.

  7. Under Output class, choose to either insert the unit test in an existing test class or create a new test class and click OK.

    If your test project is new or has not been configured, the Project ProjectName configuration dialog box appears. In this dialog box, you can configure the following settings for your test project:

    • The database connection that is used to execute tests

    • The database connection that is used to validate test results

    • Whether to automatically deploy the database project and any associated schema changes to a given project configuration before unit tests are run.

    • A data generation plan that you want to run before unit tests are run.

    For more information, see How to: Configure Database Unit Test Execution.

  8. Configure the project, and click OK.

    - or -

    Click Cancel to create the unit test without configuring the test project.

    Your test appears in the Database Unit Test Designer, and the Transact-SQL code stubs appear in the Transact-SQL editor.

    Note

    The Inconclusive test condition is the default condition that is 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 add other test conditions. For more information, see How to: Add Test Conditions to Database Unit Tests.

See Also

Tasks

How to: Create an Empty Database Unit Test