DevOps - Part Two - Continuous Integration With XUnit Tests

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 from where we stopped there.

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:
  1. public class Calculation  
  2. {  
  3.     public double SumTwoNumbers(double numberOne, double numberTwo)  
  4.     {  
  5.         return numberOne + numberTwo;  
  6.     }  
  7. }  
Created a new project for the XUnit unit tests to be run after the build in the Azure Pipelines,
 
Running XUnit Tests in your build Pipelines

Running XUnit Tests in your build Pipelines
 
Added two Unit tests in the XUnitSampleTest project,
  1. public class CalculationSumTests  
  2.     {  
  3.         [Fact]  
  4.         public void CalculateSumCorrect()  
  5.         {  
  6.             //arrange  
  7.             var calculate = new Calculation();  
  8.             double numberOne = 5;  
  9.             double numberTwo = 4;  
  10.             double expectedResult = 9;  
  11.   
  12.             //act              
  13.             var realResult = calculate.SumTwoNumbers(numberOne, numberTwo);  
  14.   
  15.             //assert  
  16.             Assert.Equal(expectedResult, realResult) ;  
  17.         }  
  18.         [Fact]  
  19.         public void CalculateSumFalse()  
  20.         {  
  21.   
  22.             //arrange  
  23.             var calculate = new Calculation();  
  24.             double numberOne = 1;  
  25.             double numberTwo = 8;  
  26.             double expectedResult = 7;  
  27.   
  28.             //act  
  29.             var realResult = calculate.SumTwoNumbers(numberOne, numberTwo);  
  30.   
  31.             //assert  
  32.             Assert.NotEqual(expectedResult, realResult);  
  33.         }  
  34.     }  
I updated the yaml file, in order to execute the unit tests from the new XUnit project,
  1. # ASP.NET Core  
  2. # Build and test ASP.NET Core projects targeting .NET Core.  
  3. # Add steps that run tests, create a NuGet package, deploy, and more:  
  4. # https://docs.microsoft.com/azure/devops/pipelines/languages/dotnet-core  
  5. pool:  
  6. vmImage: 'Ubuntu 16.04'  
  7. variables:  
  8. buildConfiguration: 'Release'  
  9. steps:  
  10. - script: dotnet build --configuration $(buildConfiguration)  
  11. displayName: 'dotnet build $(buildConfiguration)'  
  12. - task: DotNetCoreCLI@2  
  13. inputs:  
  14. command: test  
  15. projects: '**/*Test/*.csproj'  
  16. arguments: '--configuration $(buildConfiguration)'  
Commit the changes and we are going to see the XUnit unit tests being executed by the Azure Pipelines,
 
Running XUnit Tests in your build 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,
Running XUnit Tests in your build Pipelines 
 
Running XUnit Tests in your build Pipelines

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/