【问题标题】:code coverage in gitlab ci/cd for .net applications?.net 应用程序的 gitlab ci/cd 中的代码覆盖率?
【发布时间】:2020-09-19 01:13:34
【问题描述】:

我最近开始与C# 合作,我正在研究我们拥有的一个遗留系统。我试图弄清楚这个遗留系统的代码覆盖率是多少。这是我的Sample.UnitTests.csproj 文件:

<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>

我做了一些研究,发现我们可以使用coverlet,它可以生成cobertura风格的报告。我完全按照我的 mac 盒子上提到的here 进行操作,一切正常,我可以看到在我的控制台上正确生成了报告,它还生成了index.html 文件,我们也可以使用该文件进行可视化。

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;

现在,由于我们在项目中使用了gitlab ci/cd 管道 - 有什么方法可以让我的.gitlab-ci.yml 文件的这一部分成为我的.gitlab-ci.yml 文件的一部分,这样它就可以在构建发生时自动为我生成报告并且我团队中的每个人都可以看到它成功地。因为到目前为止它都是手动的,因为我需要在我的本地 Mac 机器上运行上述命令,我只能通过单击 index.html 文件从我的控制台中看到它。

这些是我的.gitlab-ci.yml 文件的阶段,如下所示。如果需要,我也可以提供我的yml 文件,但是任何可以演示如何执行此操作的简单示例都会有很大帮助。我尝试了很多搜索,但根本找不到关于如何通过使用coverletcobertura .net 应用程序样式报告的 gitlab 管道来做到这一点。..

stages:
  - test
  - publish
  - increment
  - deploy
  - integrationTests
  - release

如果需要,这也可以通过 webhook 完成吗?

【问题讨论】:

  • 您可以查看现有的 gitlab 问题以添加封面支持 gitlab.com/gitlab-org/gitlab-foss/-/issues/62727gitlab.com/gitlab-org/gitlab/-/issues/37379
  • 哦,所以它还不支持你的意思是说?这就是我通过阅读该链接所理解的。如果我在这里错了,请纠正我?
  • 您的链接说明了如何收集覆盖率并创建 html 报告,与 CI/CD 集成无关
  • 嗯,我猜这也是我提到的。它没有,我自己也进行了研究,我没有看到任何地方提到如何通过 gitlab 管道完成它并集成coverlet 以实现.net 代码覆盖。
  • @dragons 你解决了这个问题吗?你能分享一下你做了什么吗?

标签: c# asp.net-core code-coverage gitlab-ci coverlet


【解决方案1】:

可能需要使用实际的 GUID 而不是 *

stages:
  - test
  - publish
  - increment
  - deploy
  - integrationTests
  - release

build-and-test:
  stage: test
  image: mcr.microsoft.com/dotnet/sdk:6.0
  script:
  - dotnet add package coverlet.msbuild
  - dotnet restore
  - dotnet build
  - 'dotnet test --collect:"XPlat Code Coverage"'
  artifacts:
    reports:
      cobertura: TestResults/*/coverage.cobertura.xml

GitLab 可以与之前的 Cobertura 报告进行比较。

如果您想要 HTML,只需将其包含在工件中即可。也可以将其发布到 GitLab Pages。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-21
    • 2022-10-13
    • 2013-02-08
    相关资源
    最近更新 更多