c# - 单元测试的 App.config 替换

标签 c# visual-studio msbuild webdeploy slowcheetah

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

谢谢,贡萨洛

最佳答案

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

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

<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 文件到您的项目。例如:

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

相关资源:

关于c# - 单元测试的 App.config 替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5091243/

相关文章:

visual-studio - (VS2017中的SFML 2.5.1)许多 Unresolved external symbol 错误

c# - 如何强制以父类(super class)形式放置控件?

c# - Roslyn 是否真的允许您为 CSharp 项目操纵 TreatWarningsAsErrors?

c# - 关闭通用应用程序中打开的文件

c# - 从强类型列表填充下拉列表

c# - 更改新创建对象的保护级别?

c# - 从托管代码访问 GetConsoleHistoryInfo()

c++ - 查找 Visual Studio 中链接错误的原因

msbuild - Visual Studio和MSBuild在WiX项目上以不同的方式触发BeforeBuild

msbuild - 使用 msdeploy 和 Visual Studio 2012 构建一次并部署到多个环境