App.config替换单元测试

Gon*_*alo 12 c# msbuild visual-studio webdeploy slowcheetah

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

谢谢,贡萨洛

Enr*_*lio 29

可以通过变通方法为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>
Run Code Online (Sandbox Code Playgroud)

然后将其他App.config文件添加到项目中,以用于要应用转换的每个构建配置.例如:

<ItemGroup>
    <None Include="App.config" />
    <None Include="App.Release.config">
        <DependentUpon>App.config</DependentUpon>
    </None>
</ItemGroup>
Run Code Online (Sandbox Code Playgroud)

相关资源:

  • SlowCheetah似乎仅限于将变换绑定到项目配置,即"App.Debug.config".使用Enrico的描述,我能够轻松地对不符合配置约定的文件执行相同的操作.谢谢! (2认同)

Say*_*imi 7

我已经创建了一个Visual Studio添加,可以用来转换app.config,就像转换web.config一样.您可以在http://visualstudiogallery.msdn.microsoft.com/69023d00-a4f9-4a34-a6cd-7e854ba318b5找到加载项SlowCheetah .

我发布了一篇关于如何在构建服务器上运行的博客.


Mor*_*ten 6

我建议您在测试时包装配置查找,提取接口并存根.