In my previous posts, we learned how to write your first dotnet core app and debugging dotnet core on mac. Now in this post, we will learn how to write unit tests for the dotnet core app
There are two parts to get your unit tests up and running for a dotnet core app.
- A unit test framework to write the actual tests
- A test runner to execute the tests
Fortunately, there are Nuget packages published by Microsoft to accomplish both of these tasks. As of this writing both of these packages are in pre release.
Step 1: Adding a unit test framework
MsTest test framework is the default unit test framework available with the previous versions of .Net framework and it continue to exist for .Net core as well. Installing the framework is as easy as adding the entry in project.json file
"MSTest.TestFramework": "1.0.0-preview"
Once a new dependency is added, to get it available for the project, run the restore command.
dotnet restore
Step 2: Writing the first test
Now, we can go ahead and add a new test file with the desired test method. The attributes like TestClass, TestMethod are available here as well. Most commonly used Assert statements are present as well. Below is a sample test written for the directory parser code used in the previous post.
using Microsoft.VisualStudio.TestTools.UnitTesting; namespace ConsoleApplication { [TestClass] public class TestClass { [TestMethod] public void shouldReturn12AsCount() { string dirPath = "/Users/z066157/SampleProjects"; DirectoryParser dp = new DirectoryParser(); var expectedCount = 12; var actualCount = dp.GetSubdirectoriesCount(dirPath); Assert.AreEqual(expectedCount,actualCount); } } }
Step 3: Adding the test runner
After writing the test, if you try to build and run the tests using the below commands, you might encounter an error as below:
bash-3.2$ dotnet build Project dotnetDemoApp (.NETCoreApp,Version=v1.0) will be compiled because inputs were modified Compiling dotnetDemoApp for .NETCoreApp,Version=v1.0 Compilation succeeded. 0 Warning(s) 0 Error(s) Time elapsed 00:00:01.0004377 bash-3.2$ dotnet test Project dotnetDemoApp (.NETCoreApp,Version=v1.0) was previously compiled. Skipping compilation No executable found matching command "dotnet-test-"
Note the last line in the console output which says No executable found matching command “dotnet-test-“. That means, there are no test runners which can execute these tests
To add the dotnet-test-mstest test runner, add the below line of code to the project.json
"dotnet-test-mstest": "1.0.1-preview"
Run the dotnet restore
command to restore the package.If you encounter the below error, modify the dependencies as below:
bash-3.2$ dotnet restore log : Restoring packages for /Users/z066157/SampleProjects/dotnetSamples/dotnetDemoApp/project.json... error: Package Microsoft.DiaSymReader 1.0.6 is not compatible with netcoreapp1.0 (.NETCoreApp,Version=v1.0). Package Microsoft.DiaSymReader 1.0.6 supports: error: - net20 (.NETFramework,Version=v2.0) error: - portable-net45+win8 (.NETPortable,Version=v0.0,Profile=Profile7) error: One or more packages are incompatible with .NETCoreApp,Version=v1.0. log : Lock file has not changed. Skipping lock file write. Path: /Users/z066157/SampleProjects/dotnetSamples/dotnetDemoApp/project.lock.json log : /Users/z066157/SampleProjects/dotnetSamples/dotnetDemoApp/project.json log : Restore failed in 701ms. Errors in /Users/z066157/SampleProjects/dotnetSamples/dotnetDemoApp/project.jsonPackage Microsoft.DiaSymReader 1.0.6 is not compatible with netcoreapp1.0 (.NETCoreApp,Version=v1.0). Package Microsoft.DiaSymReader 1.0.6 supports: - net20 (.NETFramework,Version=v2.0) - portable-net45+win8 (.NETPortable,Version=v0.0,Profile=Profile7) One or more packages are incompatible with .NETCoreApp,Version=v1.0.
The above error occurs due to the fact that one of the dependencies is not compatible with netcoreapp1.0. For this to work, you can modify the frameworks
section of project.json file to include portable-net45+win8
. The modified frameworks section should look like this
"frameworks": { "netcoreapp1.0": { "imports": [ "dnxcore50", "portable-net45+win8" ] } }
Running the dotnet restore
command again should successfully restore the dotnet-test-mstest package.
We are not completely ready yet to execute the tests, as we need to make dotnet cli aware of which test runner to use for executing the tests. That is done by adding the below entry to the project.json file
"testRunner": "mstest"
Step:4 Running the tests
Now that we are all set, we can run the tests by running the below command
bash-3.2$ dotnet test Project dotnetDemoApp (.NETCoreApp,Version=v1.0) will be compiled because inputswere modified Compiling dotnetDemoApp for .NETCoreApp,Version=v1.0 Compilation succeeded. 0 Warning(s) 0 Error(s) Time elapsed 00:00:01.0170496 Discovering Tests ... Executing Tests ... Passed shouldReturn12AsCount ============ Test Run Summary ============ Total tests: 1. Passed: 1. Failed: 0. Skipped: 0 Test Run Successful. SUMMARY: Total: 1 targets, Passed: 1, Failed: 0.