This article is old and outdated. The official MS Documentation on testing has a wealth of information that is more up to date. I’m leaving this here in the slim chance it helps someone out who is on an extremely old version of .NET Core (<1.1), but otherwise, to the official documentation we go!
With .net core comes a new way to build and run unit tests with a command line tool named “dotnet test”. While the overall syntax of writing tests using MSTest, XUnit or NUnit hasn’t changed, the tooling has changed substantially from what people are used to. There is even a couple of gotchas that are enough to cause migraines if you aren’t careful.
Annoyingly, many of the packages you need to get started are in pre-release, and much of the documentation is locked into the old style DNX tooling. This article focuses on .net Core 1.1 – Preview 2. If you are already running Preview 4 then much of this will still apply, but you will be working with csproj files instead of project.json. Once Preview 4 gets a proper release, this article will be updated to reflect the (likely) many changes that go along with it.
Let’s get started.
The Basics Using MSTest
We will go over the basics using the MSTest framework. If you prefer to use NUnit or XUnit there are sections below to explain how they run within dotnet test, but still read through this section as it will give you the general gist of how things work.
Create a .net core console project in your solution. Now you are probably used to creating a class library for unit tests or maybe even a “unit test” project. We do not use the “Unit Test” project type as there is no such type for .net core, only the full framework. And we do not use a class library only for the reason that it defaults the target framework to .net standard, not .net core. It’s not that we are going to be building a console application per-say, it’s that the defaults for a console application in terms of project.json are closer to what we need.
Once the project is created, if you are on .net core 1.1 or lower, open up project.json. Delete the entire “buildOptions” node. That’s this node :
"buildOptions": { "emitEntryPoint": true }
You can also go ahead and delete the Program.cs file as we won’t be needing it.
Inside that project, you need to install a couple of packages.
You first need to install the MSTest Framework package from here. At this point in time it is still in Pre-Release so you need the “Pre” flag (At some point this will change).
Install-Package MSTest.TestFramework -Pre
And now install the actual runner package from here. Again, it’s in Pre-Release so you will need the “Pre” flag.
Install-Package dotnet-test-mstest -Pre
Now go back to your project.json and add in a “testrunner” node at the top level called “mstest”. All in all, your project.json should look like the following :
{ "version": "1.0.0-*", "testRunner": "mstest", "dependencies": { "dotnet-test-mstest": "1.1.2-preview", "Microsoft.NETCore.App": { "type": "platform", "version": "1.0.1" }, "MSTest.TestFramework": "1.0.8-rc2" }, "frameworks": { "netcoreapp1.0": { "imports": "dnxcore50" } } }
Great! Now, for the purpose of this article we will create a simple class called “UnitTests.cs”, and add in a couple of tests. That file looks like the following :
[TestClass] public class UnitTests { [TestMethod] public void TestMethodPass() { Assert.IsTrue(true); } [TestMethod] public void TestMethodFail() { Assert.Fail(); } }
Open a command prompt in your unit test directory. Run “dotnet test”. You should see something like the following :
And you are done! You now have a unit test project within .net core.
Using NUnit With Dotnet Test
Again, follow the steps above to create a console application, but remove the buildOptions node in your project.json.
Add in your NUnit nuget package by running the following from the package manager console :
Install-Package NUnit
Now you need to add the NUnit test runner. Install the following nuget package. Note that at the time of this article, the package is only in pre-release so you need the Pre flag :
Install-Package dotnet-test-nunit -Pre
Inside your project.json, you need a top level node called “testRunner” with the value of “nunit”. If you’ve followed the steps carefully, you should have a project.json that resembles the following :
{ "version": "1.0.0-*", "testRunner": "nunit", "dependencies": { "dotnet-test-nunit": "3.4.0-beta-3", "Microsoft.NETCore.App": { "type": "platform", "version": "1.0.1" }, "NUnit": "3.6.0" }, "frameworks": { "netcoreapp1.0": { "imports": "dnxcore50" } } }
For the purpose of this article, I have the following test class :
[TestFixture] public class UnitTests { [Test] public void TestMethodPass() { Assert.Pass(); } [Test] public void TestMethodFail() { Assert.Fail(); } }
Open a command prompt inside your project folder and run “dotnet test”. You should see something similar to the following :
Using XUnit With Dotnet Test
Again, follow the steps above to create a console application, but remove the buildOptions node in your project.json.
Add in XUnit Core nuget package to your project. In your package manager console run :
Install-Package xunit
Now add in the XUnit test runner with the following command in your package manager :
Install-Package dotnet-test-xunit -Pre
Open up your project.json. Add in a top level node named “testrunner” with the value of “xunit”. All going well, your project.json should resemble something close to the following :
{ "version": "1.0.0-*", "testRunner": "xunit", "dependencies": { "dotnet-test-xunit": "2.2.0-preview2-build1029", "Microsoft.NETCore.App": { "type": "platform", "version": "1.0.1" }, "xunit": "2.1.0" }, "frameworks": { "netcoreapp1.0": { "imports": "dnxcore50" } } }
For this article, I have the following test class :
public class UnitTests { [Fact] public void TestMethodPass() { Assert.True(true); } [Fact] public void TestMethodFail() { Assert.True(false); } }
Open up a command prompt in your project folder. Run the command “dotnet test” and you should see something similar to the following :