XUnit working with .NET Core using Cake
Asked Answered
B

2

6

I have a relatively simple solution. All works fine under MSBuild (in VS 2017 Mac). I'm creating a Cake build script, but I just cannot get unit testing to work. There are tons of examples, but it seems that none are valid anymore. Some call for DotnetCoreTest, most for XUnit2. Neither work at all in my project.

I get the following error when running XUnit2: System.InvalidOperationException: Unknown test framework: could not find xunit.dll (v1) or xunit.execution.*.dll (v2) in /Users/dev/Projects/MyProject/tst/MyProject.Serialization.UnitTests/bin/Release/netcoreapp1.1

And that output is absolutely valid, the file is in fact not there. Here are the references for my unit test project:

<ItemGroup>
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0" />
    <PackageReference Include="xunit" Version="2.2.0" />
    <PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />
    <PackageReference Include="xunit.runner.console" Version="2.2.0" />
    <PackageReference Include="FluentAssertions" Version="4.19.2" />
    <PackageReference Include="Moq" Version="4.7.99" />
    <PackageReference Include="FluentAssertions.AspNetCore.Mvc" Version="0.7.0-beta1" />
    <PackageReference Include="AutoMapper" Version="6.1.1" />
    <PackageReference Include="ExpectedObjects" Version="1.3.0" />
    <PackageReference Include="OpenCover" Version="4.6.519" />
    <PackageReference Include="xunit.extensibility.execution" Version="2.2.0" />
</ItemGroup>

I have referenced xunit.runner.console AND xunit.extensibility.execution, yet these are not copied to the destination directory. .NET Core doesn't appear to have any way to force that, and even if I copy them manually the test assemblies still won't load.

Here is my Cake Test script:

#tool nuget:?package=NUnit.ConsoleRunner&version=3.4.0
#tool nuget:?package=xunit.runner.console
...
Task("Test")
    .IsDependentOn("Build")
    .Does(() =>
{
    Information("Start Running Tests");
    XUnit2(string.Format("./tst/**/*Tests/bin/{0}/**/*Tests.dll", configuration), new XUnit2Settings
    {
    });
});

Clearly I'm missing something very basic, but with all the conflicting information out there I don't know what it is. Should I not be using XUnit?

Bouncy answered 19/9, 2017 at 19:0 Comment(4)
Is this a public rewpository? Have you also added xunit as a #tool preprocessor in your Cake script?Jaynes
Unfortunately not :-( Yes, I have the #tool directives. Have edited question to show.Bouncy
Can you create a small sample application, hosted on GitHub or similar, which illustrates the issue that you are having?Jaynes
Maybe take a look at flubu which is a nice alternative to cake. Flubu is A C# library for building projects and executing deployment scripts using C# code. More at: github.com/flubu-core/flubu.core/wikiDiatropism
H
7

I encounter the same issue, resolve it by using DotNetCoreTest instead of XUnit2.

Task("Test")
    .Does(() => 
    {
        DotNetCoreTest("./test/[TestProjectPath]/[TestProject.csproj]");
    });
Hanson answered 26/9, 2017 at 2:7 Comment(0)
R
2

I stumbled upon a similar issue and found the following solution for the latest xUnit v2.4.0 and want to share it for future travellers. With this, the tests are discovered in Visual Studio and can be executed through command line with the CAKE build files.

Stack

  • Visual Studio 2017 Community Edtion (v15.8.4)
  • .NET Core 2.1
  • Cake Build
  • Windows 10

Test Project

The project is called MyProject.Tests and is located in the \test\MyProject.Tests folder relative to the solution folder. The MyProject.Tests.csproj project file includes the packages xunit and xunit.runner.visualstudio at their current version v2.4.0. The latter is required to discover the tests.

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netcoreapp2.1</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
   <PackageReference Include="xunit" Version="2.4.0" />
   <PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
  </ItemGroup>

</Project>

CAKE

The build.cake has the following code. The Task("Test") gets all projects in the \test folder that end with .Tests and executes them.

#tool nuget:?package=xunit.runner.console

// Configuration argument that can be passed via command line
// Default is "Release"
var configuration = Argument("configuration", "Release");

// ... omitted for brevity

// Run all test projects located in ./test folder
Task("Test")
  .IsDependentOn("Build")
  .Does(() =>
  {
    var projects = GetFiles("./test/**/*.Tests.csproj");
    foreach(var project in projects)
    {
      DotNetCoreTest(
        project.FullPath,
        new DotNetCoreTestSettings()
          {
            // Set configuration as passed by command line
            Configuration = configuration
          });
    }
});
Recurved answered 28/9, 2018 at 7:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.