How to: Create an Empty Database Unit Test
Include unit tests in your database project to verify changes you make to database objects do not break existing functionality. The following procedures explain how to create database unit tests for any database object. Visual Studio Premium includes some additional support for database functions, triggers, and stored procedures. For more information, see How to: Create Database Unit Tests for Functions, Triggers, and Stored Procedures.
When you create a database unit test using the first procedure, a test project is automatically created for you if no test project exists. If test projects already exist, you have the option of adding the new test to one of those projects or you can create a new test project. For more information about test projects, see How to: Create a Test Project for Database Unit Testing.
You have two options for creating a database unit test:
Create a new database unit test inside a new test class.
All database unit tests within a given test class will use the same TestInitialize and TestCleanup scripts. Create a new test class if you want your unit test to use different TestInitialize and TestCleanup scripts than other unit tests. For more information, see Scripts in Database Unit Tests.
Create a new database unit test inside an existing test class.
Choose this option if your unit test will use the same TestInitialize and TestCleanup scripts as other unit tests within the class.
To create a database unit test inside a new test class
On the Test menu, click New Test.
- or -
In Solution Explorer, right-click a test project, point to Add, and then click New Test.
The Add New Test dialog box appears.
Under Templates, click Database Unit Test.
Under Test Name, enter a name for the test.
Under Add to Test Project, select a test project to add this test to.
- or -
If no test project exists or if you want to create a new test project, select Create a new <language> test project.
If your test project is new or has not been configured, the Project <ProjectName> configuration dialog box appears. This dialog box allows you to configure the following information for your test project:
The database connection used to execute tests.
The database connection used to validate test results, deploy a database, and generate data.
The automatic deployment of the database project and any associated schema changes to a given project configuration before you run unit tests.
A data generation plan that you want to run before running unit tests.
For more information, see How to: Configure Database Unit Test Execution.
Provide project configuration information and click OK.
- or -
Click Cancel to create the unit test without configuring the test project.
Your blank test appears in the Database Unit Test Designer. Depending on the language you specified for creating the test project, a Visual Basic or C# source code file is added to the test project. This file contains the database unit test class that Visual Studio Premium generates for the unit test you just created. This test class can contain one or more unit tests that you can add through the Database Unit Test Designer or through code as new test methods in the test class.
When you select this file in Solution Explorer, it is displayed in the Database Unit Test Designer, by default. To view the code or to customize it to add more functionality to your unit tests, select the file, right-click, and choose View Code.
To create a database unit test inside an existing test class
Open an existing database unit test class in the Database Unit Test Designer.
Click the plus (+) sign in the navigation bar.
The Specify a Name for a Database Test dialog box appears.
Type a name and click OK.
Your new database unit test is available in the drop-down list in the navigation bar. It is also added as a new test method in the test class. To view the test method in code, select the class file, right-click, and choose View Code. The name of the current test class file is displayed on the tab at the top of the Database Unit Test Designer.
After you have configured the test project and created the unit test, the next steps are:
Define pre-test and post-test actions.
Add a Transact-SQL test script.
Add test conditions or other assert statement to verify the script results.
The Inconclusive test condition 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. For more information, see How to: Add Test Conditions to Database Unit Tests.