Home > ALM, Visual Studio > VS2012: Run Test Method twice for different connection strings

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



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: