在 Team Build 2013 上启用 XUnit.NET 的 NuGet 版本时遇到问题

Having trouble enabling the NuGet version of XUnit.NET on Team Build 2013

我想使用 Xunit 作为测试框架。我创建了测试项目,添加了两个 nuget 包(xunit 和 xunit.runner.visualstudio)并且一切正常。 Visual Studio 发现测试。

但我如何配置 TFS 2013 版本来发现该测试?这样做的正确方法是什么?我找到了很多提示,但我认为所有提示都与旧的测试运行器有关,它作为 Visual Studio 扩展而不是当前的 NuGet 包下载。

迁移到基于 NuGet 的发行版的原因之一是它应该 "just work" 与 Visual Studio(和 Team Build)一起使用,而不需要在构建服务器上安装任何东西。

也就是说,我们不正式支持 Team Build,因为我们没有支持它的带宽或基础设施。如果有问题需要修复,我们很乐意接受社区的修复。

迁移到 xunit.net 2 后,我遇到了同样的问题。您需要将 TFS 构建控制器指向包含所需 xunit.net 程序集的文件夹(请参阅:构建控制器属性/“版本自定义程序集的控制路径”)。

只需将 NuGet 包“构建”下的子文件夹中的所有文件复制到您在测试控制器中配置的文件夹中,在我的例子中是 .\packages\xunit.runner.visualstudio.2.1.0-beta2-build1055\build。由于 TFS 不扫描子文件夹,因此将层次结构展平很重要,这样目标文件夹只包含文件。

我终于解决了这个问题。 xUnit 不适用于构建,因为 NuGet 恢复被调用得太晚了。它应该在解决方案的 运行 MSBuild 之前调用。详细说明可以在 Nuget Documentation website 上找到。

应用该解决方案后,无需在构建服务器上安装 VS 或任何其他第 3 方扩展即可正常工作。

随着基于 NuGet 的测试 运行 人员的引入,在 Team Build 上启用这些测试 运行 人员的解决方案已经更改。您不再需要提取 .vsix.nuget 文件并将它们签入源代码管理(在配置构建控制器以了解下载这些文件的位置之后)。

安装最新版本

您需要确保这些项目已经安装在构建服务器上:

  • Visual Studio 已更新至 Update 4
  • Nuget 已更新至至少 2.7

卸载旧测试运行ner

确保从构建服务器上的 Visual Studio 实例中卸载旧的基于 .vsix 的测试 运行ner,并从源代码管理文件夹中删除文件(如果它们存储在那里) .

如果您在此类文件夹中有较旧版本的 XUnit 运行ner,它可能会比您的 nuget 包加载得更早,如果您的解决方案引用了较新版本的 XUnit,则会导致问题。

修复以前修补的项目文件

如果您的解决方案之前已 "patched" 使用解决方案资源管理器中的“启用 Nuget 包还原”选项,则您需要 "unpatch" 它们。该过程在 NuGet Documentation. The documentation isn't always very clear on this, but the "Automatic Package Restore" option is the preferred way of working with Team Build 2013 as mentioned on the page you referenced in your own answer:

上有说明

Applies To:

  • Custom MSBuild projects running on any version of TFS Team Foundation Server 2012 or earlier
  • Custom Team Foundation Build Process
  • Templates migrated to TFS 2013 or later
  • Build Process Templates With Nuget Restore functionality removed

If you're using Visual Studio Online or on-premise Team Foundation Server 2013 with its build process templates, Automatic Package Restore happens as part of the build process.

混合使用新旧方法可能会破坏包恢复。要取消修补文件 follow the procedure outlined here

  1. Remove the NuGet.exe and NuGet.targets files from the solution's .nuget folder. Make sure the files themselves are also removed from the solution workspace.
  2. Retain the NuGet.Config file to continue to bypass adding packages to source control.
  3. Edit each project file (e.g., .csproj, .vbproj) in the solution and remove any references to the NuGet.targets file. Open the project file(s) in the editor of your choice and remove the following settings:
<RestorePackages>true</RestorePackages>  
...
<Import Project="$(SolutionDir)\.nuget\nuget.targets" />  
...
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">  
    <PropertyGroup>
        <ErrorText>This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them.  For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
    </PropertyGroup>
    <Error Condition="!Exists('$(SolutionDir)\.nuget\NuGet.targets')" Text="$([System.String]::Format('$(ErrorText)', '$(SolutionDir)\.nuget\NuGet.targets'))" />
</Target>

检查构建日志

如果一切正常,您的诊断构建日志应该显示对 NuGet 和测试适配器的引用:

(图片显示 NUnit,但你明白我想的意思)

使用新的构建模板

如果您使用的是自定义模板或旧版本的 Team Build 过程模板,请确保更新为扩展名为 .v12.xaml 的模板。更新后的构建模板使用新的 activity 来调用 instructed to run NuGet Package Restore before the solution is built 的 MsBuild。您可以自己检查一下,找到 Run MsBuild activity 并检查它是否正确,应该是 Microsoft.TeamFoundation.Build.Activities.RunMSBuild.

检查构建服务器的环境变量

有一个环境变量(以及一个 MsBuild 属性)可以禁用自动包还原。这会导致在构建期间出现一条特定的错误消息,该消息会抱怨 "consent"。基本上 NuGet 是 "told" 的环境变量,你没有同意自动恢复包。这些步骤因系统上 NuGet 的版本而异。在至少更新到 2.7 之后,这些将是要做的检查:

检查您的环境变量以查看没有名为 'EnableNuGetPackageRestore' 的变量具有除 true 以外的任何其他值。该变量不再需要,可以删除。

然后打开 Visual Studio(也在构建服务器上至少打开一次)并检查:

(Nuget 2.7+): Visual Studio -> Tools -> Package Manager -> Package Manager Settings ->

  • [x] Allow NuGet to download missing packages
  • [x] Automatically check for missing packages during build in Visual Studio

Source

进行这些更改后,您的解决方案应该会在构建期间自动恢复所有 nuget 包,而无需 "enable" 您的解决方案,签入 nuget.exe 或 运行 自定义脚本或其他 msbuild 文件比你的解决方案文件。

将 Nuget 包添加到每个测试项目

确保每个测试项目都配置了正确的 XUnit 测试 运行ner 包。您应该寻找 "xUnit.net [Runner: Visual Studio]":

 PM> Install-Package xunit.runner.visualstudio