← all posts

Setting up .net core continuous integration build with VSTS/TFS

Gordon Beeming
Gordon Beeming
On this page3 sections ▾

You might be wondering after recently posting Setting up a standard continuous integration build with VSTS/TFS why I'd need to post specifically on .net core. After all it's still a Visual Studio solution so things should just work, the keyword here is should 🙂. Things didn't just work when I tried to get this working so decided to share my experience here (as well as document it for myself later 😜).

#Sample project

So we going to need a sample project for this, I'm going to create a standard .net core web project

Visual Studio New Project dialog: Selecting ASP.NET Core Web Application template.
Creating a new ASP.NET Core Web Application in Visual Studio.

using asp.net core 1.1 as a Web Application

Visual Studio: Selecting ASP.NET Core 1.1 Web Application template.
Choosing the ASP.NET Core 1.1 Web Application template.

We also going to add another project using the Unit Test Project (.NET Core) template

Visual Studio New Project dialog: Selecting Unit Test Project (.NET Core) template.
Adding a Unit Test Project (.NET Core) to the solution.

We now need to add the MVC nuget reference to this test project. Open manage Nuget reference for solution

  1. In the Installed tab
  2. Click on Microsoft.AspNetCore.Mvc
  3. Check the test project
  4. Click Install
Visual Studio Manage NuGet Packages: Installing Microsoft.AspNetCore.Mvc for the test project.
Installing Microsoft.AspNetCore.Mvc NuGet package for the unit test project.

Next we'll

  1. Add a reference to our web project
  2. Rename the test to "HomeControllerTests.cs"
  3. Rename the test class to "HomeControllerTests"
  4. Add a About Test Method which you can find in my GitHub Gists
Visual Studio: HomeControllerTests.cs with About test method and reference to web project.
Setting up HomeControllerTests with a reference to the web project and an About test method.

That should be all we need to demonstrate CI being harder for .net core 🙂. Check this code into TFS and we all set to get started

Visual Studio Team Explorer: Checking in code changes to TFS.
Checking the sample project code into TFS.

Let's start off by creating a standard build how explained in the last post I did Setting up a standard continuous integration build with VSTS/TFS, I only did the first section with non of the extra fluff 🙂.

#Changes to build for .net core

The first thing you will notice if you just try run the build it will break

#Restoring .net core packages

So our first set of errors is that the build can't find a bunch of references even though the restore step ran through.

VSTS/TFS build log: Errors indicating missing references after package restore.
Build errors due to missing .NET Core package references.

This is because we need to do our restore a different way using the .net core restore. Let's fix this by adding the .NET Core (PREVIEW) task to our build

VSTS/TFS build definition: Adding the .NET Core (PREVIEW) task.
Adding the .NET Core task to the build definition for package restore.

Next we need to

  1. Place this task first in the list of tasks
  2. Change the Command to restore
  3. Set the Project(s) to "\\\\*.sln"
VSTS/TFS build definition: .NET Core task configured for restore command on the solution file.
Configuring the .NET Core task to restore packages for the solution.

Click Save. Run your build. This time our build runs through but is partially succeeded.

VSTS/TFS build summary: Build partially succeeded.
Build summary indicating a partially successful build after adding .NET Core restore.

We going to remove the Publish symbols path step from our build because we aren't going to need it

VSTS/TFS build definition: Removing the 'Publish symbols path' task.
Removing the unnecessary 'Publish symbols path' task from the build definition.

Click Save after removing this and run your build so we can confirm everything is green before continuing on.

#Running .net core tests

If you looked at your build after it ran you would have noticed that there is no test results

VSTS/TFS build summary: No test results reported.
Build summary showing no test results before adding .NET Core test task.

We know that we have a test and that we are running tests in the build so what's going on here?

Well you probably guessed it but we need to add the .NET Core (PREVIEW) step again this time

  1. Place the step after the Build solution step
  2. Change the Command to test
  3. Set the Project(s) to "\\\\*.Tests.csproj"
  4. Set the Arguments to "--no-build --logger:trx --configuration $(BuildConfiguration)"
VSTS/TFS build definition: .NET Core task configured to run tests for .Tests.csproj projects.
Configuring the .NET Core task to run unit tests.

What we are doing now is using .net core's test capabilities, telling the running we want to log the results using the trx format and that we want to run the BuildConfiguration that the build is running under.

Click Save. Run the build. You will notice that there is still no test results showing up and this is because we need to tell the build system to publish these results. Add the Publish Test Results task

VSTS/TFS build definition: Adding the 'Publish Test Results' task.
Adding the 'Publish Test Results' task to the build definition.

Next we'll

  1. Make sure we place that task after the dotnet test task
  2. Set the Test Result Format to VSTest
  3. Set the Test Results Files to "\\/TestResults/\*.trx"
  4. Check the Merge Test Results checkbox
  5. Open the Advanced section and set the Configuration to "$(BuildConfiguration)"
  6. Check the Always run (we want to publish results if test fail 😜, thank you Mikael Krief)
VSTS/TFS build definition: 'Publish Test Results' task configured for VSTest format and .trx files.
Configuring the 'Publish Test Results' task.

Click Save. Run the build. This time when the build has run if you go to the Summary you will notice 2 weird things

  1. We have 2 tests found (but only building 1 configuration)
  2. The build says there is no tests found
VSTS/TFS build summary: Discrepancy in test reporting - 2 tests found but also 'no tests found' message.
Build summary showing conflicting test result information.

The 2nd issue is easy to fix, if you don't expect to have regular .net tests in the solution remove the standard test step to get rid of this warning

VSTS/TFS build definition: Removing the standard 'Visual Studio Test' task.
Removing the standard test step to resolve conflicting test reports.

The 1st issue is because we are running the tests but not cleaning up the test results after the builds test results after they have generated or before we generate them in the build. The easiest way to fix this is to Clean all build directories before starting your build as I mentioned in the standard CI process blog mentioned multiple times in this post

VSTS/TFS build definition: Repository tab, Clean option set to 'All build directories'.
Setting the build to clean all build directories to ensure accurate test counts.

If you don't want to do this you can look at adding in a delete task before your run tests step to remove all trx files.

If we make either change, save and run the build. We'll notice that we have 1 test result. Note you may not have seen this experience if your build was running on a different agent each time but when it did run on the same agent again you would have seen this behavior

VSTS/TFS build summary: 1 test result reported correctly.
Build summary showing the correct count of 1 test result after cleaning build directories.

Now our CI process is behaving like normal 🙂

#Conclusion

Yes there is extra steps you need to do for .net core but in the end the process is still considered easy, especially if you have developed with .net core and know the dotnet commands.

Gordon Beeming
Gordon Beeming

Father • Husband • Triathlete • SSW Solution Architect

Related posts