Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XUnit working with .NET Core using Cake

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?

like image 431
Sean Hederman Avatar asked Sep 19 '17 19:09

Sean Hederman


People also ask

Does xUnit work with .NET core?

The xUnit.net test runner that we've been using supports . NET Core 1.0 or later, . NET 5.0 or later, and . NET Framework 4.5.

How do you write xUnit test cases in .NET core?

To write a test you simply create a public method that returns nothing and then decorate it with the Fact attribute. Inside that method you can put whatever code you want but, typically, you'll create some object, do something with it and then check to see if you got the right result using a method on the Assert class.

Is xUnit compatible with .NET framework?

I've been using xUnit for quite some time now, and it's my Unit testing framework of choice. It's an open source unit testing tool for . Net framework that's compatible with ReSharper, CodeRush, TestDriven.Net, and Xamarin. You can take advantage of xUnit.Net to assert an exception type easily.

Does xUnit run tests in parallel?

XUnit by default run all test tests in parallel which are sitting in separate class files. This makes it easy to run our test in parallel without us doing any special configuration.


1 Answers

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

Task("Test")
    .Does(() => 
    {
        DotNetCoreTest("./test/[TestProjectPath]/[TestProject.csproj]");
    });
like image 138
woodylic Avatar answered Sep 23 '22 03:09

woodylic