6

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?

Ryan Lundy
  • 204,559
  • 37
  • 180
  • 211

3 Answers3

2

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>
Sergio Rykov
  • 4,176
  • 25
  • 23
  • I've tried it with AssemblyFile getting the DLL from the project directory itself, or from the bin\Debug directory, or a host of other places, and I get the same result. I also tried just making a new simple task that does nothing but log a message during build. I get the same "task was not found" message. I've tried the TaskName with namespace and without. I've tried AssemblyName instead of AssemblyFile. I've tried a hard-coded (C:\[etc]) path to AssemblyFile. I'm beginning to wonder if the ability to do this was simply broken in VS 2010. – Ryan Lundy Mar 02 '11 at 15:44
  • In the end, what I determined was: **TaskName** must be Namespace.ClassName of task class; **AssemblyFile** must be absolute or relative path to assembly; **Name** of Target doesn't matter, unless it's BeforeBuild or AfterBuild; **Tag inside Target** should be class name of task but no namespace. – Ryan Lundy Mar 03 '11 at 20:13
  • But with all that, it didn't work; when I had the xunit task set up correctly, it crashed Visual Studio. So I gave up and went with a batch command running xunit console instead. – Ryan Lundy Mar 03 '11 at 20:14
0

By default building in "release" configuration triggers running xunit tests. If you are trying to disable running xunit tests in tfsbuild pass the following build parameter. This can be useful in the new cross platform builds where running unit tests is a separate step.

/p:RunXunitTests=false
Edward Olamisan
  • 800
  • 1
  • 18
  • 28
0

I get exactly the same error message if I have Pex and Moles installed. Everything works fine after uninstalling them.

vlacko
  • 51
  • 1
  • 2