使用针对不同数据库的测试运行MSTest

时间:2021-01-27 15:43:32

I would like to ask what is the best way to execute a setup like the following: We have tests 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 the tests suite against every connection, and I hoped to parametrize in some way this process - something like setting the name of the connection and passing it on to the testrun as a parameter. What I figured out so far is that I can use different localconfigrun files and through deployment items I can feed a xml/txt file with the required value, but is there a nicer and lighter solution? I need just to send a key/value pair or simple string to configure my base class inside the test suite.

我想问一下执行如下设置的最佳方法是什么:我们有编译的测试套件,在app.config文件中我有6-7个不同的连接字符串到不同的数据库。我想针对每个连接运行测试套件,我希望以某种方式对这个过程进行参数化 - 比如设置连接的名称并将其作为参数传递给testrun。到目前为止我想到的是我可以使用不同的localconfigrun文件,通过部署项我可以提供具有所需值的xml / txt文件,但是有更好更轻的解决方案吗?我只需要发送一个键/值对或简单的字符串来配置我在测试套件中的基类。

I am using tfsbuild but I can use the mstest thrugh other environments as well (pure msbuild, etc.)

我正在使用tfsbuild,但我也可以使用mstest thrugh其他环境(纯msbuild等)

Thanks in advance.

提前致谢。

1 个解决方案

#1


I have had a similar issue. This is what I did:

我有类似的问题。这就是我做的:

My app.config looks like this:

我的app.config看起来像这样:

<?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 I connect to and I have 3 different connection strings for each (Development, Certification and Production)

在这个场景中,我连接了2个dbs,每个都有3个不同的连接字符串(开发,认证和生产)

Add this to the bottom of your project file (right click on the project and unload it). Make sure you add it before the </project> tag. (You will need to install the MSBuild Community Tasks for this to work. They can be downloaded for free from: http://msbuildtasks.tigris.org/ (Make sure you get a nightly build))

将其添加到项目文件的底部(右键单击项目并卸载它)。确保在 标记之前添加它。 (您需要安装MSBuild社区任务才能使用。可以从以下网站免费下载:http://msbuildtasks.tigris.org/(确保每晚都有一个版本))

  <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 <appSettings> section of the app.config file based on the current configuration. You will need to make new new configurations (I called them Cert and Prod).

这将根据当前配置替换app.config文件的 部分。您将需要进行新的配置(我称之为Cert和Prod)。

The last step is to make a file for each configuration (I called them DevAppConfig.xml, CertAppConfig.xml, ProdAppConfig.xml)

最后一步是为每个配置创建一个文件(我称之为DevAppConfig.xml,CertAppConfig.xml,ProdAppConfig.xml)

In each file should look like this (this one 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 of this, once installed will make the file that is output by app.config be auto changed based on the configuration you are compiling. This code works for both compiling in the IDE and in Team Build.

所有这一切,一旦安装将使app.config输出的文件根据您正在编译的配置自动更改。此代码适用于IDE和Team Build中的编译。

#1


I have had a similar issue. This is what I did:

我有类似的问题。这就是我做的:

My app.config looks like this:

我的app.config看起来像这样:

<?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 I connect to and I have 3 different connection strings for each (Development, Certification and Production)

在这个场景中,我连接了2个dbs,每个都有3个不同的连接字符串(开发,认证和生产)

Add this to the bottom of your project file (right click on the project and unload it). Make sure you add it before the </project> tag. (You will need to install the MSBuild Community Tasks for this to work. They can be downloaded for free from: http://msbuildtasks.tigris.org/ (Make sure you get a nightly build))

将其添加到项目文件的底部(右键单击项目并卸载它)。确保在 标记之前添加它。 (您需要安装MSBuild社区任务才能使用。可以从以下网站免费下载:http://msbuildtasks.tigris.org/(确保每晚都有一个版本))

  <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 <appSettings> section of the app.config file based on the current configuration. You will need to make new new configurations (I called them Cert and Prod).

这将根据当前配置替换app.config文件的 部分。您将需要进行新的配置(我称之为Cert和Prod)。

The last step is to make a file for each configuration (I called them DevAppConfig.xml, CertAppConfig.xml, ProdAppConfig.xml)

最后一步是为每个配置创建一个文件(我称之为DevAppConfig.xml,CertAppConfig.xml,ProdAppConfig.xml)

In each file should look like this (this one 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 of this, once installed will make the file that is output by app.config be auto changed based on the configuration you are compiling. This code works for both compiling in the IDE and in Team Build.

所有这一切,一旦安装将使app.config输出的文件根据您正在编译的配置自动更改。此代码适用于IDE和Team Build中的编译。