Posts Tagged ‘30 posts in 30 days’

TFS2012: Create Build Definitions


In this article I will explain how to create a build definition in the Visual Studio 2012 and TFS 2012 server. I used TFS 2012 Express when I wrote this post. In a previous post, I went through the build types that Team Foundation Server supports.

To run a Team Build in TFS, assuming you already setup the Build Controller and Agents, you need to create a build definition. A build definition acts as a settings or configurations file for the build process.

Create Build Definition

The following steps show how to create a team build definition:

Open Visual Studio 2012

Open Team Explorer (View—>Team Explorer)

Connect to a TFS Server and Team Project


After you are connected to the Team Project of interest, go back to the Home menu or page and click on the Builds link


Click New Build Definition to create a new build definition


Give your build definition a meaningful name (e.g. mypp-CI-Debug, myapp-Scheduled-UnitTests).

Queuing process option is sufficiently explained on the screen.


Click on the Trigger node

On this screen, you can specify the build type. Build types were covered in the previous article. Under Rolling builds, Build no more often than ever x minutes means you want at least x minutes between builds to allow for build accumulation. Under Gated Check-in, check the Merge and build up to x submissions, if you want to group submissions when building them to improve performance.


The next step is to configure the Workspace.

On this screen, you can select as many folders as you want. Those folders and their contents will be downloaded (Get performed) to the Source Directory to the code. As a best practice, you want to avoid referencing the root folder of you team project (e.g. $/MyTeamProject) because you want to avoid getting the code for unrelated files or projects. Point to the folder where your code exists and add other folders for other related code or libraries such as libraries for third party controls. When adding multiple folders, make sure you maintain the folder level or relative path among folders at the Build Agent Folder.


Click on the Build defaults, it is probably called with that name because you can override these values when you queue a build <— I am guessing 🙂

On this screen, you have to specify a Build a controller and the drop folder location. The drop folder contains the output of the build process.


The Process screen is where you can define the Build process Template, which is a workflow of activities to execute your team build, and the Build process parameters that are passed to the workflow build template as arguments.


If you are using the Default Template, you need to at least populate the Items to Build parameter with a solution. Deferent build templates may have deferent parameters. You can also create and customize a build template.

Click the ellipsis button

Click on Add

Select a solution

You can select multiple solutions

You can set the configurations for the build in the Configurations tab


In another post, I will explain the other parameters.

The last step, is to define the retention policy. The retention policy automate the deletion of old builds to save disk space.

Save the build definition

Notice that it will be added to the build definition section on the build window. To queue a build manually, right click and click on Queue New Build


TFS2012: Run Data Driven Unit Tests in TFS Team Build

In a previous post, I explained how to run one test method twice using the data driven approach with Excel workbook as a datasource. The solution worked without issues when running using Visual Studio 2012. But I wasn’t able to run the unit tests using TFS Team build. I was getting the following error

