In this post I am going to talk about writing unit tests in .NET Core with NUnit andwatch 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 writelots 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 theMicrosoft.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
torun. Here are some various dotnet-test-*
runners.
- dotnet-test-nunit. Runner for the NUnit test framework.
- dotnet-test-xunit. Runner for the XUnit test framework.
- dotnet-test-mstest. Runner for the MSTest test framework.
Note, that all therunners above are in prerelease stage, so when you look for them via NugetPackage 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 areaand 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.
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, Idid 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 tonetcoreapp1.0
, else you will get errors on build. Ialso put the Geometry
project reference under the dependencies
node.
To installthe watcher tool into project, use the tools
section and download the packageMicrosoft.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.
Whenever you make a change to your tests or yourcode 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 isat prereleasestage, 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.
Comments powered by Disqus.