单元测试的App.config替换 [英] App.config replacements for unit tests

查看:113
本文介绍了单元测试的App.config替换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的持续集成服务器(TeamCity)配置为在构建时运行我们的应用程序中的所有单元测试.在运行这些测试之前,我需要更改一些appSettings以使其对我们的CI服务器有效.通过使用Visual Studio随附的部署项目,我的Web项目实现了类似的目的.我可以为测试项目做同样的事情吗?

my continuous integration server (TeamCity) is configured to run all the unit tests in our app on build. Prior to running those tests, i need to change some of the appSettings to make them valid for our CI server. I'm achieving something similar for my web project by using the deployment project provided with Visual Studio. Can i do the same for a Test project?

感谢贡萨洛

推荐答案

可以通过变通办法对App.config文件使用Web.config转换.

您只需要在构建过程的正确阶段调用适当的MSBuild任务即可.
将此代码段添加到您的项目文件中:

You simply have to invoke the appropriate MSBuild tasks at the right stage in your build process.
Add this code snippet to your project file:

<UsingTask
    TaskName="TransformXml"
    AssemblyFile="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.Publishing.Tasks.dll" />

<Target Name="AfterCompile" Condition="exists('App.$(Configuration).config')">
    <!-- Generates the transformed App.config in the intermediate directory -->
    <TransformXml
        Source="App.config"
        Destination="$(IntermediateOutputPath)$(TargetFileName).config"
        Transform="App.$(Configuration).config" />
    <!-- Forces the build process to use the transformed configuration file -->
    <ItemGroup>
        <AppConfigWithTargetPath Remove="App.config"/>
        <AppConfigWithTargetPath
            Include="$(IntermediateOutputPath)$(TargetFileName).config">
            <TargetPath>$(TargetFileName).config</TargetPath>
        </AppConfigWithTargetPath>
    </ItemGroup>
</Target>

然后针对要在其中应用转换的每个构建配置,向项目添加其他App.config文件.例如:

Then add additional App.config files to your project for each build configuration where you wish to apply a transformation. For example:

<ItemGroup>
    <None Include="App.config" />
    <None Include="App.Release.config">
        <DependentUpon>App.config</DependentUpon>
    </None>
</ItemGroup>

相关资源:

  • Web.config Transformation Syntax for Web Application Project Deployment
  • .Config File Transformation

这篇关于单元测试的App.config替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