This documentation is archived and is not being maintained.

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, either as individual products or as part of Visual Studio Team System. You probably have to install Team Foundation Build and Team Foundation version control on different computers.

  • Install Team Explorer on the client computer.

  • Connect a Visual Studio user session to a computer that is running Team Foundation Server. For more information about how to connect to Team Foundation Server, see How to: Connect to Team Foundation Server.

After you configure your working environment, you must then follow these steps in Visual Studio Team System Database Edition:  

  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. Create a test list of database unit tests that you want to run.

  2. Check in the test list.

  3. Create a build definition, specifying that the test list should run as part of the build.

  4. Modify the build definition to copy the app.config file to the correct location for Team Foundation Build.

  5. Check in the updated 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 create an app.config file for use with Team Foundation Build. In this example, the file is named app.TeamBuild.config. In this file, you must specify the correct paths and connection strings for when Team Foundation Build runs on the build computer. You can use a post-build target to copy this file to AssemblyName.config. When the file is copied, you will overwrite the .config file that contained the values that are used when developers run the unit tests on their local development computers.

NoteNote:

Before you can build a database project, you must configure the instance of the design-time validation database for the service account on the computer where you run Team Foundation Build. You can configure the instance by running Visual Studio as the service account on the build computer. As an alternative, you can use the following command line to create the necessary registry entry, replacing the placeholders with the names for your configuration:

Runas /user:[DOMAIN\BuildServiceUserName] "REG ADD HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\9.0\DBPro\DialogPage\Microsoft.VisualStudio.TeamSystem.Data.DBProject.Settings.DataConnectionOptionsSettings /v SqlServerName /d [LocalInstanceName]"

To create a test list of database unit tests

  1. In the Test List Editor window, in the Group By list, click Test List Full Name to display the test list pane.

  2. Right-click Lists of Tests, and then click New Test List.

    The Create New Test List dialog box appears.

  3. Type the name of the test list that will contain your database unit tests. (For example, type Database Unit Tests.)

  4. (Optional) Type a description of your database unit test list, and specify a location in the test list hierarchy to put the new test list.

  5. Click OK.

    Your test list is created and appears in the test list hierarchy pane.

  6. Click All Loaded Tests to display all the tests in your solution.

  7. Drag the tests that you want to run as your database unit tests into the test list that you just created.

    NoteNote:

    To drag more than one test at a time, press and hold the CTRL key down as you click each test. To drag a contiguous block of tests, click the first test in the block, and then press and hold the SHIFT key down as you click the last test in the block. Do not use the check boxes for this purpose.

  8. (Optional) To verify the contents of the test list, click it in the test list hierarchy pane.

    The test list that you created, together with all the test lists in your solution, resides in your solution's test metadata file. Test metadata files have the extension .vsmdi. For more information, see Using Test Lists.

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 lists and tests. Whenever you add, delete, reorganize, or change the contents of test lists, your test metadata file is automatically updated to reflect these changes.

NoteNote:

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 test list

  1. Connect to a computer that is running Visual Studio Team System Team Foundation Server. For more information, see How to: Connect to Team Foundation Server.

  2. If your solution is not already in source control, add it to source control. For more information, see How to: Add a Project or Solution to Version Control.

  3. Click View, and then click Pending Checkins to display the Pending Checkin window.

  4. Check in all the files of your solution. For more information, see How to: Check In Pending Changes.

    NoteNote:

    If you are using Visual Studio Team System, 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.

    After the check-in operation is finished, a padlock icon appears next to each file in Solution Explorer to indicate that the file is checked in. For more information, see How to: Identify Source Control Item Status.

    Your checked-in tests are available to the Team Foundation Build feature of Visual Studio Team System. You can now create a build definition that contains the tests that you want to run.

To create a build definition

  1. In Team Explorer, click your team project.

  2. Right-click Builds, and then click New Build Definition.

    The Build Definition dialog box appears.

  3. Complete the sections in the Build Definition dialog box. For more information, see How to: Create a Build Definition.

    Important noteImportant Note:

    Team Foundation Build uses an MSBuild project file. In the MSBuild project options, you must select the Run test check box and specify the test metadata file that contains the test list that you created in Create a Test List. Finally, you must specify the test list that contains the tests that you want to run.

  4. When you have finished providing information in the wizard, click Finish.

    Your new build definition appears in the Team Explorer window under the Builds folder.

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 noteImportant Note:

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

To use a different app.config file for Team Foundation Build

  1. In Solution Explorer, right-click the test project file (.vbproj or .csproj) that you want to modify, and click Check Out for Edit.

    The Check Out dialog box appears.

  2. Click Check Out.

  3. On the File menu, click Close Solution.

    You must close the solution because you cannot modify the project file as long as the solution that contains that project file is open.

    NoteNote:

    You could also unload the project and reload it after you have modified it.

  4. On the File menu, point to Open, and click File.

  5. Browse to the test project file from step 1, and double-click the file to open it in the editor.

  6. Scroll to the bottom of the file, and add the following after the element that starts with "<Import Project=" and that imports the Visual Basic or Visual C# .Targets file:

    <Import Condition="'$(TeamBuildConstants)' != ''" Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v8.0\TeamData\Microsoft.VisualStudio.TeamSystem.Data.TeamBuild.targets"/>
    <Target Name="AfterBuild" DependsOnTargets="$(AfterBuildTeamBuildTargets)">
    </Target>
    
  7. On the File menu, click Save FileName.

  8. On the computer where you run Team Foundation Build, browse to the folder %ProgramFiles%\MSBuild\Microsoft\VisualStudio\v9.0\TeamData.

  9. In that folder, create a file that is named Microsoft.VisualStudio.TeamSystem.Data.TeamBuild.Targets with the following contents:

    <?xml version="1.0" encoding="utf-8"?>
    <Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <PropertyGroup> 
    <AfterBuildTeamBuildTargets>RenameTeamBuildConfig</AfterBuildTeamBuildTargets>
    </PropertyGroup>
    <ItemGroup>
    <__TeamBuildConfig Include="$(OutDir)$(TargetFileName).config"/>
    </ItemGroup>
    
    <Target Name="RenameTeamBuildConfig">
    
    <CreateItem Include="app.TeamBuild.config">
    <Output ItemName="TeamBuildAppConfig" TaskParameter="Include" />
    </CreateItem>
    
    <Copy SourceFiles="@(TeamBuildAppConfig)" DestinationFiles="@(__TeamBuildConfig)" />
    </Target>
    
    </Project> 
    
    NoteNote:

    If you name your configuration file something other than app.TeamBuild.config, you should replace the name in the XML.

  10. Copy the app.config file for your test project to a file that is named app.teambuild.config.

  11. Customize the settings for relative paths and connection strings in the app.teambuild.config file to have the values that you want to be used when you run the database unit tests under Team Foundation Build.

To check in the updated build definition

  • Repeat the steps that you followed in the procedure To check in the test list to check in the changes to the test project file (.vbproj, .csproj).

To run the new build type

  1. On the Build menu, click Queue Build {TeamProjectName}.

    The 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 Understanding the Team Foundation Build Explorer.

Show: