Snack: NSubstitute. A little friend to help you create fast unit tests in C#.

For most developers, coding tests is one of most challenging and boring tasks you do during your development, but trust me, it is a necessary evil and everybody will thank when you deliver a tested and reliable code.

One of the most time consuming task is to mockup dependencies your class depends on. Some classes can have so many dependencies, you will have to write all return conditions. Also you´ll need to instantiate all of them before you test your class.

I’m assuming you follow the DI (Dependency Injection) pattern principles in your code.

When you use dependencies mockups, you need to tell all return scenarios your class need to coverage all paths of your custom code. NSubstitute comes in help to easily create those mockup dependency classes.

The main method of NSubstitute library is Substitute.For<T>(), where T can be a class or interface you want to mock. After that, you can inject the returns values to your T mock class methods as you need.

NSubstitute is available as package in nuget.org package manager. See how to use it:

Let’s assume you have an ICalculator interface class. This interface has 4 Methods Add(), Subtract(), Multiply() and Divide() as follow:

In your custom class you declare the ICalculator interface in the class constructor method and create a method to do all calculations and return the sum of all values:

To test your CustomClass, create a unit test class (I’m using Nunit) and write the assertions to your class, in this case 2 valid scenarios and 1 invalid:

If you look to code above, the CustomClass class needs the ICalculator interface implementation to do all execution inside the DoCalculations() method. NSubstitute comes in this part, creating a concrete class for your interface. To use it, create a method in your test class, returning the ICalculator interface:

The Substitute.For<T>() method will create a fake concrete class of ICalculator interface and will expose all interface methods to be configured. You can add to each method call parameter condition, a return value accordingly the return value of interface method.

And finally, change the CustomClass constructor method in your test class to add the CreateMock() method.

Voilá, now test your class, and the ICalculator interface will return for all condition parameters, the return values you’ve configured in the mock method. If a condition doesn’t match a return value you’ve configured, a default value will return (null for reference types, default for value types).

NSubstitute can also be combined with all DI libraries, like Microsoft.Extensions.DependencyInjection, Simple Injector, and others.

For a complete NSubstitute documentation, click this link. With NSubstitute you’ll have a set of tools to easily create your dependencies mockup class (You can also reuse your mockup classes across projects). This will help you to gain productivity in your development.

Dinner: Creating your CI pipeline for net core app container and AzureDevOps Part 4/4 – Final steps and summary

In previous post, we´ve discussed about the docker build and publish tasks in the Azure DevOps pipeline.

In this post we will add the final tasks to pipeline and setup triggers to execute the pipeline automatically, enabling the continuous integration.

Tag your code

Your pipeline is almost ready, the last task to add is tag your source code. Go to Get Sources task and change the Tag Sources property to On success. You can also customize the tag format, but is a good idea to use the $(build.buildNumber) pipeline variable, because the tag must be a unique name.

dinner-creating-your-ci-pipeline-for-net-core-app-container-and-azuredevops-part-4-4-final-steps-and-summary-01

Your pipeline task are now ready, you need to add a trigger to start the pipeline automatically during continuous integration of your code. To do this, click on Triggers menu. Then enable the continuous integration trigger and select which branch you want to monitor (in this example branch master).

dinner-creating-your-ci-pipeline-for-net-core-app-container-and-azuredevops-part-4-4-final-steps-and-summary-02

To test your pipeline you can simply do a pull request from a branch to master and the pipeline will start automatically. The pipeline results will be as follow:

Pipeline logs: It shows all executed steps of your pipeline, if you click in each step, a detailed log will be shown.

dinner-creating-your-ci-pipeline-for-net-core-app-container-and-azuredevops-part-4-4-final-steps-and-summary-03

Tests results: The tests resuls page will show all tests executed in your test project, if a test fails, it will appear in a list and also the reason the test has failed.

dinner-creating-your-ci-pipeline-for-net-core-app-container-and-azuredevops-part-4-4-final-steps-and-summary-04

Summary page: The summary page is responsible for showing all linked information in your pipeline, since the commits made, until deployments (in this case the deployment does not exists).

dinner-creating-your-ci-pipeline-for-net-core-app-container-and-azuredevops-part-4-4-final-steps-and-summary-05

Also you can check the SonarQube Analysis result of analysis. You can also click on Detailed SonarQube report and you will be redirected to the SonarQube project page:

dinner-creating-your-ci-pipeline-for-net-core-app-container-and-azuredevops-part-4-4-final-steps-and-summary-06

In this page you have all information regarding bugs, vulnerabilities, code smells and the code coverage.

If look to your repository you will see a new tag in GitHub:

dinner-creating-your-ci-pipeline-for-net-core-app-container-and-azuredevops-part-4-4-final-steps-and-summary-07

Now everytime you commit a code and your pull request is approved to the master branch (or the branch you defined as trigger) will start the build pipeline, analyzing, testing and publishing your image wherever your need.

For any doubts just leave a comment.

Cheers!

Tea Time: Creating your CI pipeline for net core app container and AzureDevOps Part 3/4 – Building and Publishing Your Docker Image

In part 2, we added code analysis and tests tasks to the build pipeline, now it´s time to build the application and publish it to docker hub.

To build an Docker image, you need to create a Docker file and write the steps to mount your image. In the example, the Docker file is responsible for download the net core sdk image to build the image and download a net core runtime sdk to run the application within.

You can use your own Docker file or use the file from this repository as template.

