【问题标题】:App.config replacements for unit testsApp.config 替换单元测试
【发布时间】:2011-07-02 18:02:27
【问题描述】:

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

谢谢,贡萨洛

【问题讨论】:

    标签: c# visual-studio msbuild webdeploy slowcheetah


    【解决方案1】:

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

    【讨论】:

    • 这是一个不错的选择,但我希望有一个 Visual Studio 解决方案(例如部署项目)。谢谢!
    【解决方案2】:

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

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

    <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>
    

    相关资源:

    【讨论】:

    • 正是我需要的。谢谢!
    • 我也发现了这个:frankmao.com/2011/07/15/… 这似乎表明它可以用于任何 XML 文件。
    • 您可以使用visualstudiogallery.msdn.microsoft.com/…,而不是手动执行此操作
    • 是的,应该使用 SlowCheetah 而不是这样做
    • SlowCheetah 似乎仅限于将转换绑定到项目配置,即“App.Debug.config”。使用 Enrico 的描述,我可以轻松地对不遵守配置约定的文件执行相同的操作。谢谢!
    【解决方案3】:

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

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

    【讨论】:

      猜你喜欢
      • 2011-11-29
      • 1970-01-01
      • 2021-03-24
      • 2011-04-03
      • 2018-12-11
      • 2020-02-17
      • 1970-01-01
      • 1970-01-01
      • 2013-09-10
      相关资源
      最近更新 更多