The unit test adapter failed to connect to the data source or to read the data. For more information on troubleshooting this error, see “Troubleshooting Data-Driven Unit Tests” ( in the MSDN Library. Error details: ERROR [IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified


The following is my setup:


Active Directory: Windows 2008 R2

TFS 2012 Express: Windows 2008 R2, SQL Server 2012 Express

Team Build Server: Windows 2008 R2, Team Build Controller and 1 Agent running as NT Authority\ Network Service

Troubleshooting Steps

Check if the Excel file is copied to the Out folder

In my case it was copied


Check if you have the proper drivers installed


In my case I had make sure that the ODBC driver for Excel xlsx files is installed. There are two versions of ODBC Datasource: 32bit and 64bit. To learn more about that topic checkout this KB

I opened both the 32bit and 64bit versions of the odbcad32.exe on the build machine and I noticed that the drivers were installed for the earlier version of Excel (xls) and not the 2007+ version (xlsx).


The reason it was working in Visual Studio (local machine) is I had Office 2010 installed on the machine that has Visual Studio.

I logged on as lab\administrator (domain admin rights) and then I Installed Office 2010

Now I opened the ODBCad32 and found out that the driver is installed and the DSN is added to the User DSN.





If I run the team build now, I will fail with the same error because the service is running as Network Service not lab\Administrator

Change the Run as account

Open the TFS Admin console from the build machine

Click on Build Configurations Node

Stop the Build Service

Click on Change in the Run the Service as groupbox

Select User a user account

WARNING: You MUST NOT use a domain admin for your run as service. I will change it later on in the article. The reason I am using a domain admin here is because I installed office using that account. The office installation adds the DNS names for at the user level which only accessible to the current user. I will show it to you working first then I will change the account.

Enter your username. If you keep the “User the same identity as Windows Service” checked, TFS will add this account to the Project Collection\Project Collection Build Service Accounts group. You can use a local machine admin account in the first box and a domain user with limited privileges in the Connect to TFS as box as I will show later on.


Click OK and Save.

Queue a team build


Replace Run as Service Account

Now I will replace the the run as Service Account as a local Administrator

Open the TFS admin console from the build machine

Build Configuration—>Service Property—>Stop the service

Click the change button

I am using a local admin account. If you template interact with other network computers you need to use a domain account.


Uncheck Use the same identity as windows Service

Click the Change button

Enter the account information. This account will be used for any interaction with TFS


Click and Start

Queue a new build

The build should fail with same error we had above.


I am getting this error because the installation of Office 2010 added the DNS name to the User DNS which means it is only available to the the current user. You can fix this issue by logging on as your new account and add the DNS to the User DNS tab.

Add Excel DNS to the Local DNS list

Log on as the new user (I am logging on as LocalAdmin). You can also run the Odbca32.exe as a different user if you don’t want of log off and on.

Open “C:\Windows\SysWOW64\odbcad32.exe”

In the User DNS

Click Add


Select Microsoft Excel Driver (the one that contains xlsx)

Click finish

Name it Excel Files

Click OK

Run the build again and it should pass

TFS2012: How to delete a workspace using the tf command

Today while I am writing another blog post, I encountered the following error

Exception Message: The working folder C:\Builds\5\MyTeamProject\UnitTestProject1\src is already in use by the workspace 6_5_TFS2012Build1;TFS2012BUILD1$ on computer TFS2012BUILD1. The Build Agent TFS2012Build1 – Agent1 is running as a different user Administrator and local paths can only be mapped to a single workspace. To resolve this issue, you can configure the Build Service to run as the user who owns the conflicting workspace, or you can delete the workspace using the ‘tf workspace’ command. (type WorkingFolderInUseException)

I received that error because, I changed the run as service account for the Build Controller. From a machine that has Visual Studio installed, open the VS command prompt.

Then, the first thing you want to do is check what switches TF workspace has by running the following command

tf workspace /?

To learn more about the switches or arguments, check this msdn article

You can also use the tf workspaces to target multiple workspaces or to list all workspaces for a collection

tf workspaces /collection:http://tfs2012express:8080/tfs/defaultcollection Collection:http://tfs2012express:8080/tfs/defaultcollection

The above will return you the workspaces on the current machine. If you want to view all workspaces, you need to use the /computer switch

tf workspaces /collection:http://tfs2012express:8080/tfs/defaultcollection Collection:http://tfs2012express:8080/tfs/defaultcollection /computer:*


In my case, I am using the following switches

tf workspace /delete /collection:<a href="http://tfs2012express:8080/tfs/defaultcollection 6_5_tfs2012build1;tfs2012build1$


Type Yes and press enter

VS2012: Run Test Method twice for different connection strings


I am writing this blog post in response to the question posted in the the Social Msdn community. I recorded the steps to solve the problem. Scroll all the way down to view it.

The question was

“We have a project with a number of integration tests testing our Entity Framework 5.0 queries against Sql Server. We have a requirement for this product to also persist to Oracle.

We have the project set up to set the connection string and build and seed the database via EF5 in the AssemblyInitialize.   Changing the connection string is all that is required to successfully run all the tests in the project against either db.

Is there a way to configure the TFS 2012 Build process so that it will execute all tests in this project twice?  Once against Sql Server and then run them all again against Oracle?

If at all possible, we need these tests to run against each database on check-in.”

Proposed Solution

I will be using the data driven unit test approach to solve this issue. The code will read the connection string values form an Excel spreadsheet file.


Create a new Test Project

Add an app.config file


Replace the code in the config file with the following where DBConnections.xlsx is the Excel file name:

<?xml version="1.0" encoding="utf-8" ?>
 <section name="microsoft.visualstudio.testtools" type="Microsoft.VisualStudio.TestTools.UnitTesting.TestConfigurationSection, Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
 <add name="ExcelConnection" connectionString="Dsn=Excel Files;dbq=.\DBConnections.xlsx;defaultdir=.; driverid=790;maxbuffersize=2048;pagetimeout=5" providerName="System.Data.Odbc" />
 <add name="MyExcelDataSource" connectionString="ExcelConnection" dataTableName="Sheet1$" dataAccessMethod="Sequential"/>


Create a new Excel file and save it as DBConnections.xlsx in the project directory

In Sheet1, type ConnectionStrings in the first cell

In cell A2, type the SQL connection string

In cell A3, type the Oracle connection string


Save Excel sheet

Click show all files icon in solution explorer

add DBConnections.xlsx to the project


Change the “Copy to Output directory” of the DBConnections.xlsx to copy if newer. Wasn’t able to get it to deploy to the test Out folder without doing so.

Add a reference to the System.Data.dll

Add TestContext property and decorate your test method with the DataSource and DeploymentItem Attributes

Use TestContext.DataRow[“ConnectionStrings”] to access the row. The test method will run once for each data row

namespace UnitTestProject1
public class UnitTest1
public TestContext TestContext { get; set; }

public void TestMethod1()

Build the solution

From test explorer run the test


TFS2012 Team Build Types

In this article, I will go through the build types (triggers) the TFS Team Build supports out of the box.

What is TFS Team Build

Team build is a TFS service that is responsible for building, or compiling, your code. You can ,however, change the behavior of the process by modifying the Build Definition and the Build Process template. The Team Build Service should, ideally, be placed on a machine that is different from the TFS Server. Team Build is a resource intensive operation and placing it on the same machine as your TFS Server will degrade performance. In TFS, you can run as many builds as the number of build agents you have installed.

Build Types in TFS

Team Foundation Server 2012 supports the following build types:

Manual: The Team Build is triggered manually from Visual Studio, TFS API or tfsBuild command.

Continuous Integration (CI): Team Build is triggered whenever a new code checkin occurs. You want to use this type of build to verify the a code checkin from a developer is not breaking the build. You may want to run unit tests part of the CI to verify that the new code is not breaking any of the tested business roles.


Rolling Build: Rolling build is similar to CI build. It queues a build for every check in. However, it allows you to limit your build to one at a time and accumulate the checkins until the previous build is complete. Once the previous build is complete, the team build queues a new build request which includes all the checkins occurred while the previous build was running. You can also limit the number of builds TFS can queue within a certain period of time.


Gated Check in:Gated check in similar to the CI build. It is gets triggered on code check-ins. The code however gets shelved and only is committed to source control if the build passes otherwise that check-in is rejected. Team build run one Gated Build at a time. When you have an active team that checks in many files it may take a long time for the code to be built and committed to source control. In TFS 2012, you can define the Gated Check in build definition to merge x number of submissions together. That feature improves the efficiency of TFS Team Build. If the consolidated build fails, Team build will build each submission individually and commit the changesets that succeed and reject the failed ones.


Scheduled: is a build that runs at a specific day of week and time. You may want to run a schedule this build off hours to run the tests that take long time on your code, e.g. integration, regression and/or coded UI tests. The scheduled build can run once a day. You can choose which days of week you want the scheduled build to run.