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

有些话、适合烂在心里 提交于 2019-12-24 03:43:33

问题


I would like to use Xunit as a test framework. I've created test project, added two nuget packages (xunit and xunit.runner.visualstudio) and everything works great. Visual Studio discover tests.

But how can I configure TFS 2013 build to discover that tests? What's the proper way to do that? I found a lot of tips but I think all are related to old test runner which was downloaded as Visual Studio Extensions instead of the current NuGet package.


回答1:


With the introduction of the NuGet based test runners the solution for enabling these test runners on Team Build has changed. No longer do you need to extract the .vsix or .nuget files and check them into source control (after configuring your build controller to know where to download these files).

Install the latest versions

What you need to ensure is that these items have been installed on the build server:

  • Visual Studio has been updated to Update 4
  • Nuget has been updated to at least 2.7

Uninstall the old test runner

Make sure you uninstall the old .vsix based test runner from your Visual Studio instance on the build server and remove the files from the source control folder if they were stored there.

If you have an older version of the XUnit runner in such folder, it may load earlier than your nuget package and cause issues if your solution references a newer version of XUnit.

Fix previously patched project files

If your solution has previously been "patched" using the Enable Nuget Package Restore option in the solution explorer, you need to "unpatch" them. The process is explained on the 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.

Mixing both the old and the new way can break package restore. To unpatch your files 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>

Check the build log

If all is well, your diagnostic build log should show a reference to NuGet and the test adapter:

(Image shows NUnit, but you get the meaning I suppose)

Use the new build template

If you're using a custom template or an older version of the Team Build process template, make sure you update to one that has .v12.xaml as extension. The updated build templates use a new activity to invoke MsBuild that is instructed to run NuGet Package Restore before the solution is built. You can check this yourself by finding the Run MsBuild activity and checking whether it's the right one, it should be Microsoft.TeamFoundation.Build.Activities.RunMSBuild.

Check your build server's environment variables

There is an environment variable (as well as an MsBuild property) that can disable automatic package restore. This results in a specific error message during build that will complain about "consent". Basically NuGet is "told" by the environment variable that you haven't given consent to restore packages automatically. The steps are different based on the version of NuGet on your system. After having updated to at least 2.7, these would be the checks to do:

Check your environment variables to see that there is no variable called 'EnableNuGetPackageRestore' with any other value than true. The variable is no longer needed and can be removed.

Then open Visual Studio (also on the build server at least once) and check:

(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

After making these changes your solution should automatically restore all nuget packages during build without the need to "enable" your solution, check in nuget.exe or run custom scripts or msbuild files other than your solution file.

Add the Nuget package to each test project

Ensure that each of your test projects has the correct XUnit test runner package configured. You should be looking for "xUnit.net [Runner: Visual Studio]":

 PM> Install-Package xunit.runner.visualstudio



回答2:


One of the reasons to move to a NuGet-based distribution is that it should "just work" with Visual Studio (and Team Build) without needing to install anything on the build server.

That said, we don't officially support Team Build, because we don't have the bandwidth or the infrastructure to support it. If there are issues that need fixing, we would be happy to accept those fixes from the community.




回答3:


Finally I resolve that problem. xUnit didn't work with build because NuGet restore was called too late. It should be called before running MSBuild on solution. Detailed description can be found on the Nuget Documentation website.

After applying that solution it just works without installing VS or any other 3rd party extensions on build server.




回答4:


I had the same problem after the migration to xunit.net 2. You need to point your TFS Build controller to a folder with the required xunit.net assemblies (see: build controller properties / “Version control path to custom assemblies”).

Just copy all files from the subfolders under “build” of the NuGet package, in my case .\packages\xunit.runner.visualstudio.2.1.0-beta2-build1055\build, to the folder you configured in the test controller. Since TFS does not scan sub folders, it's important to flatten the hierarchy so the target folder only contains the file.



来源:https://stackoverflow.com/questions/30231314/having-trouble-enabling-the-nuget-version-of-xunit-net-on-team-build-2013

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!