I've posted about Data Driven Testing
before, unfortunately that solution (setting the DataDirectory attribute) no longer works. It looks like the between Beta3 (when the entry was written) and the release the way the test code was parsed has changed. Setting the DataDirectory now fails to work. A colleague of mine (Simon Horrell) came up with a better solution which I thought I'd document more fully here.
When using the DataSource attribute in the testing code you can, rather than entering all the details for the attribute, instead refer to an App.config file. This file would then contain the connection string and the other details needed by the data source.
This makes our code look like this
[TestMethod]
[DataSource("dataDrivenTestingDS")]
public void TestFirePhotonTorpedoe()
{
// code here
}
This refers to an entry in an app.config file called "dataDrivenTestingDS". This entry is part of a custom configuration section parsed by the testing framework.
To get this code to work you need to add an App.config file to your testing project. This file will need to have three configuration sections. The first is for the connection string that would appear in the DataSource attribute of the test. You also need to add a configuration section handler that is part of the unit testing framework. This custom handler parsers a section called DataSources that contains the information the DataSource attribute needs.
The config file looks like this
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="microsoft.visualstudio.testtools"
type="Microsoft.VisualStudio.TestTools.UnitTesting.TestConfigurationSection,
Microsoft.VisualStudio.QualityTools.UnitTestFramework,
Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
</configSections>
<connectionStrings>
<add name="dataDrivenTesting"
connectionString="server=.\SQLExpress;AttachDBFilename=|DataDirectory|\StarShipTest.mdf;Integrated
Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
<microsoft.visualstudio.testtools>
<dataSources>
<add name="dataDrivenTestingDS" connectionString="dataDrivenTesting"
dataTableName="FireTorpedoesTest" dataAccessMethod="Sequential"/>
</dataSources>
</microsoft.visualstudio.testtools>
</configuration>
This doesn't quite get you all the way home as the data base still has to becopied to the 'out' directory wasting space, and hardcoding the fully qualified directory name is not an option (the config file should be under source control). This means we need to amend the App.config file to use an external per user file containing the user's settings, something like this.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="microsoft.visualstudio.testtools"
type="Microsoft.VisualStudio.TestTools.UnitTesting.TestConfigurationSection,
Microsoft.VisualStudio.QualityTools.UnitTestFramework,
Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
</configSections>
<connectionStrings configSource="mysettings.config">
</connectionStrings>
<microsoft.visualstudio.testtools>
<dataSources>
<add name="dataDrivenTestingDS" connectionString="dataDrivenTesting"
dataTableName="FireTorpedoesTest" dataAccessMethod="Sequential"/>
</dataSources>
</microsoft.visualstudio.testtools>
</configuration>
and the external configuration file contains the following
<connectionStrings>
<add name="dataDrivenTesting" connectionString="server=.\SQLExpress;AttachDBFilename=c:\datadirectory\StarShipTest.mdf;Integrated
Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
You need to remember to deploy the per user config file (mysettings.config above) to the 'out' directory (the App.config is deployed and renamed automatically by Visual Studio), I typically do this in the testrunconfig configuration file.
Your developers can check the App.confg into source control and keep the mysettings.config only on their local machine.