Share via


How to: Run Database Unit Tests from Team Foundation Build

You can use Team Foundation Build to run your database unit tests as part of a build verification test (BVT). You can configure your database unit tests to deploy the database, generate test data, and then run selected tests. If you are not familiar with Team Foundation Build, you should review the following information before you follow the procedures in this topic:

Before you use these procedures, you must first configure your working environment by performing the following tasks:

  • Install Team Foundation Build and Team Foundation version control. You probably have to install Team Foundation Build and Team Foundation version control on different computers.

  • Connect to an instance of Visual Studio Team Foundation Server. For information about how to connect to Team Foundation Server, see How to: Connect to a Team Project in Team Foundation Server.

After you configure your working environment, you must then follow these steps in Visual Studio Premium or Visual Studio Ultimate: 

  1. Create a database project.

  2. Import or create the schema and objects for the database project.

  3. Configure the database project properties for build and deployment.

  4. Create one or more database unit tests.

  5. Add the solution that contains the database project and the database unit test project to version control, and check in all files.

The procedures in this topic describe how to create a build definition to run your database unit tests as part of an automated test run:

  1. Configure Test Settings to Run Database Unit Tests on an x64 Build Agent

  2. Assign Tests to a Test Category (Optional)

  3. Modify the Test Project

  4. Check in the Solution

  5. Create a Build Definition

  6. Run the New Build Definition

Running Database Unit Tests on a Build Computer

When you run database unit tests on a build computer, the unit tests might be unable to find the database project files (.dbproj) or the data generation plans (.dgen). This problem occurs because the app.config file references those files by using relative paths. In addition, your database unit tests might fail if they cannot find the instance of SQL Server that you want to use to run the database unit tests. This problem can occur if the connection strings that are stored in the app.config file are not valid from the build computer.

To resolve these issues, you must specify an override section in your app.config that overrides app.config with an configuration file that is specific to your Team Foundation Build environment. For more information, see Modify the Test Project, later in this topic.

Configure Test Settings to Run Database Unit Tests on an x64 Build Agent

Before you can run database unit tests on an x64 build agent, you must configure your test settings to change the Host Process Platform.

To specify the Host Process Platform

  1. Open the solution that contains the test project for which you want to configure settings.

  2. In Solution Explorer, in the Solution Items folder, double-click the Local.testsettings file.

    The Test Settings dialog box appears.

  3. In the list, click Hosts.

  4. In the details pane, in Host Process Platform, click MSIL to configure your tests to run on an x64 build agent.

  5. Click Apply.

Assign Tests to a Test Category (Optional)

Typically, when you create a build definition to run unit tests, you specify one or more test categories. All tests in the specified categories are run when the build is run.

To assign tests to a test category

  1. Open the Test View window.

  2. Select a test.

  3. In the properties pane, click Test Categories and then click the ellipsis (…) in the right-most column.

  4. In the Test Category window, in the Add New Category box, type a name for your new test category.

  5. Click Add and then click OK.

    The new test category will be assigned to your test and it will be available to the other tests through their properties.

Modify the Test Project

By default, Team Foundation Build creates a configuration file from the project's app.config file when it builds the unit tests project. The paths to the data generation plan and database project are stored as relative paths in the app.config file. The relative paths that work in Visual Studio will not work because Team Foundation Build puts the built files in different locations relative to where you run the unit tests. In addition, your app.config file contains the connection strings that specify the database that you want to test. You also need a separate app.config file for Team Foundation Build if the unit tests must connect to a different database than the one that was used when the test project was created. By making the modifications in the next procedure, you can set up your test project and build server so that Team Foundation Build will use a different configuration.

Important

You must perform this procedure for each test project (.vbproj or .vsproj).

To specify an app.config file for Team Foundation Build

  1. In Solution Explorer, right-click the app.config file, and click Copy.

  2. Right-click the test project and click Paste.

  3. Right-click the file called Copy of app.config, and click Rename.

  4. Type BuildComputer.dbunitttest.config and press ENTER, where BuildComputer is the name of the computer on which your build agent runs.

  5. Double-click BuildComputer.dbunitttesting.config.

    The configuration file opens in the editor.

  6. Change the relative path to the .dbproj and .dgen files by adding a folder level for the Sources folder and a subfolder with the same name as the solution. For example, if the configuration file initially contains the following entry:

    <DatabaseDeployment DatabaseProjectFileName="..\..\..\SalesDB\SalesDB.DBProj" Configuration="Debug" />
    

    Update the file as follows:

    <DatabaseDeployment DatabaseProjectFileName="..\..\..\Sources\SalesDB\SalesDB.DBProj" Configuration="Debug" />
    

    Repeat this process for the data generation file, if one was specified.

    When you are finished, your BuildComputer.dbunitttest.config file should resemble the following example:

    <DatabaseUnitTesting>
            <DatabaseDeployment DatabaseProjectFileName="..\..\..\Sources\UnitTest\UnitTest\UnitTest.dbproj"
                Configuration="Debug" />
            <DataGeneration ClearDatabase="true" />
            <ExecutionContext Provider="System.Data.SqlClient" ConnectionString="Data Source=.\SQLEXPRESS;Initial Catalog=UnitTestB;Integrated Security=True;Pooling=False"
                CommandTimeout="30" />
            <PrivilegedContext Provider="System.Data.SqlClient" ConnectionString="Data Source=.\SQLEXPRESS;Initial Catalog=UnitTestB;Integrated Security=True;Pooling=False"
                CommandTimeout="30" />
    </DatabaseUnitTesting>
    
  7. Update the ConnectionString attribute for ExecutionContext and PrivilegedContext to specify connections to the target database to which you want to deploy.

  8. On the File menu, click Save All.

  9. In Solution Explorer, double-click app.config.

  10. In the editor, update the <DatabaseUnitTesting> node to match the following:

    <DatabaseUnitTesting AllowConfigurationOverride="true">
    

    By making this change, you allow Team Foundation Build to use the replacement configuration file that you created.

  11. On the File menu, click Save All.

    Next, you must update Local.testsettings to include your customized configuration file.

To customize Local.testsettings to deploy the customized configuration file

  1. In Solution Explorer, double-click Local.testsettings.

    The Test Settings dialog box appears.

  2. In the list of categories, click Deployment.

  3. Select the Enable deployment check box.

  4. Click Add File.

  5. In the Add Deployment Files dialog box, specify the BuildComputer.dbunitttest.config file that you created.

  6. Click Apply.

  7. Click Close.

  8. On the File menu, click Save All.

    Next, you check in your solution to version control.

Check in the Solution

In this procedure, you check in all the files of your solution. These files include the test metadata file of your solution, which contains your test category associations and tests. Whenever you add, delete, reorganize, or change the contents of tests, your test metadata file is automatically updated to reflect these changes.

Note

This procedure describes the steps if you are using Team Foundation version control. If you are using different version control software, you must follow the steps that are appropriate for your software.

To check in the solution

  1. Connect to a computer that is running Team Foundation Server.

    For more information, see Using Source Control Explorer.

  2. If your solution is not already in source control, add it to source control.

    For more information, see Add Files to Version Control.

  3. Click View, and then click Pending Checkins.

  4. Check in all the files of your solution.

    For more information, see Check In Pending Changes.

    Note

    You might have a specific team process that governs how automated tests are created and managed. For example, the process might require that you verify your build locally before you check in that code together with the tests that will be run on it.

    In Solution Explorer, a padlock icon appears next to each file to indicate that it is checked in. For more information, see View Version Control File and Folder Properties.

    Your tests are available to Team Foundation Build. You can now create a build definition that contains the tests that you want to run.

Create a Build Definition

To create a build definition

  1. In Team Explorer, click your team project, right-click the Builds node, and click New Build Definition.

    The New Build Definition window appears.

  2. In Build definition name, type the name that you want to use for the build definition.

  3. In the navigation bar, click Build Defaults.

  4. In Copy build output to the following drop folder (UNC path, such as \\server\share), specify a folder to contain the build output.

    You can specify a shared folder on your local computer or any network location to which the build process will have permissions.

  5. In the navigation bar, click Process.

  6. In the Required group, in Items to Build, click the browse (…) button.

  7. In the Build Project List Editor dialog box, click Add.

  8. Specify the solution file (.sln) that you added to version control earlier in this walkthrough, and click OK.

    The solution appears in the Project or solution files to build list.

  9. Click OK.

  10. In the Basic group, in Automated Tests, specify the tests that you want to run. By default, the tests that are contained in files named *test*.dll from your solution will be run.

  11. On the File menu, click Save ProjectName.

    You have created a build definition. Next you modify the test project.

Run the New Build Definition

To run the new build type

  1. In Team Explorer, expand the team project node, expand the Builds node, right-click the build definition that you want to run, and then click Queue New Build.

    The Queue Build {TeamProjectName} dialog box appears with a list of all existing build types.

  2. If necessary, in Build definition, click your new build definition.

  3. Confirm that the values in the Build definition, Build agent, and Drop folder for this Build fields are all appropriate, and then click Queue.

    The Queued tab of Build Explorer appears. For more information, see Manage and View Completed Builds.

See Also

Tasks

Create a Basic Build Definition

Monitor Progress of a Running Build

Concepts

Queue a Build