Welcome!

How to get NUnit up and running with .NET Core

There are a couple of issues getting .NET Core projects working with NUnit and the built in test runner UI in Visual Studio for Mac v7.2. As with many things in life, they are easy to resolve once you know how.

Problem #1: No NUnit template project

The first problem, at least in Visual Studio for Mac v7.2, is that there is no official NUnit project template that supports testing .NET Core projects. That last bit's important. There is an NUnit project template but it only works with the full .NET Framework. Trouble is, you won't discover that until you reference the .NET Core project.

Fortunately, you can create your own NUnit test project very easily - it's just a case of adding a couple of NuGet packages. The solution comes from this great article by Rob Prouse Testing .NET Core with NUnit in Visual Studio 2017

Visual Studio screen-shot of NuGet packages
Required NuGet packages

It's the same steps in Visual Studio for Mac, as follows:

  1. Create a new .NET Core Class Library project
  2. Add the following NuGet packages
    • NUnit
    • NUnit3TestAdapter
    • Microsoft.NET.Test.Sdk
  3. Restart Visual Studio

Problem #2: Can't add more than 9 tests

This problem affects Visual Studio for Mac v7.2 and earlier. It kicks in when you create more than 9 tests. The test runner UI in Visual Studio for Mac gets completely screwed up and prevents you from running any tests.

At first I thought it was something I'd done wrong. After a lot of hair pulling and a few wrong turns on Google, I discovered it's actually a bug in Visual Studio. The bug results in the test framework not being able to discover more than 9 tests. It's been fixed in Visual Studio for Mac version 7.3 Preview. At the time of writing this version was still in beta so you need to do a bit of fiddling around to get the beta version.

Visual Studio screen-shot - check for updates
Checking for updates

Firstly, you'll need to manually check for updates using the system menu. On the Visual Studio update dialog, switch the Update channel to beta. Visual Studio should then list Visual Studio for mac v7.3 preview as an update.

Visual Studio screen-shot - switch to beta update channel
Switching to the beta update channel

Install the update and after restarting Visual Studio you should now be able to add more than 9 unit tests.