Friday, July 19, 2024

Running .NET Core Tests and collecting coverage with Azure Pipelines

Installing Coverlet

Coverlet is an open source project that produces code coverage reports.

In Visual Studio install the coverlet.collector and coverlet.msbuild NuGet package.

nuget install coverlet.collector

nuget install coverlet.msbuild

Running Coverlet Locally

dotnet test /p:CollectCoverage=true

Viewing Coverage in Visual Studio

This Visual Studio 2022 extension allows you to view coverage line by line and as a summary

https://marketplace.visualstudio.com/items?itemName=FortuneNgwenya.FineCodeCoverage

Defining the Azure Pipeline

Using the wizard follow the steps below.

Use the .NET Core SDK

Select the .NET Core SDK appropriate for your project.



Perform a .NET Restore

A restore will download the Nuget packages for your project.



Perform a .NET Build



Run the Tests

Ensure to define the arguments to collect coverage



Publish the Code Coverage

Task version 1 will be deprecated. See this post from Microsoft:

https://learn.microsoft.com/en-us/azure/devops/pipelines/tasks/reference/publish-code-coverage-results-v2?view=azure-pipelines




Tuesday, July 16, 2024

Running Jasmine Tests Using Karma and Azure Pipelines

In order to run Jasmine tests, the runner Karma must be used.  Karma requires Chrome.  So in Azure we will be installing all of that in a container in order to run the tests.  We will also be collecting coverage.  It is important that a full Linux distribution be used like Ubuntu.  We will be using the Azure pipeline wizard.

Install Jasmine

This will add Jasmine to your package.json

npm install --save-dev jasmine-core @types/jasmine @types/jasminewd2


Install Karma

This will add Karma to your package.json

npm install --save-dev karma karma-chrome-launcher karma-coverage karma-jasmine
karma-jasmine-html-reporter

Add a karma.conf.js to your project

Generating detailed code coverage reports can help you identify untested parts of your codebase. Add the coverageReporter to your karma.config.js

// Karma configuration file, see link for more information
// https://karma-runner.github.io/1.0/config/configuration-file.html

module.exports = function (config) {
  config.set({
    basePath: "",
    frameworks: ["jasmine", "@angular-devkit/build-angular"],
    plugins: [
      require("karma-jasmine"),
      require("karma-chrome-launcher"),
      require("karma-jasmine-html-reporter"),
      require("karma-coverage"),
      require("@angular-devkit/build-angular/plugins/karma"),
    ],
    client: {
      jasmine: {
        // you can add configuration options for Jasmine here
        // the possible options are listed at https://jasmine.github.io/api/edge/Configuration.html
        // for example, you can disable the random execution with `random: false`
        // or set a specific seed with `seed: 4321`
      },
      clearContext: false, // leave Jasmine Spec Runner output visible in browser
    },
    jasmineHtmlReporter: {
      suppressAll: true, // removes the duplicated traces
    },
    coverageReporter: {
      dir: require("path").join(__dirname, "./coverage/angularapp"),
      subdir: ".",
      reporters: [{ type: "html" }, { type: "text-summary" }],
    },
    reporters: ["progress", "kjhtml", "coverage"],
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: true,
    browsers: ["Chrome", "ChromeHeadless"],
    singleRun: false,
    restartOnFileChange: true,
  });
};


Add test and test-headless to your package.json

  "scripts": {
    "test": "ng test",
    "test-headless": "ng test --code-coverage --watch=false --browsers=ChromeHeadless",
  },

In Azure Define the Pipeline



Specify the Source Repository



Using Bash to Install Chrome




Here are the commands
sudo apt-get update
sudo apt-get install -y wget
wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
sudo apt-get install -y ./google-chrome-stable_current_amd64.deb
google-chrome --version

Use Node

Use the appropriate version of node for your project.





Use NPM to install the Angular CLI




Use NPM to install the node packages





Use NPM to run the build




Use NPM to run the tests