Writing and running NUnit tests for .NET Core projects

What is actual state of .NET Core development for those who write NUnit tests?

Can we write cross-platform applications testing them with NUnit?

Is there are project templates like xUnit has?

Is there a tooling support for NUnit on par with xUnit and MSTest?

Couple days ago I asked myself these three questions. Here I'll show the results of exploration on the first day of summer 2017.

Post will contain many links, they are here just for me for memory.

.NET Core project system

There was some perturbations in .NET Core tooling world.

Initially .NET Core team implemented package.json-based project system . Many efforts the community made to implement tooling support for this ecosystem (test runners, IDE tooling etc). Rob Prouse written post about using dotnet-test-nunit package to support running NUnit tests with dotnet test command. This test runner can run your NUnit tests if you have package.json-based project.

Then .NET Core team decided to throw away package.json-based projects (they are deprecated now) in favor to well-known MSBuild-based format (aka csproj). Good news, they are simplifiedcsproj files significantly. Bad news, all the tooling for package.json projects will not work with it (like dotnet-test-nunit) and will never be updated in future.

To the future, when you start writing a new project, choose csproj-based project.

NUnit support for .NET Core

To support .NET Core, libraries should be targeted to .NET Core App or .NET Standard. .NET Standard is promising, all new cross-platform development should be targeted to it.

.NET Standard 1.6 and lower has many missing parts. .NET Standard 2 will be released in the fall and will implement many familiar APIs to the place. You can try now .NET Standard 2 preview, the future is at hand. I tried it and was pleasantly surprised that I can now use XslCompiledTransform class in .NET Core!

NUnit has been targeted to run in .NET Core (DNX, actually) since start and to .NET Standard 1.6 since v3.6.0. You can use latest version and be confident it will work with .NET Core.

NUnit Test Project Template

There is xUnit Test Project template for dotnet new command out of the box. There was efforts to implement such support for NUnit too, PR was merged by .NET Core CLI team. But since then dotnet new command has changed its templating engine and the team forgot to create new templates for NUnit.

Running NUnit tests

There is dotnet test command to running unit tests. This command will work with Visual Studio Test Runner Adapters which targeted to .NET Standard or .NET Core App. There is efforts to do this for NUnit, NUnit3TestAdapter v3.8.0-alpha1 now has .NETCoreApp 1.0 targeting and can be used with dotnet test command.

To add such support for your test project you should add following packages to your csproj:

Try it out! Below I have one passing test then I have changed it to fail. Watcher rebuilds project and rerun all the tests. We see that second run has one failed test, watcher works just like Swiss clock :)

Conclusion

Despite preview status of the .NET Core 2, .NET Standard 2 and NUnit3TestAdapter, I think that the tooling is ready for real tasks. I'm glad to see that with .NET Standard 2 targeting I can compile my friend's reflection-heavy project without any change of source code! Now we can fully use NUnit tests for .NET Core projects, and we have same tooling support like other unit-tests frameworks supported by Microsoft.