Lunch: Creating your CI pipeline for net core app container and AzureDevOps Part 2/4 – Code Analysis and Unit Tests

In part 1, we started a new pipeline of build and added a net core application repository attached to it.

In this topic of the series, we will add code analysis and unit test tasks to improve the quality control of your code.

Your Code

First of all, you need to add some changes to your code allowing SonarQube to properly analyze your code. Compiling net core on Linux has some limitations and SonarQube analysis with full code coverage is one limitation.

Edit every csproj of your solution, add the following property into it:




The GUID must be different for each csproj. This property is needed by SonarQube to discover net core projects.

I´m using NUnit to execute tests in my solution, you can use MSTest or XUnit. On your test project, you need to add the following nuget package to it:


The Coverlet dependency will generate the Opencover coverage format during unit test that will be used later on SonarQube Analysis. The dotnet cli command dotnet test does not support natively code coverage on Linux, only Windows, so the Coverlet is used to build the coverage file no matter the platform the application is built. Your test project csproj file will be as follow:


Whitout these changes, the code analysis will not run on your application, or will result in a false positive analysis (All results 100% green).

Code Analysis and Unit Tests

To analyze the code we are going to use SonarQube, this topic assumes you already have a SonarQube server instance running. If you don´t have a SonarQube, please follow this link to guide you creating a new instance.

Add a new task by clicking the + button on agent job task, search for SonarQube task and add to pipeline the Prepare Analysis Configuration task. (If the task doesn´t appear, you need to install the SonarQube extension).



On the Prepare Analysis Configuration task, add the SonarQube server connection to your server.


Run the analysis using the MSBuild Integration, insert the project key and project name of your analysis. In this case net-core-svc-devops-sample. Click the Advanced link and in the Additional Properties field you need to add the following property:


The property above will use the opencover format as code coverage result file of your test. The Coverlet package will be responsible to generate this file.


Add a new task to pipeline, search for .NET Core task and add to pipeline. Select the task added and change the properties:

Command: test
Path to Project: **/*[Tt]ests/*.csproj
Arguments: /p:CollectCoverage=true /p:CoverletOutputFormat=opencover –settings $(System.DefaultWorkingDirectory)/CodeCoverage.runsettings

If you look the arguments property, you will see the output format as opencover generated by Coverlet package, also the argument used to add settings during test execution. The CodeCoverage.runsettings file is a test configuration file to inform test what to consider and what to exclude during tests. It´s useful to exclude 3rd party dependencies from test coverage. You can use the file from my repository or get from this link.


Add a new task to pipeline, search for SonarQube task and add the Run Code Analysis task to pipeline. This task will execute the analysis on your code after the build and test. The coverage file will be generated and saved in analysis results.


Add a new task to pipeline, search for SonarQube task and add the Publish Quality Gate Result task to pipeline. This task will publish your analysis to SonarQube Server and publish the quality gate result in your pipeline result.


Your pipeline has all tasks necessary for code quality and test control. Next post you will add the the build and publish docker image.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s