Back to pipeline, add a new task, by clicking the + button on agent job. Search for Docker, select the Docker task and add to your pipeline. The default action will build the image and push to the container registry.

Once the task is placed, select a container registry connection, this is the place you will publish your image. The container registry can be a Docker Hub, Azure Container Registry or others. In this post we are using Docker Hub as container registry. For first time you need to add a connection to Docker Hub account Add a new connection and fill the properties of connection:

tea-time-creating-your-ci-pipeline-for-net-core-app-container-and-azuredevops-part-3-4-building-and-publishing-your-docker-image-01.png

On container repository property, write the name of image you want to create. The user account on Docker Hub must preeced the image name, otherwise, a access denied error will be thrown.

tea-time-creating-your-ci-pipeline-for-net-core-app-container-and-azuredevops-part-3-4-building-and-publishing-your-docker-image-02

When the pipeline runs, the Docker task will build the image based on the Dockerfile file and will publish the image to your Docker hub account. One important thing is the image tag, this property will set the image version of your Docker image. Change the default value $(Build.BuildId) to $(Build.BuildNumber) pipeline variable. This variable represents the build id number and is unique for each build.

Now your pipeline is ready to build your application and publish to the docker hub registry. In the next and final post we will add a final task, set the pipeline trigger and test it.

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:

<ProjectGuid>{E01A9E66-2425-4FF4-88A2-DA55BAECD2D1}</ProjectGuid>

Example:

lunch-creating-your-ci-pipeline-for-net-core-app-container-and-azuredevops-part-2-4-code-analysis-and-unit-tests-04

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:

Coverlet.Msbuild

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:

lunch-creating-your-ci-pipeline-for-net-core-app-container-and-azuredevops-part-2-4-code-analysis-and-unit-tests-05

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).

lunch-creating-your-ci-pipeline-for-net-core-app-container-and-azuredevops-part-2-4-code-analysis-and-unit-tests-01

lunch-creating-your-ci-pipeline-for-net-core-app-container-and-azuredevops-part-2-4-code-analysis-and-unit-tests-02

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

lunch-creating-your-ci-pipeline-for-net-core-app-container-and-azuredevops-part-2-4-code-analysis-and-unit-tests-03

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:

sonar.cs.opencover.reportsPaths=**/coverage.opencover.xml

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.

lunch-creating-your-ci-pipeline-for-net-core-app-container-and-azuredevops-part-2-4-code-analysis-and-unit-tests-06

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.

lunch-creating-your-ci-pipeline-for-net-core-app-container-and-azuredevops-part-2-4-code-analysis-and-unit-tests-06

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.

lunch-creating-your-ci-pipeline-for-net-core-app-container-and-azuredevops-part-2-4-code-analysis-and-unit-tests-08

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.

lunch-creating-your-ci-pipeline-for-net-core-app-container-and-azuredevops-part-2-4-code-analysis-and-unit-tests-09

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

Breakfast: Creating your CI pipeline for net core app container and AzureDevOps Part 1/4 – Code and Build Pipeline

azuredevops-2x

In this series, I will guide you to create a complete CI (build) pipeline in Azure DevOps, covering all necessary aspects to build a full net core application (an API service), and deploying to docker hub.

The aspects covered in this series are:

  • Get source code from GitHub
  • Code Analysis
  • Unit Tests
  • Unit Tests coverage
  • Build DockerFile
  • Push DockerFile to DockerHub
  • Publish artifacts for CD pipeline

Looking at the steps above, it looks simple to achieve, but while I was creating the pipeline, I´ve face some challenges to resolve to fully attend all aspects above.

This series are divided in 4 topics:

  1. The code and Build Pipeline
  2. The code analysis and unit tests steps
  3. Building and publishing your Docker image
  4. Final steps and summary

Part 1 – Code and Build Pipeline

Code

In this part I will show how to create a simple repository and a simple web api application in net core using swagger

I use Github as repository for this demo, but you can use AzureDevops Repo or even Bit Bucket as well.

Get the code repo here, you can use it in the following steps:

https://github.com/aledaccas/net-core-svc-devops-sample

The code consist in two projects, a web api application and a test project. The web api contains basic functionalities using the concepts of DI (Dependency Injection). The test project mocks the dependency and test it to produce the % of code coverage of your application.

Build Pipeline

To create your build pipeline, access your Azure Devops subscription (https://dev.azure.com/), create a project (in my case net-core-svc-devops-sample) and access the build pipelines list (https://dev.azure.com//net-core-svc-devops-sample/_build). Create a new pipeline by clicking the New pipeline button.

breakfast-creating-your-ci-pipeline-for-net-core-app-container-and-azuredevops-part-1-4-code-and-build-pipeline-01

A new page will ask to select your code repository, you can use the Azure Repos Git, GitHub, Subversion, Bitbucket or another git version control. I will use the example GitHub repository (https://github.com/aledaccas/net-core-svc-devops-sample). Click Continue

breakfast-creating-your-ci-pipeline-for-net-core-app-container-and-azuredevops-part-1-4-code-and-build-pipeline-02

In template page, select empty template and apply. The result will be a empty pipeline with one agent job. Change the pipeline agent pool to Hosted Ubuntu 1604. This will be necessary to build the docker image in linux version.breakfast-creating-your-ci-pipeline-for-net-core-app-container-and-azuredevops-part-1-4-code-and-build-pipeline-03

Press Ctrl+s to save your pipeline. Now you are ready to add the tasks to test, analyze and build your net core container image. Check next post for unit test and quality control.