How to: Configure Database Unit Test Execution

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

By configuring your test project, you can specify several settings that control aspects of how your database unit tests are run. These configuration settings are stored in your test project's app.config file. If you edit this file directly, the new values appear in the Project Configuration dialog box.

Your solution can contain multiple test projects. Each test project contains one app.config file (that is, one set of configuration settings). As a result, your solution can contain different sets of database unit tests (one set for each test project) that are configured to run differently.

These settings control how your test connects to the database that you will test, how you deploy a schema from a database project to that database, and whether you populate the database by using a data generation plan:

  • Database Connections. You use this setting to specify the connection strings that are used to connect to the database that you are testing. For more information, see Specify Connection Strings

  • Schema deployment. A database project is an offline representation of your database. The database project represents the structure of your database objects but contains no data. After you make schema changes in a database project, you can test them in an actual database. In the schema deployment step, database objects that you want to test are copied from your database project into the database on which you run tests. For more information about schema deployment, see Deploy a Database Schema.

  • Database State. By generating test data for your database, you can set its state before your test run starts. You can also set the database state by restoring it from a backup copy in the TestInitialize script. For more information about this approach, see Scripts in Database Unit Tests.

    To specify the data to generate, you create a data generation plan in which you identify the tables and columns that should contain test data. You also specify the form that you want the data to take. For example, you can generate the appropriate number of digits for a phone number or import fictitious customer names from a database of scrubbed test data. For more information, see Use a Data Generation Plan.

    Note

    Tests do not run in the solution folder but in a separate folder on the local hard disk. Although you can configure aspects of test deployment, you typically do not need to configure them for database unit tests. For more information about test deployment, see Running Tests.

Specify Connection Strings

To specify database connection strings

  1. On the Test menu, click Database Test Configuration.

    The Project 'TestProject' configuration dialog box appears.

  2. Under Database Connections, you can do the following:

    • Click the database connection against which you want to execute unit tests.

    • Select the Use a secondary data connection to validate unit tests check box, and click a database connection in the list if you want test execution to be validated against a different database connection.

    • Click New Connection to add a connection to either list. You can also click Edit Connection to modify settings on an existing connection.

    This step creates the ExecutionContext connection string, which is used to execute the test script in your database unit test. If you also specify a secondary connection, the PrivilegedContext connection string is also created. This connection is used to test interactions with the database outside the test script in your database unit test. For more information, see Overview of Connection Strings and Permissions.

  3. Click OK to close the Project 'TestProject' configuration dialog box.

  4. Rebuild the test project to apply the configuration changes.

Deploy a Database Schema

To deploy to a database the schema of a database project

  1. In Solution Explorer, right-click your database project, and then click Build.

    When you build your database project, you generate a Transact-SQL script. This script, when it is run against a database, re-creates the structure of your database project in that database.

  2. Select the test project that you want to configure.

  3. On the Test menu, click Database Test Configuration.

    The Project 'TestProject' configuration dialog box appears.

  4. Under Deployment, you can do the following:

    • Select the Automatically deploy database projects before running tests check box to makesure that any schema changes that you have made to your database project are committed before tests are run.

    • Under Database Project, click the database project that you want to deploy, or click the ellipsis to browse for another project. Database project files have the extension .dbproj.

    • Under Deployment Configuration, click the project configuration against which you want to deploy. Your choices are Debug, Default, or Release. However, if you create a configuration for unit testing, that configuration also appears as an option.

  5. Click OK to close the Project 'TestProject' configuration dialog box.

    At the start of the test run, the Transact-SQL script that was generated in step 1 is run. This action deploys the schema to the target database.

  6. Rebuild the database unit test project to apply the configuration changes.

    Note

    If you are using data generation and deploy the schema as part of your unit test project, clear the Block incremental deployment if data loss might occur check box. This check box appears on the Build tab in the properties of the database project. If you do not clear this check box before you try to deploy the schema, deployment will fail. For more information, see How to: Control Data Loss during Deployment to Existing Databases.

Use a Data Generation Plan

To use a data generation plan with a database unit test

  1. Create a data generation plan. For more information, see How to: Create Data Generation Plans.

  2. Specify the tables that you want to populate with the data generation plan. For more information, see How to: Specify Tables for Data Generation.

  3. Specify columns within those tables to populate. For more information, see How to: Specify Columns for Data Generation.

  4. Save the data generation plan, and note its name.

  5. In Solution Explorer, click the test project that you want to configure.

  6. On the Test menu, click Database Test Configuration.

    The Project 'TestProject' configuration dialog box appears.

  7. Under Database State, do the following:

    • Select the Generate test data prior to running unit tests check box to apply a data generation plan to your database project before you run tests. Data generation plans have a .dgen extension.

    • Click the data generation plan that you want to apply in the list, or click the ellipsis to browse for a plan.

    • Select the Clear database prior to generating test data check box to overwrite the existing test database in the specified database project.

  8. Click OK to close the Project 'TestProject' configuration dialog box.

    Code that invokes the data generation plan is added to your test project's AssemblyInitialize method, which you can find in the file that is named DatabaseSetup.cs or DatabaseSetup.vb. If you run database unit tests now, the data generation plan populates the database one time, at the start of your test run.

    Note

    You cannot reverse the effects of generating test data.

  9. (Optional) To use this data generation plan more than one time in your test run, copy the code that invokes it from the AssemblyInitialize method, and paste it into other methods of your database unit test.

  10. Rebuild the test project to apply the configuration changes.

See Also

Tasks

How to: Create Data Generation Plans

Concepts

Verifying Database Code by Using Unit Tests