Continuous Integration - Running Unit Tests
Continuous integration is a good practice for software deployment automation. And as it is automatized you may add as many tasks on it as you wish. Here I am talking about a very specific and important task, which is unit testing.
In this article, I will be explaining how to run XUnit tests with continuous integration in your .NET Core projects using Azure Pipelines.
This article is the second part of a previous article about continuous integration and we are going to move on from where we stopped there.
Check the article and the project code
here.
What is XUnit?
XUnit is an open source automation unit testing tool for the .NET Framework and .NET Core.
You may find its official website
here.
Why use XUnit?
- Open Source;
- Community-focused
- A wide range of languages accepted: NET languages such as C#, VB.Net, and F#.
- Works well with ReSharper, CodeRush, TestDriven.Net, Xamarin.
Benefits of using unit tests in your build pipelines
- Validate if none business rules were broken.
- Catch development bugs or mistakes.
- Facilitates the integration.
- Keep the general quality of the project code.
- Reduces investigation time when any problem happens.
How to run XUnit unit tests in your continuous integrations pipelines?
You must have a project with your Azure Pipelines already set up. To find out how to check it here,
Some adjustments were made in the previous project in order to run unit tests, you may find the original project
here.
In order to be testable, I isolated the calculation method to a new class:
- public class Calculation
- {
- public double SumTwoNumbers(double numberOne, double numberTwo)
- {
- return numberOne + numberTwo;
- }
- }
Created a new project for the XUnit unit tests to be run after the build in the Azure Pipelines,
Added two Unit tests in the XUnitSampleTest project,
- public class CalculationSumTests
- {
- [Fact]
- public void CalculateSumCorrect()
- {
-
- var calculate = new Calculation();
- double numberOne = 5;
- double numberTwo = 4;
- double expectedResult = 9;
-
-
- var realResult = calculate.SumTwoNumbers(numberOne, numberTwo);
-
-
- Assert.Equal(expectedResult, realResult) ;
- }
- [Fact]
- public void CalculateSumFalse()
- {
-
-
- var calculate = new Calculation();
- double numberOne = 1;
- double numberTwo = 8;
- double expectedResult = 7;
-
-
- var realResult = calculate.SumTwoNumbers(numberOne, numberTwo);
-
-
- Assert.NotEqual(expectedResult, realResult);
- }
- }
I updated the yaml file, in order to execute the unit tests from the new XUnit project,
- # ASP.NET Core
- # Build and test ASP.NET Core projects targeting .NET Core.
- # Add steps that run tests, create a NuGet package, deploy, and more:
- # https://docs.microsoft.com/azure/devops/pipelines/languages/dotnet-core
- pool:
- vmImage: 'Ubuntu 16.04'
- variables:
- buildConfiguration: 'Release'
- steps:
- - script: dotnet build --configuration $(buildConfiguration)
- displayName: 'dotnet build $(buildConfiguration)'
- - task: DotNetCoreCLI@2
- inputs:
- command: test
- projects: '**/*Test/*.csproj'
- arguments: '--configuration $(buildConfiguration)'
Commit the changes and we are going to see the XUnit unit tests being executed by the Azure Pipelines,
After successfully running we may validate the results.
Here is the result of your unit test execution, where you have detailed information about your tests,
Congratulations! You have successfully run XUnit tests with continuous integration in your .NET Core projects using Azure Pipelines.
Next Step
Go for continuous delivery and creating a new release pipeline. This can save many productive hours deploying packages into a server.
External references
https://xunit.github.io/