Skip to content

lukasz-pekala/CodeCoverageWorkshop

Repository files navigation

CodeCoverageWorkshop

A project created to test different possibilities of code coverage generation engines and to present code coverage in various IDE (VS, VS Code, etc.)

Build Quality Gate Status

Essential part of SonarCloud (SonarQube) configuration

.\.sonar\scanner\dotnet-sonarscanner begin `
  /k:"lukasz-pekala_CodeCoverageWorkshop" `
  /o:"lukasz-pekala" `
  /d:sonar.login="${{ secrets.SONAR_TOKEN }}" `
  /d:sonar.host.url="https://sonarcloud.io" `
  /d:sonar.cs.nunit.reportsPaths="**/TestResults/*/*.trx" `
  /d:sonar.cs.opencover.reportsPaths="CodeCoverageWorkshop.Logic.xUnit.Test/CoverageData/coverage.opencover.xml,CodeCoverageWorkshop.Logic.NUnit.Test/CoverageData/coverage.opencover.xml" `   /d:sonar.coverage.exclusions="**/*.conf.js,**/*.spec.ts,**/*.html,**/wwwroot/**/*,**/obj/**,**/bin/**,**/.git/**,**/package.json,**/angular.json,**/appsettings*json,**/node_modules/**,**/Upgrades/Migrations/**"
  
dotnet restore
dotnet build --no-restore
dotnet test --no-build --verbosity normal --logger trx /p:CollectCoverage=true /p:CoverletOutput=.\CoverageData\ /p:CoverletOutputFormat=opencover

.\.sonar\scanner\dotnet-sonarscanner end /d:sonar.login="${{ secrets.SONAR_TOKEN }}" 

dotnet test and coverlet

dotnet test example

dotnet restore
dotnet build --no-restore
dotnet test --no-build --verbosity normal --logger trx /p:CollectCoverage=true /p:CoverletOutput=.\CoverageData\ /p:CoverletOutputFormat=opencover

coverlet examples

dotnet test /p:CollectCoverage=true
dotnet test /p:CollectCoverage=true /p:CoverletOutput=.\CoverageData\ /p:CoverletOutputFormat=opencover
dotnet test /p:CollectCoverage=true /p:Threshold=\"80,100,70\" /p:ThresholdType=\"line,branch,method\"

coverlet - więcej informacji

https://github.com/coverlet-coverage/coverlet/blob/master/Documentation/MSBuildIntegration.md

Code Coverage Exclusions

Exclusion of Entity Framework migrations

dotnet test /p:CollectCoverage=true /p:CoverletOutput=.\CoverageData\ /p:CoverletOutputFormat=opencover /p:Exclude="[*]CodeCoverageWorkshop.DAL.Migrations.*"

[assembly: ExcludeFromCodeCoverage]

[assembly: ExcludeFromCodeCoverage]

.NET 5:

[AttributeUsageAttribute(AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Constructor | AttributeTargets.Method | AttributeTargets.Property | AttributeTargets.Event, Inherited = false, AllowMultiple = false)]

https://docs.microsoft.com/pl-pl/dotnet/api/system.diagnostics.codeanalysis.excludefromcodecoverageattribute?view=net-5.0

.NET 4.X

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Constructor | AttributeTargets.Event | AttributeTargets.Method | AttributeTargets.Property | AttributeTargets.Struct, AllowMultiple = false, Inherited = false)]

https://docs.microsoft.com/pl-pl/dotnet/api/system.diagnostics.codeanalysis.excludefromcodecoverageattribute?view=netframework-4.8

More

https://www.axian.com/2021/04/30/simple-powershell-script-for-producing-code-coverage-reports-in-net-core/

Visual Studio Code configuration

.NET Core Test Explorer

Let's add:

/p:CollectCoverage=true /p:CoverletOutputFormat=lcov /p:CoverletOutput=./lcov.info

to:

image

ReportGenerator

ReportGenerator - instalacja

Dla .NET Core/.NET 5 zainstalujmy ReportGenerator globalnie.

dotnet tool install -g dotnet-reportgenerator-globaltool

lub lokalnie:

dotnet tool install dotnet-reportgenerator-globaltool --tool-path tools

dotnet new tool-manifest
dotnet tool install dotnet-reportgenerator-globaltool

tworząc plik .config\dotnet-tools.json

{
  "version": 1,
  "isRoot": true,
  "tools": {
    "dotnet-reportgenerator-globaltool": {
      "version": "4.8.12",
      "commands": [
        "reportgenerator"
      ]
    }
  }
}

ReportGenerator - wygenerowanie raportu

Niestety ReportGenerator nie potrafi przetworzyć danych wygenerowanych przez Coverlet w domyślnym formacie (pliki coverage.json) Zmieńmy więc sposób generowania danych o Coverleta tak by otrzymać wyniki w formacie OpenCover

dotnet test /p:CollectCoverage=true /p:CoverletOutput=.\CoverageData\ /p:CoverletOutputFormat=opencover

Wygenerujmy raport w oparciu o dostępne dane

reportgenerator -reports:".\CodeCoverageWorkshop.Logic.xUnit.Test\CoverageData\coverage.opencover.xml;.\CodeCoverageWorkshop.Logic.NUnit.Test\CoverageData\coverage.opencover.xml" -targetdir:.\CoverageReports -reporttypes:"Html;HtmlSummary;MarkdownSummary;TeamCitySummary"

dotCover

dotCover - How to configure

https://www.jetbrains.com/help/dotcover/Running_Coverage_Analysis_from_the_Command_LIne.html

dotCover example

dotnet tool install JetBrains.dotCover.GlobalTool -g
dotnet dotcover test --no-build --dcReportType=Html

Front-end Code Coverage

Angular

Poniższe polecenie generuje plik lcov.info w podkatalogu coverage<nazwa aplikacji angular>\

ng test --browsers=ChromeHeadless --code-coverage --watch false

Raport wygenerowany przez Istanbul może zostać skonsumowany przez Gitlaba

Cypress & React

https://docs.cypress.io/guides/tooling/code-coverage#Introduction

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages