Testing .NET Core RC2 Using NUnit 3

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"
    }
  • Pingback: NUnit 3 Tests for .NET Core RC2 and ASP.NET Core RC2 - Rob Prouse()

  • Rob Prouse

    Thanks for getting back with the fix. I haven’t seen the warning, but others may.

  • Martin

    I have fixed it, there were some .csprojResolveAssemblyReference.cache files, I deleted those and the above warning has gone.

  • Martin

    I am getting this warning:

    Warning CS1701 Assuming assembly reference ‘System.Runtime, Version=4.0.20.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a’ used by ‘Microsoft.AspNetCore.Authorization’ matches identity ‘System.Runtime, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a’ of ‘System.Runtime’, you may need to supply runtime policy CSWebSite..NETCoreApp,Version=v1.0

    Can I safely ignore this or is there a fix?

  • Thanks for your quick reply and keep up the good work for NUnit.

  • Rob Prouse

    Andre, I have been sticking with official releases so I have not tried 002973 yet. I have actually been working pretty hard on getting dotnet-test-nunit working so that we have a full test runner for NUnit, so I haven’t had time to keep up with the .NET Core builds, sorry.

  • Thanks for providing this example. I have recently switched to version 1.0.0-preview2-002973 of .NET Core and after that I can’t run my tests any more. The execution fails with the following message:

    Invalid : CMS.Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
    Could not load file or assembly ‘System.Runtime.InteropServices.PInvoke, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a’. Das System kann die angegebene Datei nicht finden.

    The last part is German as I’m running a German version of Windows. The message simply is ‘File not found.’. Can you confirm this also happens on your system?

    Thanks.

  • Pingback: Testing .NET Core using NUnit 3 | Alteridem [al′tər ī′dem′]()