Github Actions

我们的开源项目 Host 在 Github,并且使用它强大的 Actions 功能在做 CICD。单看 Github Actions 可能不知道是啥。其实它就是我们常说的 CICD pipeline 或者叫 workflow。当我们 Push 代码到 Github,它会自动触发这些管道。它会帮我们自动 build 代码,跑 test cases,构建镜像,发布镜像,等等。这一切还都是免费的。

今天我把 AgileConfig 的测试在 Github Actions 上跑通了。原来集成测试在 Actions 上跑一直有点问题,今天终于修好了。既然 test cases 都可以跑通了,那么能不能在 Actions 直接看到 code coverage 呢?答案当然是肯定的。

运行测试并收集结果

在我们每次运行单元测试的时候,微软的工具其实已经可以为我们生成结果描述文件了。请使用以下代码运行测试:

dotnet test --no-build --verbosity normal --collect:"XPlat Code Coverage" --results-directory ./coverage

运行完成后我们可以看到会生成多个 xml 文件,这些文件其实就是用来描述测试内容与结果的文件.

测试运行成功。
测试总数: 123
通过数: 123
总时间: 2.6757 分钟
1>已完成生成项目“D:\00WORKSPACE\AgileConfig\AgileConfig.sln”(VSTest 个目标)的操作。 已成功生成。
0 个警告
0 个错误 已用时间 00:02:43.08 附件:
D:\00WORKSPACE\AgileConfig\coverage\1a7564b1-aa53-44ad-b34b-9abbb4b97c9f\coverage.cobertura.xml
D:\00WORKSPACE\AgileConfig\coverage\45f571fb-2ec4-445b-b444-6879f784c925\coverage.cobertura.xml
D:\00WORKSPACE\AgileConfig\coverage\ff1d49a5-9663-42f9-813b-2ef81fe5ed3f\coverage.cobertura.xml
D:\00WORKSPACE\AgileConfig\coverage\6b502f8f-a99f-4931-8643-faf79db7273c\coverage.cobertura.xml
D:\00WORKSPACE\AgileConfig\coverage\ff389298-7b68-4e60-a35c-d9c0db8cd640\coverage.cobertura.xml

在本地分析测试结果

有了这几个 xml 文件我们就可以进行分析了。当然用肉眼来分析是看不出来啥的。我们先在本地用插件来分析一波。

在 VS 里安装插件:

Fine Code Coverage

安装完后插件后,再次运行一下所有的测试用例。在 VS 的输出窗口我们选择 FCC

这个窗口内会打印出每个 project 的测试结果。

打开 “视图”>“其他窗口”>“Fine code coverage” ,我们能查看更详细的结果。



使用 CodeCoverageSummary

既然在本地能分析这些文件,那么我们显然只要能在 Github Actions 里面插入一步分析的步骤就可以了。显然已经有大佬实现了这个 Action,我们只要集成到自己的 workflow 文件里就行了。

CodeCoverageSummary:

A GitHub Action that reads Cobertura format code coverage files from your test suite and outputs a text or markdown summary. This summary can be posted as a Pull Request comment or included in Release Notes by other actions to give you an immediate insight into the health of your code without using a third-party site.

CodeCoverageSummary

修改我们的 CI Workflow 文件:

  - name: Build
run: dotnet build --no-restore
- name: Test
run: dotnet test --no-build --verbosity normal --collect:"XPlat Code Coverage" --results-directory ./coverage
- name: Code Coverage Report
uses: irongut/CodeCoverageSummary@v1.3.0
with:
filename: coverage/**/coverage.cobertura.xml
badge: true
fail_below_min: true
format: markdown
hide_branch_rate: false
hide_complexity: true
indicators: true
output: both
thresholds: '60 80'

使用起来非常简单。并且有非常多的配置项,这里不一一介绍了。有需要的直接看它的文档吧。

下面让我们跑一下这个 workflow 试试看。

