Running MSTest with tests against different databases
I would like to ask what is the best way to accomplish the setup like this: We have a test suite that is compiled and in the app.config file I have 6-7 different connection strings to different databases. I would like to run a suite of tests for each connection, and I was hoping to parameterize this process somehow - something like setting a connection name and passing it to testrun as a parameter. So far I have figured out that I can use different localconfigrun files and through the deployment items I can feed the xml / txt file with the required value, but is there a nicer and easier solution? I just need to send a key / value pair or a simple string to set up my base class inside a test suite.
I am using tfsbuild, but I can use other mstest thrugh environments (pure msbuilds, etc.).
Thanks in advance.
a source to share
I had a similar problem. This is what I did:
My app.config looks like this:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="ConenctToInputDB" value="InputDev" />
<add key="ConnectToOutputDB" value ="OutputDev"/>
<add key="ClientSettingsProvider.ServiceUri" value="" />
</appSettings>
<connectionStrings>
<add name="LocalConnection" connectionString="YOUR CONNECTION STRING HERE" />
<add name="InputDev" connectionString="YOUR CONNECTION STRING HERE" />
<add name="InputCert" connectionString="YOUR CONNECTION STRING HERE"/>
<add name="OutputDev" connectionString="YOUR CONNECTION STRING HERE/>
<add name="OutputCert" connectionString="YOUR CONNECTION STRING HERE" />
<add name="InputProd" connectionString="YOUR CONNECTION STRING HERE/>
<add name="OutputProd" connectionString="YOUR CONNECTION STRING HERE" />
</connectionStrings>
In this secenario I have 2 dbs which I am connecting to and I have 3 different connection strings for each (development, certification and production)
Add this to the bottom of your project file (right click on the project and unload it). Make sure to add it before the tag </project>
. (To do this, you will need to install MSBuild Community Tasks, which can be downloaded for free: http://msbuildtasks.tigris.org/ (make sure you get a nightly build))
<PropertyGroup>
<!--Import the MSBuild community tasks so we can update xml-->
<MSBuildCommunityTasksPath>C:\PathToMSBuildCommunityTasks\MSBuildTasks</MSBuildCommunityTasksPath>
<SubstitutionsFile Condition="'$(Configuration)' == 'Debug'">DevAppSettings.xml</SubstitutionsFile>
<SubstitutionsFile Condition="'$(Configuration)' == 'Cert'">CertAppSettings.xml</SubstitutionsFile>
<SubstitutionsFile Condition="'$(Configuration)' == 'Prod'">ProdAppSettings.xml</SubstitutionsFile>
</PropertyGroup>
<Import Project="C:\PathToMSBuildCommunityTasks\lib\MSBuildTasks\MSBuild.Community.Tasks.Targets" />
<Target Name="AfterBuild">
<!--Update the app config to have the correct environment paths-->
<Message Text="Updating $(MSBuildProjectName) config to $(Configuration)" Importance="high"></Message>
<XmlMassUpdate ContentFile="$(OutDir)\$(MSBuildProjectName).dll.config" SubstitutionsFile="..\..\$(SubstitutionsFile)" />
</Target>
This will replace the section of <appSettings>
the app.config file based on the current configuration. You will need to create new new configurations (I named them Cert and Prod).
The last step is to create a file for each configuration (I named them DevAppConfig.xml, CertAppConfig.xml, ProdAppConfig.xml)
Each file should look like this (this is for the certification configuration):
<?xml version="1.0" encoding="utf-8"?>
<!--This file is used by the build files to merge in solution wide app settings
Some projects contain files that have an AppSetting section (usually in App.config). Those projects have
and AfterBuild event in the project file that substitues this xml tree over the the normal xml tree.-->
<configuration xmlns:xmu="urn:msbuildcommunitytasks-xmlmassupdate">
<appSettings>
<add xmu:key="key" key="ConenctToInputDB" value="Cert"/>
<add xmu:key="key" key="ConnectToOutputDB" value="ESPCert"/>
</appSettings>
</configuration>
all this, once installed, will make the file that app.config outputs will be automatically modified based on the configuration you are compiling. This code works for both the IDE and Team Build compilation.
a source to share