NUnit 3 Tests for .NET Core RC2 and ASP.NET Core RC2

Update 2: The switch from the project.json format to the new csproj format and the associated tooling changes made the dotnet-test-nunit adapter obsolete by switching the test adapter API. I have released an update to the NUnit Visual Studio Adapter that implements the new test API and supports .NET Core. If you are using Visual Studio 2017 and/or the new .NET Core tooling, see Testing .NET Core with NUnit in Visual Studio 2017.

Update: I have released beta 1 of dotnet-test-nunit which has been updated to the RTM of .NET Core 1.0. I have updated this post and the code on GitHub to reflect the changes.

The NUnit team has been working hard since .NET Core RC2 and ASP.NET Core RC2 was released last month to add full NUnit testing for .NET Core, and we are happy to announce the second alpha release of dotnet-test-nunit.

In my previous blog posts, I explained how to use NUnit to test .NET Core using NUnitLite which worked, but was not the ideal solution. The new dotnet-test-nunit allows you to test from the command line using the dotnet test command and allows you to run your tests within Visual Studio.

The code from this post is on GitHub and is based on the code from my previous post.

Create a .NET Core Project

In Visual Studio, File | New | Project… and select Class Library (.NET Core) under the new C# | .NET Core node.

New .NET Core Project

This it the code under test. To this, I have added the simple calculator class from the last post.

public static class Calculator
{
    public static int Add(int x, int y) => x + y;
    public static int Subtract(int x, int y) => x - y;
    public static int Multiply(int x, int y) => x * y;
    public static int Divide(int x, int y) => x / y;
}

Create an NUnit .NET Core Test Project

If you look in global.json, the new standard is to put your code in src and tests in test directories. To conform with this, add a New Solution Folder named test.

{
  "projects": [ "src", "test" ],
  "sdk": {
    "version": "1.0.0-preview2-003121"
  }
}

To this new test folder, add another Class Library (.NET Core) as your test project and edit the project.json based on the following;

{
    "version": "1.1.0-*",

    "dependencies": {
        "NUnitWithDotNetCoreRC2": {
            "target": "project"
        },
        "NUnit": "3.4.0",
        "dotnet-test-nunit": "3.4.0-beta-1"
    },
    "testRunner": "nunit",

    "frameworks": {
        "netcoreapp1.0": {
            "imports": [
                "netcoreapp1.0",
                "portable-net45+win8"
            ],
            "dependencies": {
                "Microsoft.NETCore.App": {
                    "version": "1.0.0-*",
                    "type": "platform"
                }
            }
        }
    }
}

Under dependencies, NUnitWithDotNetCoreRC2 is the project reference to the project under test. There are also dependencies on NUnit and the new dotnet-test-nunit.

After the dependencies, we specify that we want to use NUnit for testing with the line "testRunner": "nunit".

Under frameworks, we need to add to the imports because NUnit is still a PCL assembly. The .NET Standard (netstandard) version of the NUnit Framework won’t likely be ready until the 3.6 release this summer. We are waiting until after the final .NET Core release to start that work.

Add Tests

Next, you can add your tests to the project by adding a new class;

using NUnit.Framework;

namespace NUnitWithDotNetCoreRC2.Test
{
    [TestFixture]
    public class CalculatorTests
    {
        [TestCase(1, 1, 2)]
        [TestCase(-1, -1, -2)]
        [TestCase(100, 5, 105)]
        public void CanAddNumbers(int x, int y, int expected)
        {
            Assert.That(Calculator.Add(x, y), Is.EqualTo(expected));
        }

        [TestCase(1, 1, 0)]
        [TestCase(-1, -1, 0)]
        [TestCase(100, 5, 95)]
        public void CanSubtract(int x, int y, int expected)
        {
            Assert.That(Calculator.Subtract(x, y), Is.EqualTo(expected));
        }

        [TestCase(1, 1, 1)]
        [TestCase(-1, -1, 1)]
        [TestCase(100, 5, 500)]
        public void CanMultiply(int x, int y, int expected)
        {
            Assert.That(Calculator.Multiply(x, y), Is.EqualTo(expected));
        }

        [TestCase(1, 1, 1)]
        [TestCase(-1, -1, 1)]
        [TestCase(100, 5, 20)]
        public void CanDivide(int x, int y, int expected)
        {
            Assert.That(Calculator.Divide(x, y), Is.EqualTo(expected));
        }
    }
}

Running Your Tests

Now you can run your tests in Visual Studio, or from the command line. In Visual Studio, you may need to reload the solution before your changes are picked up, then your tests will appear in Test Explorer after you build the solution.

NUnit testing .NET Core in Visual Studio

In addition, use the following commands to run your tests from the command line;

# Restore the NuGet packages
dotnet restore

# Run the unit tests in the current directory
dotnet test

