Debugging a single NUnit test in .NET Core

Note: An official NUnit test runner for .NET CLI has been released since writing this article. More info here.

Reason

Sadly there’s currently no way to easily debug specific NUnitLite tests via Visual Studio when running .NET Core applications, because there’s no suitable test runner and the NUnit 3.0 test adapter doesn’t support .NET Core yet.

Unsupported NUnit fixture

Until proper tooling is available via Visual Studio or 3rd party providers, “poor mans debugging” of single tests can be achieved using test categories and dnx/dotnet commands, as explained in the following.

Note: If you’re looking for a “HOW-TO NUnit + .NET Core” read this instead.

Code

The setup consists of three simple steps:

  1. Create a “debug” command which runs tests from a single test category.
  2. Annotate the tests you want to debug with the same category name as used in the debug command.
  3. Add breakpoints and start your project using the debug command.

If you’re familiar with the Jasmine test framework it’s similar to running focused specs.

Step 1 — add commands

To simplify running only some of our tests, we’ll add suitable commands to the project.json file of our test project.

A common scenario would be one command for debugging, one for running a subset of tests prior to pushing changes, and one for running all tests incl. slow integration and end-to-end tests on a build server.

The category include/exclude syntax is fairly self explanatory and is fully documented in the test selection language section of the NUnit wiki.

ASP.NET Core 1 RC1

This config should be backwards compatible down to ASP.NET 5 beta-6.

project.json:

[...]

"commands": {
"testDebug": "MyTestProject -where \"cat == Debug\"",
"testQuick": "MyTestProject -where \"cat != LongRunning and cat != SomeOtherCategory\"",
"test": "MyTestProject"
}

[...]

.NET Core 1 RC2

project.json:

[...]

"commands": {
"testDebug": "",
"testQuick": "",
"test": ""
}

[...]

launchSettings.json:

[...]

"profiles": {
"testDebug": {
"commandName": "testDebug",
"commandLineArgs": "-where \"cat == Debug\""
}
}

[...]

Step 2 — annotate tests

Annotate the fixtures and/or tests you want to debug using the NUnit category attribute and the category name used in the previous step (e.g. [Category("MyCategory")]).

NUnit fixture

Step 3 — debug tests

Start your test project using the appropriate command created in step 1.

Running a command

Step 3½ — clean-up

Remember to remove the temporary category attributes used for debugging prior to pushing changes 🙂

Example

A sample debug session can be seen here.

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