Automated Testing - Running xUnit Test to DevOps Pipeline

Definitions

Automated Testing: using an automated testing tool to execute your test case suite.

xUnit: is an open source testing platform that focuses on increase and flexibility with a Community-focused development structure. It also accepts a wide range of languages such as C#, VB.Net, and F#.

Overview

Being able to do automated testing is important because:

  • Doing manual testing with all workflows can be time and cost consuming.
  • An automated test can be run unattended and does not need human intervention.
  • It can be hard to test for very fluent sites manually.
  • Doing automated testing can increase the speed of test execution and test coverage.

The benefits of using unit tests in your build pipelines are:

  • It can validate if none business rules were broken.

  • It will bale able to catch bugs or mistakes.

  • it can manage the integrations.

  • Manages to keep the quality of the project code.

  • Reduces investigation time when any problem happens.

Running xUnit into DevOps Pipeline

** The following research was based on this website: https://www.c-sharpcorner.com/article/running-nunit-tests/ , I was able to follow the steps of that website and be able to run a successful unit test. 

** You can clone my project with their demo if you get lost, just create a new repo and add this to your pipeline. https://jervinfrancisco@dev.azure.com/jervinfrancisco/AzurePipeline/_git/AzurePipeline

To do this you must have a project with your Azure Pipelines that is already set up. Then create a new project for the xUnit tests to be run after the build in the Azure Pipelines in visual studio.

Running XUnit Tests in your build Pipelines


Within the xUnit Project that you have created, this is where you can add testing based on your application inside the UnitTest.cs file that was created. 

Once that is created you will need to go to your .yaml in your pipeline build and updated it with the following in order to execute the unit tests from the new XUnit project:

yaml
# 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)'  

Then you can commit the changes and then you would be able to see the XUnit unit tests being executed by the Azure Pipelines:

|

Running XUnit Tests in your build Pipelines

Running XUnit Tests in your build Pipelines

You should be seeing your test results in the summary of logs just like the above example. 

Contact me at any time if you have any question or help: jervin.francisco@tc.gc.ca

References