# Run the unit tests in a different directory
dotnet test .\test\NUnitWithDotNetCoreRC2.Test\

Notes

Since this is an alpha release, so there are still likely bugs, but it is already being used by some large open source projects so is likely stable for production. Please report any bugs or issues to the dotnet-test-nunit repository on GitHub.

Finally, the dotnet command line swallows blank lines and does not work with color. The NUnit test runner’s output is in color, but you won’t see it. These are known issues with the dotnet CLI and not an NUnit bug.

  • Rob Prouse

    Andrew, I responded to your question on Stack Overflow so more people could find the answer. In short, when .NET Core switched from the project.json format to the csproj format, it dropped support for the original .NET Core test adapter API, so the dotnet-test-nunit adapter no longer works with Visual Studio 2017 or the new .NET Core command line tools.

    The new csproj format uses the old Visual Studio test adapter API, so the NUnit team (me) needed to update that test adapter to support .NET Core. That work is nearly done.

  • Hi Rob,

    Thank you for your article.

    I try to use it in Visual Studio 2017, but Test Explorer doesn’t see my tests (for ASP.NET Core MVC). I described my problem here:
    http://stackoverflow.com/questions/43494462/why-test-explorer-doesnt-see-my-nunit-tests-for-asp-net-core-mvc

    I would be very grateful to you for the help in this question.

    Best regards,
    Andrey

  • Hi Rob. Thanks. I was able to get my unit testing running. Although, I imagine because I am new to working with NET Core, and Standard (I am using 1.6), that I found myself hacking about to get it going.

  • Rob Prouse

    Adam, NUnit 3.6 includes a .NET Standard and a PCL, so the instructions in this post should work for you. What we (NUnit) still need to do is update our adapters to work with Visual Studio 2017 and the new CSPROJ .NET Core projects. The adapter API has changed, so moving to VS2017 and CSPROJ cause this adapter to stop working.

  • Hi Rob. Thanks for your work here. Regarding your statement “The .NET Standard (netstandard) version of the NUnit Framework won’t likely be ready until the 3.6 release this summer.”, would you have any updates on how to accomplish this task ? I am using VS 2015, with NETStandard1.6.1, NUnit3.6.1, and my immediate issue is have VS recognize my unit tests.

  • I’m playing now with a command line call to dotnet test and a pickup of TestResults.xml – We shall see what we shall see….

  • Rob Prouse

    I don’t use VSTS very often, but my understanding is that the Unit Test task does not work with .NET Core. You need to run dotnet test directly and then publish the results. See https://github.com/Microsoft/vsts-tasks/issues/2126

    I am not sure if VSTS supports the NUnit 3 result format yet though, so you might run into problems with that since the NUnit .NET Core runner doesn’t support the v2 format yet (ever?). I think there is a pull request outstanding to add support for the v3 format.

    If you get it working, I would appreciate it if you could report back here or on GitHub.

  • How does this tie in with VSTS / Visual Studio Online? I’ve got everything working locally but I can’t seem to get anywhere with the build server.

  • Rob Prouse

    I believe that the R# team is working on .NET Core support, but I don’t think it has been released yet. You can check their EAPs.

    An alternative for running tests in Visual Studio is TestDriven.NET. Jamie has just added .NET Core support. It isn’t released yet, but you can get a copy at https://github.com/jcansdale/TestDriven.Net-Issues/issues/6

  • andrecarlucci

    Any chance of getting them to run using resharper test runner?

  • Rob Prouse

    I haven’t figured out how to do it myself, but I have mainly been using Visual Studio and only have limited experience debugging in Visual Studio Code. If you figure it out, I would appreciate it if you post your solution.

    Good luck…

  • Иван Архипов

    Hello! Is there a way to debug NUnit test with Visual Studio Code? I changed launch.json “.NET Core Launch (console)” section parameters with
    “program”: “/usr/bin/dotnet”, “args”: [“test”] but breakpoints doesn’t working. I see that tests running, but i can’t debug them. Thanks!

  • Pingback: Matt Ward: .NET Core Test Runner Support in Xamarin Studio – XamGeek.com()

  • Rob Prouse

    Daniel, I would suggest that you watch this issue on GitHub, https://github.com/nunit/dotnet-test-nunit/issues/42

    We’re all learning this .NET Core stuff together and that is why this is still in beta. I will be posting to the GitHub issue as we learn more.

  • Lets say I have a project that targets netstandard1.1, is there a way to get this setup to work without having to add netcoreapp1.0 to my project under test? I’ve tried to use imports in my test project but that fails hard.

  • Pingback: The week in .NET – 6/21/2016 | Tech News()

  • Pingback: The week in .NET – 6/21/2016 | 神刀安全网()

  • Pingback: Testing .NET Core using NUnit 3 - Rob Prouse()

  • Pingback: Testing .NET Core RC2 Using NUnit 3 - Rob Prouse()