Today I Learned How to Run NUnit Tests for a .NET Core Project in TeamCity

In TeamCity you can’t use the usual NUnit Runner to run .NET Core unit tests.  At least I couldn’t get it to work.  I’m sure this will be fixed in the future but for now the below works for me.  I got lots of inspiration from this TeamCity blog post.

For this example I’ll use the NConstraints project.  It contains a .NET Standard project (SaturdayMP.Constraints) that we want to test and two .NET Core Test projects (SaturdayMP.Constratints.Test and TestClient).

NConstraint Solution Explorer

In TeamCity I created a project with the usual first two steps of NuGet Install and Compile.

TeamCity Project Before Adding Test Step

If you read the TeamCity blog post, the one I got my inspiration from, then you probably noticed it used the .NET Core Runner to compile the project.  You might have had to do this when the TeamCity blog post was first written but now using the Visual Studio Runner will compile .NET Core projects.

Before we can add the .NET Core unit test build step we need to install the TeamCity .NET CLI Plugin.  Do this by downloading the plugin zip file from here.

TeamCity .Net Plugin Download

Then install the plugin by clicking on Administration in the top right then Plugins List on the bottom left.

TeamCity Navigate To Plugins List

Then click Upload Plugin Zip link and choose the plugin zip file you just downloaded.

TeamCity Upload Plugin Zip

 

TeamCity Upload .Net Core Plugin

Once the upload is complete you should see something similar to the screen shot below.

TeamCity Upload Successful

The plugin is uploaded but before it’s active you need to reboot TeamCity.  In my case I just rebooted the server.

Once the reboot is complete you can check that the plugin installed correctly by going to an agent with Visual Studio 2017 installed.

TeamCity Navigate To Agent Parameters

Once on the agent navigate to the parameters page and you should see something similar to the below screen shot.

TeamCity Check .Net CLI Plugin Installed

If you don’t see the DotNetCli listed then you either forgot to reboot TeamCity, the plugin didn’t install correctly, or you don’t have .NET Core installed on you build agent.

Now that the plugin is installed we can go back to our project and add the unit test build step.  Add a new build step and in the runner type pick .NET CLI (dotnet).  Then in the command drop down pick test and finally enter the paths to your projects.  If you have several projects you can use wildcards.

TeamCity .Net CLI Build Step

If you don’t see all the options in the screen shot above them make sure to click Show Advanced Options.  In my case I also enabled code coverage at the bottom of the list.

TeamCity .Net CLI Build Step Enable Code Coverage

 

Now when we build the tests should get executed.  You can see the results of the tests in the build log or the tests tab of the build results.

TeamCity Test Results

As I said at the beginning I’m sure the NUnit Runner will be updated to handle .NET Core but use the above for now.

P.S. – R.I.P. Tom Petty.

This entry was posted in Code Examples, Software Development, Today I Learned and tagged , , , . Bookmark the permalink.