MSBuild - trying to run xUnit (.net) tests

断了今生、忘了曾经 提交于 2020-01-14 09:08:09

问题


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?


回答1:


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>



回答2:


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




回答3:


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


来源:https://stackoverflow.com/questions/5160480/msbuild-trying-to-run-xunit-net-tests

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