App.config替换单元测试

时间:2011-02-23 12:59:35

标签: c# visual-studio msbuild webdeploy slowcheetah

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

谢谢Gonzalo

3 个答案:

答案 0 :(得分:28)

可以通过解决方法对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>

相关资源:

答案 1 :(得分:7)

我创建了一个Visual Studio add,其中可以用来转换app.config,其方式与转换web.config的方式相同。您可以在http://visualstudiogallery.msdn.microsoft.com/69023d00-a4f9-4a34-a6cd-7e854ba318b5找到加载项SlowCheetah。

我发布了一篇关于如何get this working on a build server的博客。

答案 2 :(得分:6)

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

相关问题