Testing .NET Core RC2 Using NUnit 3

2016, May 18    

Update: full NUnit 3 support for .NET Core and ASP.NET Core has been released.

Things have changed since I wrote Testing .NET Core Using NUnit 3, so it is time to update the steps for .NET Core RC2.

The code from this post is on GitHub in the nunitlite and test_assembly branches. For those interested in an early peek at the new dotnet-test-nunit runner, there is an example on the master branch.

Upgrading your project to build on RC2 is a whole series of blog posts in and of itself, so I won't cover it here, but I will remind everyone, "All previous versions of .NET Core and any tooling must be removed from the machine in order to properly install and use RC2 release." I uninstalled everything, deleted all directories in Program Files that had DNX or DotNet in their name, deleted the .dnx directory in my user directory and combed through my user and global paths before installing .NET Core RC2. I didn't get everything on one machine and was getting strange errors.

So, let's go green field, 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;
}

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-preview1-002702"
  }
}

To this new test folder, add a new Console Application (.NET Core) as your test project and add a reference to the main project.

Add New Console Project

Now, add the NUnitLite NuGet package to the test project and change Main in Program.cs.

using NUnit.Common;
using NUnitLite;
using System;
using System.Reflection;

namespace NUnitWithDotNetCoreRC2.Test
{
    public class Program
    {
        public static int Main(string[] args)
        {
            return new AutoRun(typeof(Program).GetTypeInfo().Assembly)
                .Execute(args, new ExtendedTextWrapper(Console.Out), Console.In);
        }
    }
}

You can now add your tests to the project.

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));
        }
    }
}

You can now set the test project as your startup project and run your tests. If it helps, put a breakpoint in Main to see the output.

NUnitLite 3.2.1 (Portable)
Copyright (C) 2016 Charlie Poole

Test Files
    D:\Src\Spikes\NUnitWithDotNetCoreRC2\NUnitWithDotNetCoreRC2.Test\bin\Debug\netcoreapp1.0\NUnitWithDotNetCoreRC2.Test.dll

Run Settings
    Internal Trace: Off

Test Run Summary
  Overall result: Passed
  Test Count: 12, Passed: 12, Failed: 0, Inconclusive: 0, Skipped: 0
  Start time: 2016-05-18 14:47:56Z
    End time: 2016-05-18 14:47:56Z
    Duration: 0.070 seconds

All in all, for a green field app, then it is pretty straight forward. If you prefer that your tests are in a DLL, then you can follow the steps above, but possibly name the NUnitLite project as a runner and change the AutoRun to get the assembly from a class in your test project. If you prefer this method, I have done it in the test_assembly branch on GitHub.

I am in the process of upgrading a few RC1 projects, so if I find any other gotchas, I will update. For upgrades, one thing you will have to do is add an imports statement to your project.json to handle the renaming of the targets in RC2. This is done for you if you create a new project, but not if you are upgrading.

For project.json in a console runner;

  "frameworks": {
    "netcoreapp1.0": {
      "imports": "dnxcore50"
    }

Or in an assembly;

  "frameworks": {
    "netstandard1.5": {
      "imports": "dnxcore50"
    }