I recently started working with C#
and I am working on one of the legacy system we have. I am trying to figure out what is the code coverage for this legacy system. Here is my Sample.UnitTests.csproj
file:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="AutoFixture.AutoMoq" Version="4.2.1" />
<PackageReference Include="AutoFixture.NUnit3" Version="4.2.1" />
<PackageReference Include="coverlet.msbuild" Version="2.9.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Moq" Version="4.8.2" />
<PackageReference Include="nunit" Version="3.9.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.9.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0" />
<PackageReference Include="WireMock.Net" Version="1.0.4.17" />
<PackageReference Include="Utf8Json" Version="1.3.7" />
<FrameworkReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="System.Buffers" Version="4.5.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="../Sample/Sample.csproj" />
</ItemGroup>
<ItemGroup>
<DotNetCliToolReference Include="dotnet-reportgenerator-cli" Version="4.2.10" />
</ItemGroup>
</Project>
I did some research and found out we can use coverlet
which can generate cobertura
style report. I followed exactly as mentioned here on my mac box and everything works fine and I can see the report being generated correctly on my console and also it generates index.html
file which we can use to visualize as well.
dotnet add package coverlet.msbuild
dotnet restore
dotnet build
dotnet test /p:CollectCoverage=true
dotnet test /p:CollectCoverage=true /p:CoverletOutputFormat=cobertura /p:Exclude="[xunit*]\*" /p:CoverletOutput="./TestResults/"
dotnet reportgenerator "-reports:TestResults/coverage.cobertura.xml" "-targetdir:TestResults/html" -reporttypes:HTML;
Now since we use gitlab ci/cd
pipeline for our project - Is there any way I can make this part of my .gitlab-ci.yml
file so that it can generate report automatically for me whenever build happens and everybody in my team can see it successfully. Since as of now it's all manual as I need to run those above commands on my local mac box and I can see it from my console only by clicking index.html
file.
These are my stages of .gitlab-ci.yml
file as shown below. If needed I can provide my yml
file as well but any simple example where it can demonstrate how can I do this then it will be of great help. I tried searching a lot and couldn't find this at all on how can I do it through gitlab pipeline which uses coverlet
and cobertura
style report for .net applications..
stages:
- test
- publish
- increment
- deploy
- integrationTests
- release
Can this be done through webhook as well if needed?
-reporttypes:HTML
to-reporttypes:Cobertura
. Then, setartifacts:reports:cobertura
to the path of that output xml. – Indecorum