Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MSBuild - trying to run xUnit (.net) tests

I'm trying to set up a C# project that'll run xUnit tests when I build, so I can use them in continuous integration. I have a regular project, a class library test project using xUnit, and my test runner project. From everything I've read, it appears that I should be able to get this working by doing this in the test runner project:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Test"
    xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  [auto-created project stuff]
  <UsingTask AssemblyFile="xunit.runner.msbuild.dll"
      TaskName="Xunit.Runner.MSBuild.xunit" />
  <Target Name="Test">
    <xunit Assembly="$(MSBuildProjectDirectory)\..\OnePageOneDb.Tests\bin\Debug\OnePageOneDb.Tests.dll" />
  </Target>
</Project>

When I build my solution after a change (usually editing the .csproj file), I get this:

The "Xunit.Runner.MSBuild.xunit" task could not be loaded from the assembly C:\Users[myusername]\Code\OnePageOneDb\OnePageOneDb.TestRunner\xunit.runner.msbuild.dll. Could not load file or assembly 'file:///C:\Users[myusername]\Code\OnePageOneDb\OnePageOneDb.TestRunner\xunit.runner.msbuild.dll' or one of its dependencies. The system cannot find the file specified. Confirm that the declaration is correct, that the assembly and all its dependencies are available, and that the task contains a public class that implements Microsoft.Build.Framework.ITask.

Even if I add xunit.runner.msbuild.dll and xunit.runner.utility.dll to the project in the location it refers to, I get this message. But if I build again with no changes, I consistently get this:

The "xunit" task was not found. Check the following: 1.) The name of the task in the project file is the same as the name of the task class. 2.) The task class is "public" and implements the Microsoft.Build.Framework.ITask interface. 3.) The task is correctly declared with in the project file, or in the *.tasks files located in the "C:\Windows\Microsoft.NET\Framework\v4.0.30319" directory.

But I've checked all these things:

  1. The task class in xunit.runner.msbuild.dll is Xunit.Runner.MSBuild.xunit (and xunit is lowercase in the class name).
  2. The task class inherits from Task, which implements ITask.
  3. So maybe there's a problem in UsingTask, but I don't know what it is.

(I also thought the problem might be that xunit.runner.msbuild.dll is targeted at .NET 2.0, and I'm using VS 2010, but I recreated the test runner project in .NET 2.0 and the problem persisted.)

Can anyone help?

like image 413
Ryan Lundy Avatar asked Mar 01 '11 21:03

Ryan Lundy


1 Answers

You need to specify correct path to xunit.runner.msbuild.dll. First of all, you can just set the full path and test that xunit just works as you want. But for real environment you should specify relative path to the dll.

<UsingTask AssemblyFile="$(MSBuildProjectDirectory)\..\..\lib\xunit\xunit.runner.msbuild.dll"
           TaskName="Xunit.Runner.MSBuild.xunit" />

MSBuildProjectDirectory is a reserved property and contains "the absolute path of the directory where the project file is located".

EDIT:

Try to use target by full name Xunit.Runner.MSBuild.xunit

<Target Name="Test">
    <Xunit.Runner.MSBuild.xunit Assembly="$(MSBuildProjectDirectory)\..\OnePageOneDb.Tests\bin\Debug\OnePageOneDb.Tests.dll" />
</Target>
like image 157
Sergio Rykov Avatar answered Sep 24 '22 22:09

Sergio Rykov