Coverage File: /github/workspace/coverage/d497ebaf-5670-41ea-8215-dfb4f3a037d6/coverage.cobertura.xml
Coverage File: /github/workspace/coverage/83632b7c-8df2-4179-b0a8-e6410a71b1b7/coverage.cobertura.xml
Coverage File: /github/workspace/coverage/b434d3bb-7909-4a7d-b0f2-f8620d429cfc/coverage.cobertura.xml
Coverage File: /github/workspace/coverage/af81da37-b5c5-4edd-a409-74984355857e/coverage.cobertura.xml
Coverage File: /github/workspace/coverage/1b159b39-3eb4-4951-936e-afde68925b34/coverage.cobertura.xml ![Code Coverage](https://img.shields.io/badge/Code%20Coverage-18%25-critical?style=flat) Package | Line Rate | Branch Rate | Health
-------- | --------- | ----------- | ------
AgileConfig.Server.IService | 41% | 67% |
Agile.Config.Protocol | 0% | 100% |
AgileConfig.Server.Data.Repository.Freesql | 95% | 93% |
AgileConfig.Server.Data.Freesql | 72% | 59% |
AgileConfig.Server.Service | 28% | 21% |
AgileConfig.Server.EventHandler | 0% | 0% |
AgileConfig.Server.Data.Mongodb | 73% | 54% |
AgileConfig.Server.Data.Abstraction | 77% | 50% |
AgileConfig.Server.Event | 0% | 100% |
AgileConfig.Server.Data.Repository.Mongodb | 67% | 88% |
AgileConfig.Server.Data.Entity | 97% | 100% |
AgileConfig.Server.Common | 1% | 0% |
AgileConfig.Server.Data.Repository.Selector | 95% | 67% |
AgileConfig.Server.Data.Freesql | 45% | 31% |
AgileConfig.Server.Data.Abstraction | 80% | 75% |
AgileConfig.Server.Data.Entity | 89% | 100% |
AgileConfig.Server.Common | 1% | 0% |
AgileConfig.Server.Data.Abstraction | 77% | 67% |
AgileConfig.Server.Data.Entity | 0% | 100% |
AgileConfig.Server.Common | 1% | 0% |
AgileConfig.Server.Common | 21% | 25% |
AgileConfig.Server.IService | 27% | 17% |
Agile.Config.Protocol | 0% | 100% |
AgileConfig.Server.Data.Repository.Freesql | 0% | 0% |
AgileConfig.Server.Data.Freesql | 0% | 0% |
AgileConfig.Server.Service | 2% | 2% |
AgileConfig.Server.EventHandler | 0% | 0% |
AgileConfig.Server.Data.Mongodb | 0% | 0% |
AgileConfig.Server.Data.Abstraction | 0% | 0% |
AgileConfig.Server.Event | 3% | 100% |
AgileConfig.Server.Data.Repository.Mongodb | 0% | 0% |
AgileConfig.Server.Data.Entity | 15% | 100% |
AgileConfig.Server.OIDC | 0% | 0% |
AgileConfig.Server.Common | 2% | 3% |
AgileConfig.Server.Data.Repository.Selector | 0% | 0% |
AgileConfig.Server.Apisite | 4% | 2% |
**Summary** | **18%** (2009 / 15061) | **15%** (337 / 3196) | _Minimum allowed line rate is `60%`_ FAIL: Overall line rate below minimum threshold of 60%.

我们发现这个 workflow 跑失败了。原因是我们的 coverage 只有 18%。刚才我们配置了最小值是 60%,低于60就会直接跑出异常,中断 workflow。看日志我们也可以看到对每一个项目的统计。有 line rate,branch rate,health 等内容。

总结

这次我们演示了如何在本地使用 VS 插件对单元测试的结果进行分析获得 coverage。以及演示了如何通过 CodeCoverageSummary 在 Github Action 的 workflow 内对测试结果进行分析。还是非常简单的。希望对大家有帮助。

好了不说了,补单元测试去了。。。

关注我的公众号一起玩转技术

在 Github Action 管道内集成 Code Coverage Report的更多相关文章

  1. CI集成phpunit Error: No code coverage driver is available 的解决

    CI集成phpunit时,运行报No code coverage driver is available的错误,如下图: yanglingdeMacBook-Pro:tests yangling$ p ...

  2. 用 GitHub Action 构建一套 CI/CD 系统

    ​ 缘起 Nebula Graph 最早的自动化测试是使用搭建在 Azure 上的 Jenkins,配合着 GitHub 的 Webhook 实现的,在用户提交 Pull Request 时,加个 r ...

  3. 测试工具 - IDEA - IDEA Code Coverage

    概述 使用 idea 自带的 code coverage 工具 背景 了解 白盒测试用例设计 和 测试覆盖率 之后, 大概就需要 实践 了 实践的话, 还是需要 工具 来检验效果 工具选取 选项 Ja ...

  4. Github Action 快速上手指南

    前言 各位读者,新年快乐,我是过了年匆忙赶回上海努力搬砖的蛮三刀. Github之前更新了一个Action功能(应该是很久以前了),可以实现很多自动化操作.用来替代用户自己设置的自动化脚本(比如:钩子 ...

  5. 使用Github Action自动填写疫情通

    使用Github Action自动填写疫情通 西电晨午晚检一天三次,通过企业号功能进行填写.实际上,西电企业号大部分功能是以网页模式工作的,通过构造connection发送合适的request,设置计 ...

  6. github action 实现CI/CD

    两种github action 打包.Net Core 项目docker镜像推送到阿里云镜像仓库 1.GitHub Actions 是什么? 大家知道,持续集成由很多操作组成,比如抓取代码.运行测试. ...

  7. 白嫖GitHub Action实现开源项目CICD

    什么是CI/CD?让你的项目变得更加敏捷! 在今天这个快速变化的时代,开发者们需要与时俱进,不断提升自己的工作效率.在这篇文章里,将一起探讨如何使用CI/CD和Github Action让你的项目更加 ...

  8. 代码覆盖率 (Code Coverage)从简到繁 (一)

    代码覆盖率(Code Coverage)是反映测试用例对被测软件覆盖程度的重要指标,也是衡量测试工作进展情况的重要指标.它也是对测试工作进行量化的重要指标之一,测试工作往往不如开发那样激动人心,一个重 ...

  9. iOS 9 学习系列: Xcode Code Coverage

    Code coverage 是一个计算你的单元測试覆盖率的工具. 高水平的覆盖给你的单元測试带来信心.也表明你的应用被彻底的測试过了. 你可能写了几千个单元測试,但假设覆盖率不高.那么你写的这套測试可 ...

  10. 使用Visual Studio Code Coverage和nunit上传单元测试覆盖率和单元测试结果到SonarQube上

    SonarQube.Scanner.MSBuild.exe begin /k:"OMDCCQuotes" /d:sonar.host.url="http://myip:9 ...

随机推荐

  1. Win32 自绘控件按钮类

    今天学了控件的自绘,初步偿试了下,蹂躏的不行不行的,查了好多的资料,头都弄大了, 有好多还是没弄明白,只是初步实现一个按钮的基本功能,好难呀, 先看下效果: 按下状态 弹起状态 按钮2按下状态 按钮2 ...

  2. C++11新特性(二):语言特性

    C++11新特性 nullptr空指针 nullptr空指针的使用可以规避掉以往设置为NULL的风险.NULL在编译器中常常被设置为0或者其它数字,此时判断指针是否为NULL,即判断指针类型是否能够等 ...

  3. 我恨 gevent

    报错了一晚上,最后发现是 python 版本不对.3.11,3.12,3.8,3.10 试了个遍,最后 3.10 终于编译通过了‍ 还有这个 greenlet,每次都是它和 gevent 合着来恶心我 ...

  4. Xmind 8思维导图(含补丁)

    Xmind 8思维导图(含补丁) 什么是思维导图? 如何下载Xmind8 Xmind 8软件简单使用 获取Xmind 8 补丁 什么是思维导图? 数据结构.电路模拟等学习路线,老师都有叫画思维导图,那 ...

  5. 【YashanDB知识库】绑定参数,同一个sql多个执行计划的问题

    问题现象 同一个sql有两个执行计划,是否合理? 它的EXECUTIONS,ELAPSED_TIME等统计信息怎么看,是独立分开的还是统一计算的? 如下图: 问题影响版本 tpcc测试:23.2.1. ...

  6. Serilog文档翻译系列(一) - 入门指南

    保持优质文档是 Serilog 的优先事项.如果你发现文档中有缺失或不准确的内容,或者希望通过添加主题或教程来扩展 wiki,请通过问题追踪系统告知我们. 为什么选择Serilog? 与许多其他 .N ...

  7. EF Core – Temporal Table 时态表

    前言 EF core 6.0 开始有 build-in 的 Temporal Table 支持了. 关于 SQL Server Temporal Table 可以看这篇. 主要参考 What's Ne ...

  8. C++ STL stack容器——栈

    stack容器 基本概念 stack是一种先进后出的数据结构,它只有一个出口,形式如下图所示.stack容器允许新增元素,移除元素,取得栈顶元素,但是除了最顶端外,没有任何地方可以存取stack的娶她 ...

  9. CSP-J 2024游记

    CSP-J 2024游记 题目难度 总体来说,这次考试题目对于我这个初一牲难度不高.前面的选择题出现了少量难题(格蕾码). 选择题 选择题出现了一个搞人心态的BYD题目--格蕾码.这道题我蒙的, 阅读 ...

  10. Kubernetes基础(kube-apiserver?kube-controller-manager?kube-scheduler?kubelet?kube-proxy?kubectl?)(十一)

    一.kube-apiserver API Server 提供了资源对象的唯一操作入口,其它所有组件都必须通过它提供的 API 来操作资源数据.只有 API Server 会与 etcd 进行通信,其它 ...