A project created to test different possibilities of code coverage generation engines and to present code coverage in various IDE (VS, VS Code, etc.)
.\.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 restore
dotnet build --no-restore
dotnet test --no-build --verbosity normal --logger trx /p:CollectCoverage=true /p:CoverletOutput=.\CoverageData\ /p:CoverletOutputFormat=opencover
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\"
https://github.com/coverlet-coverage/coverlet/blob/master/Documentation/MSBuildIntegration.md
dotnet test /p:CollectCoverage=true /p:CoverletOutput=.\CoverageData\ /p:CoverletOutputFormat=opencover /p:Exclude="[*]CodeCoverageWorkshop.DAL.Migrations.*"
[assembly: ExcludeFromCodeCoverage]
.NET 5:
[AttributeUsageAttribute(AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Constructor | AttributeTargets.Method | AttributeTargets.Property | AttributeTargets.Event, Inherited = false, AllowMultiple = false)]
.NET 4.X
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Constructor | AttributeTargets.Event | AttributeTargets.Method | AttributeTargets.Property | AttributeTargets.Struct, AllowMultiple = false, Inherited = false)]
Let's add:
/p:CollectCoverage=true /p:CoverletOutputFormat=lcov /p:CoverletOutput=./lcov.info
to:
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"
]
}
}
}
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"
https://www.jetbrains.com/help/dotcover/Running_Coverage_Analysis_from_the_Command_LIne.html
dotnet tool install JetBrains.dotCover.GlobalTool -g
dotnet dotcover test --no-build --dcReportType=Html
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
https://docs.cypress.io/guides/tooling/code-coverage#Introduction