我想问一下执行如下设置的最好方法是什么:我们有编译的测试套件,在app.config文件中,我有6-7个不同的连接字符串到不同的数据库。我想对每个连接运行测试套件,我希望以某种方式参数化这个过程-比如设置连接的名称并将其作为参数传递给testrun。到目前为止,我想出的是我可以使用不同的localconfigrun文件,并且通过部署项,我可以为xml/txt文件提供所需的值,但是有没有更好、更轻量级的解决方案呢?我只需要发送一个键/值对或简单的字符串来配置测试套件中的基类。
我使用的是tfsbuild,但我也可以在其他环境中使用mstest (纯msbuild等)。
提前谢谢。
发布于 2009-05-28 04:39:16
我也遇到过类似的问题。这是我所做的:
我的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>在本示例中,我有2个数据库连接,每个数据库有3个不同的连接字符串(开发、认证和生产)。
将其添加到项目文件的底部(右键单击项目并将其卸载)。确保将其添加到</project>标记之前。(您需要安装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>这将根据当前配置替换app.config文件的<appSettings>部分。您将需要创建新的新配置(我称之为Cert和Prod)。
最后一步是为每个配置创建一个文件(我称它们为DevAppConfig.xml、CertAppConfig.xml、ProdAppConfig.xml)
每个文件中的应如下所示(此文件用于认证配置):
<?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>所有这些,一旦安装,将使app.config输出的文件根据您正在编译的配置自动更改。此代码适用于在IDE和Team Build中进行编译。
https://stackoverflow.com/questions/877950
复制相似问题