1
Vote

Add msbuild targets file to xUnit NuGet package

description

UPDATE: This issue has been moved to GitHub

Problem

According to the wiki page titled How To Use MSBuild, one can use markup similar to the following to get xUnit MsBuild support.
<Project
    DefaultTargets="Test"
    xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

  <UsingTask
    AssemblyFile="xunit.runner.msbuild.dll"
    TaskName="Xunit.Runner.MSBuild.xunit"/>

  <Target Name="Test">
    <xunit Assembly="MyTests\bin\Debug\MyTests.dll" />
  </Target>

</Project>
This approach works well. However, it's not as clean when referencing the xUnit NuGet package in a .csproj file. To do this, one would need markup similar to the following:
<Project
    DefaultTargets="Build"
    xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

  <!-- .csproj file contents ommitted -->

  <UsingTask
    AssemblyFile="..\packages\xunit.1.9.2\lib\net20\xunit.runner.msbuild.dll"
    TaskName="Xunit.Runner.MSBuild.xunit"/>

  <Target Name="AfterBuild">
    <xunit Assembly="MyTests\bin\Debug\MyTests.dll" />
  </Target>

</Project>
In the example above, the path to the xunit.runner.msbuild.dll file found in the NuGet package is hard coded in the .csproj file. While this works, it's not ideal. The approach is tied to a specific version of the NuGet package. When version 1.9.3 or 2.0.0 of xUnit comes out, the developer will need to edit the .csproj file to point to the new package.

Proposal

NuGet provides a means to include .props and .targets files that are inserted into a .csproj file when the NuGet package is applied. One option would be to include a .targets file that declared the UsingTask similar to how the new xUnit Visual Studio runner imports its test adapter.

The structure of the existing xUnit NuGet Package (1.9.2) looks like this:
+---xunit.1.9.2
    \---lib
        \---net20
            |---xunit.dll
            |---xunit.dll.tdnet
            |---xunit.runner.msbuild.dll
            |---xunit.runner.tdnet.dll
            |---xunit.runner.utility.dll
            \---xunit.xml
The proposed structure would look like this:
+---xunit.1.9.2
    |---lib
    |   \---net20
    |       |---xunit.dll
    |       |---xunit.dll.tdnet
    |       |---xunit.runner.msbuild.dll
    |       |---xunit.runner.tdnet.dll
    |       |---xunit.runner.utility.dll
    |       \---xunit.xml
    \---build
        \---xunit.targets
The new .targets file could be similar to the following:

xunit.targets

<Project
    xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

  <UsingTask
    AssemblyFile="$(MSBuildThisFileDirectory)..\lib\net20\xunit.runner.msbuild.dll"
    Condition="Exists('$(MSBuildThisFileDirectory)..\lib\net20\xunit.runner.msbuild.dll')"
    TaskName="Xunit.Runner.MSBuild.xunit"/>
  
</Project>
This would reduce the amount of changes to the .csproj file a developer would need to do to run tests on build. The first example could now be reduced to the following:
<Project
    DefaultTargets="Build"
    xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

  <!-- .csproj file contents ommitted -->

  <Target Name="AfterBuild">
    <xunit Assembly="$(TargetPath)" />
  </Target>

</Project>

comments

bartelink wrote Jan 27, 2015 at 6:04 AM

GitHub!