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.

Source Code

For code outlined in this article, the related GitHub 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.

Class Triangle is trivial as well.

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.

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

In order to have the watcher running, open the test project folder and open a CMD window. Type the following 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.

If you liked this blog post, please like, share and subscribe! For more, follow me on Twitter @giorgosdyrra.