Watch NUnit tests in .NET Core

In this post I am going to talk about writing unit tests in .NET Core with NUnit and watch them for changes, re-running the test suite again, on the fly. Something you can have with NCrunch, but .NET Core has it for free.

TLDR; Code repository can be found here.

I really like .NET Core. I’ve been playing around for some time now and enjoyed it. I find it very flexible, modular, you can work cross-platform, work with the CLI, etc., stuff you probably already know. I also write lots of tests. And I want automation for them.
Recently, I have been playing with NCrunch and I really like it, having a bunch of tests running automatically for me without relying on re-running the whole suite.
At the moment, I am still reading and learning from the NCrunch documentation and I’m getting more familiar with tool, which is very cool.

But, .NET Core gives us a watch tool for free, which of course it is not as rich as NCrunch is, so you can call it NCrunch for the poor. You just need to download the Microsoft.DotNet.Watcher.Tools, run the command from the CLI and that’s it.

How you can run unit tests in .NET Core though? Simple, you need a test runner for dotnet test to run. Here are some various dotnet-test-* runners.

Note, that all the runners above are in prerelease  stage, so when you look for them via Nuget Package Manager, make sure to tick the ‘Include prerelease’ checkbox.

At the repository you will find a solution which has two .NET Core class library projects, the Geometry, which contains a class named Triangle to make a simple calculation on a triangle’s area and the Geometry.Tests project, which tests the Triangle class methods.

To create a new project, you can do it either by using the CLI or Visual Studio. With CLI you type dotnet new <NAME.OF.PROJECT> on the folder where you want to place the project.

But for this example, I am going to use just the VS 2015 IDE.

So, in order to create a new .NET Core class library project, select File > Add > New Project. Select .NET Core > Class Library (.NET Core) from the wizard.

newdotnetcoreproject

The Geometry project is simple enough, the project.json file is the following, targeting the netcoreapp1.0 framework node.


{
  "version": "1.0.0-*",

  "dependencies": {
    "NETStandard.Library": "1.6.0"
  },

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

Class Triangle is trivial as well.


namespace Geometry
{
    public class Triangle
    {
        private const double Half = 0.5;

        public double Area(double width, double height)
        {
            return width * height * Half;
        }
    }
}

For the test project, I created a new Class Library, same way as previously. But, I did some modifications on tooling and dependencies. All are reflected on the project.json file of the Geometry.Tests project.


{
  "version": "1.0.0-*",

  "dependencies": {
    "dotnet-test-nunit": "3.4.0-beta-3",
    "NETStandard.Library": "1.6.0",
    "NUnit": "3.5.0"
  },
  "testRunner": "nunit",
  "tools": {
    "Microsoft.DotNet.Watcher.Tools": "1.0.0-preview2-final"
  },
  "frameworks": {
    "netcoreapp1.0": {
      "imports": "portable-net45+win8",
      "dependencies": {
        "Microsoft.NETCore.App": {
          "version": "1.0.0-*",
          "type": "platform"
        },
        "Geometry": "1.0.0-*"
      }
    }
  }
}

I downloaded the NUnit package, for the NUnit framework, as well as the dotnet-test-nunit runner for .NET Core. In order to have the runner compatible with the project, you need to target the framework node to netcoreapp1.0, else you will get errors on build. I also put the Geometry project reference under the dependencies node.

To install the watcher tool into project, use the tools section and download the package Microsoft.DotNet.Watcher.Tools. This will have all the required CLI tools to run the dotnet watch ... command.

Lastly, I set the testRunner to nunit to specify NUnit 3 as the test adapter. Similarly with other frameworks you do the same, for XUnit you set it to xunit, for MSTest you set it to mstest.

I wrote just one test for demonstration, which checks if the Area method truly returns a valid area for a triangle.

The formula is area = 1/2*base*height


namespace Geometry.Tests
{
    using NUnit.Framework;
    using static NUnit.Framework.Assert;

    [TestFixture]
    public class TriangleTests
    {
        [Test]
        public void Triangle_Area_OfSixBaseAndThreeHeight_IsNine_Test()
        {
            // Arrange
            const double @base = 6;
            const double height = 3;
            var triangle = new Triangle();

            // Act
            var result = triangle.Area(@base, height);

            // Assert
            AreEqual(9.0d, result);
        }
    }
}

In order to have the watcher running, open the test project folder and open a CMD window. Type dotnet watch test on that directory.

dotnetcoretestresults

Whenever you make a change to your tests or your code under test, watcher will re-run the test suite.

Summary

Run your unit tests for .NET Core using the runner of your choice, but remember to do some configuration before jumping to tests.

Also, note that the tooling is at prerelease stage, so expect to have problems from time to time. For example, a problem I had was that sometimes my VS 2015 crashed when running the watcher and made changes to project.json of a project.

Advertisements

2 thoughts on “Watch NUnit tests in .NET Core

  1. Great article. It is good to see the NUnit runner is working for you. Let me know if you have any issues. It is unfortunate though that the runner will become obsolete with VS2017 with the change to CSPROJ. We need to port our old adapter to .NET Core now 😦

    Liked by 1 person

    1. Wow! Thank you for taking some time to read through and comment this article and also thanks for the heads-up on runner’s future with VS2017! Will have to go and read the changes in details. Thanks again.

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s