coverage代码覆盖率测试介绍
coverage代码覆盖率测试介绍
背景知识补充
1、什么是覆盖率
测试过程中提到的覆盖率,指的是已测试的内容,占待测内容的百分比,在一定程度上反应测试的完整程度。
覆盖率有可以根据要衡量的对象细分很多种,比如接口覆盖率、分支覆盖率、行覆盖率等等
2、做覆盖率有什么用处
覆盖率的好处是可以将测试的完整性量化,可以作为补充测试的手段,也可以在一定程度上佐证测试结果的可靠性。
简介
代码覆盖率测量通常用于衡量测试的有效性。它可以显示你的代码的哪些部分正在被测试执行,哪些没有被执行。
coverage是一个测量 Python 程序代码覆盖率的工具。它监视你的程序,并分析源码生成代码覆盖率报告。
安装
coverage依赖python环境
python环境安装:https://computingforgeeks.com/install-latest-python-on-centos-linux/
安装coverage
python3 -m pip install coverage
or
pip3 install coverage
安装完成
coverage --version
Coverage.py, version 7.2.5 with C extension
Full documentation is at https://coverage.readthedocs.io/en/7.2.5
命令概览
coverage支持命令如下
[yhgao@localhost ~]$ coverage --help
Coverage.py, version 7.2.5 with C extension
Measure, collect, and report on code coverage in Python programs.usage: coverage <command> [options] [args]Commands:annotate Annotate source files with execution information.combine Combine a number of data files.debug Display information about the internals of coverage.pyerase Erase previously collected coverage data.help Get help on using coverage.py.html Create an HTML report.json Create a JSON report of coverage results.lcov Create an LCOV report of coverage results.report Report coverage stats on modules.run Run a Python program and measure code execution.xml Create an XML report of coverage results.Use "coverage help <command>" for detailed help on any command.
Full documentation is at https://coverage.readthedocs.io/en/7.2.5
其中每个小命令后面可以查看更详细的帮助, 以run命令举例
[yhgao@localhost ~]$ coverage run --help
Usage: coverage run [options] <pyfile> [program options]Run a Python program, measuring code execution.Options:-a, --append Append coverage data to .coverage, otherwise it startsclean each time.--branch Measure branch coverage in addition to statementcoverage.--concurrency=LIBS Properly measure code using a concurrency library.Valid values are: eventlet, gevent, greenlet,multiprocessing, thread, or a comma-list of them.--context=LABEL The context label to record for this coverage run.--data-file=OUTFILE Write the recorded coverage data to this file.Defaults to '.coverage'. [env: COVERAGE_FILE]--include=PAT1,PAT2,...Include only files whose paths match one of thesepatterns. Accepts shell-style wildcards, which must bequoted.-m, --module <pyfile> is an importable Python module, not a scriptpath, to be run as 'python -m' would run it.--omit=PAT1,PAT2,... Omit files whose paths match one of these patterns.Accepts shell-style wildcards, which must be quoted.-L, --pylib Measure coverage even inside the Python installedlibrary, which isn't done by default.-p, --parallel-mode Append the machine name, process id and random numberto the data file name to simplify collecting data frommany processes.--source=SRC1,SRC2,...A list of directories or importable names of code tomeasure.--timid Use a simpler but slower trace method. Try this if youget seemingly impossible results!--debug=OPTS Debug options, separated by commas. [env:COVERAGE_DEBUG]-h, --help Get help on this command.--rcfile=RCFILE Specify configuration file. By default '.coveragerc','setup.cfg', 'tox.ini', and 'pyproject.toml' aretried. [env: COVERAGE_RCFILE]Full documentation is at https://coverage.readthedocs.io/en/7.2.5
快速使用
如果你的测试运行程序命令以“python”开头,只需将初始的“python”替换为“coverage run”即可。
python something.py -->> becomes -->> coverage run something.pypython -m amodule -->> becomes -->> coverage run -m amodule
python其他单元测试框架
pytest
pytest arg1 arg2 arg3 -->> becomes -->> coverage run -m pytest arg1 arg2 arg3unittest
python -m unittest discover -->> becomes -->> coverage run -m unittest discovernosetest
nosetests arg1 arg2 -->> becomes -->> coverage run -m nose arg1 arg2
测试完成后会在当前目录下默认生成一个.coverage
文件,这里面保存了代码覆盖率结果,也可以通过–data-file生成指定名称的文件
直接查看
coverage report
$ coverage report -m
Name Stmts Miss Cover Missing
-------------------------------------------------------
my_program.py 20 4 80% 33-35, 39
my_other_module.py 56 6 89% 17-23
-------------------------------------------------------
TOTAL 76 10 87%
其他查看方式
使用其他格式查看,例如html,会生成html文件,使用浏览器打开index.html即可查看
coverage html[yhgao@localhost coverage]$ coverage html
Wrote HTML report to htmlcov/index.html
[yhgao@localhost coverage]$ ll
总用量 4
-rw-rw-r-- 1 yhgao yhgao 679 6月 15 16:49 demo.py
drwxrwxr-x 2 yhgao yhgao 190 6月 15 16:51 htmlcov[yhgao@localhost coverage]$ cd htmlcov/
[yhgao@localhost htmlcov]$ ls -al
总用量 92
drwxrwxr-x 2 yhgao yhgao 190 6月 15 16:51 .
drwxrwxr-x 3 yhgao yhgao 53 6月 15 16:51 ..
-rw-rw-r-- 1 yhgao yhgao 21359 6月 15 16:51 coverage_html.js
-rw-rw-r-- 1 yhgao yhgao 11790 6月 15 16:51 demo_py.html
-rw-rw-r-- 1 yhgao yhgao 1732 6月 15 16:51 favicon_32.png
-rw-rw-r-- 1 yhgao yhgao 27 6月 15 16:51 .gitignore
-rw-rw-r-- 1 yhgao yhgao 3816 6月 15 16:51 index.html //浏览器打开该文件
-rw-rw-r-- 1 yhgao yhgao 9004 6月 15 16:51 keybd_closed.png
-rw-rw-r-- 1 yhgao yhgao 9003 6月 15 16:51 keybd_open.png
-rw-rw-r-- 1 yhgao yhgao 236 6月 15 16:51 status.json
-rw-rw-r-- 1 yhgao yhgao 12387 6月 15 16:51 style.css
Demo
下面以一个demo.py举例
测试目标(%):100
测试case数量(个):3
//查看demo文件
[yhgao@bogon coverage]$ cat demo.py
import sysnum = int(sys.argv[1])
if num == 1:print('output num: 1')
else:if num == 2:print('output num: 2')elif (num > 2 or num < 1):print('error input num')//python执行结果
[yhgao@bogon coverage]$ python demo.py 1
output num: 1
[yhgao@bogon coverage]$ python demo.py 2
output num: 2
[yhgao@bogon coverage]$ python demo.py 3
error input num//coverage执行代码覆盖率(区分单个case情况)
[yhgao@bogon coverage]$ coverage run --data-file=c1 demo.py 1
output num: 1
[yhgao@bogon coverage]$ coverage run --data-file=c2 demo.py 2
output num: 2
[yhgao@bogon coverage]$ coverage run --data-file=c3 demo.py 3
error input num//查看每个代码覆盖率文件
[yhgao@bogon coverage]$ coverage report --data-file=c1
Name Stmts Miss Cover
-----------------------------
demo.py 8 4 50%
-----------------------------
TOTAL 8 4 50%
[yhgao@bogon coverage]$ coverage report --data-file=c2
Name Stmts Miss Cover
-----------------------------
demo.py 8 3 62%
-----------------------------
TOTAL 8 3 62%
[yhgao@bogon coverage]$ coverage report --data-file=c3
Name Stmts Miss Cover
-----------------------------
demo.py 8 2 75%
-----------------------------
TOTAL 8 2 75%//合并代码覆盖率文件
[yhgao@bogon coverage]$ coverage combine --data-file=cc c1 c2 c3
Combined data file c1
Combined data file c2
Combined data file c3//查看合并后的代码覆盖率文件
[yhgao@bogon coverage]$ coverage report --data-file=cc
Name Stmts Miss Cover
-----------------------------
demo.py 8 0 100%
-----------------------------
TOTAL 8 0 100%可以看到整合之后的代码覆盖率达到100%,我们也就可以据此判断我们的3个测试case是符合要求的。//coverage执行代码覆盖率(不区分单个case情况)
未指定-a的情况:
[yhgao@bogon coverage]$ coverage run --data-file=cc2 demo.py 1
output num: 1
[yhgao@bogon coverage]$ coverage run --data-file=cc2 demo.py 2
output num: 2
[yhgao@bogon coverage]$ coverage run --data-file=cc2 demo.py 3
error input num
[yhgao@bogon coverage]$ coverage report --data-file=cc2
Name Stmts Miss Cover
-----------------------------
demo.py 8 2 75%
-----------------------------
TOTAL 8 2 75%代码覆盖率是会被覆盖的,所以只保留最后一个case的代码覆盖率指定-a的情况:
[yhgao@bogon coverage]$ coverage run -a --data-file=cc2 demo.py 1
output num: 1
[yhgao@bogon coverage]$ coverage run -a --data-file=cc2 demo.py 2
output num: 2
[yhgao@bogon coverage]$ coverage run -a --data-file=cc2 demo.py 3
error input num
[yhgao@bogon coverage]$ coverage report --data-file=cc2
Name Stmts Miss Cover
-----------------------------
demo.py 8 0 100%
-----------------------------
TOTAL 8 0 100%指定-a和同一个data-file即可不用执行coverage combine,直接得到一份儿完整的代码覆盖率文件
相关文章:

coverage代码覆盖率测试介绍
coverage代码覆盖率测试介绍 背景知识补充 1、什么是覆盖率 测试过程中提到的覆盖率,指的是已测试的内容,占待测内容的百分比,在一定程度上反应测试的完整程度。 覆盖率有可以根据要衡量的对象细分很多种,比如接口覆盖率、分支…...

使用 Debian、Docker 和 Nginx 部署 Web 应用
前言 本文将介绍基于 Debian 的系统上使用 Docker 和 Nginx 进行 Web 应用部署的过程。着重介绍了 Debian、Docker 和 Nginx 的安装和配置。 第 1 步:更新和升级 Debian 系统 通过 SSH 连接到服务器。更新软件包列表:sudo apt update升级已安装的软件…...

Redis原理 - 内存策略
原文首更地址,阅读效果更佳! Redis原理 - 内存策略 | CoderMast编程桅杆https://www.codermast.com/database/redis/redis-memery-strategy.html Redis 本身是一个典型的 key-value 内存存储数据库,因此所有的 key、value 都保存在之前学习…...

【计算机网络】IP 地址处理函数
目录 1.struct sockaddr_in的结构 2.一般我们写的结构 3.常见的“点分十进制” 到 ” uint32_t 的转化接口 3.1. inet_aton 和 inet_ntoa (ipv4) 3.2. inet_pton 和 inet_ntop (ipv4 和 ipv6) 3.3. inet_addr 和 inet_network 3…...

9i物联网浏览器(cef_114.2.110114.2.100支持H264视频)WinForm-CefSharp114(5735)视频版本体验
更新:2023.6.25 版本:Cef_114.2.110和114.2.100+chromium-114.0.5735.134的32位和64位 说明:支持图片,mp3,mp4(H264)多媒体 测试环境:windows server 2019 测试网址:www.html5test.com 1.包下载地址 1.1 https://www.nuget.org/packages/CefSharp.Common/ 1.2 https…...

如何在本地运行一个已关服但具有客户端的游戏
虽然游戏服务器关闭后,我们通常无法再进行在线游戏,但对于一些已经关服但仍保留客户端的游戏来说,我们仍然可以尝试在本地进行游玩。本文将介绍如何在本地运行一个已关服但具有客户端的游戏的方法。 一、获取游戏客户端 要在本地运行一个已关…...

C语言编程—预处理器
预处理器不是编译器的组成部分,但是它是编译过程中一个单独的步骤。简言之,C 预处理器只不过是一个文本替换工具而已,它们会指示编译器在实际编译之前完成所需的预处理。我们将把 C 预处理器(C Preprocessor)简写为 CP…...

使用 Maya Mari 设计 3D 波斯风格道具(p1)
今天瑞云渲染小编给大家带来了Simin Farrokh Ahmadi 分享的Persian Afternoon 项目过程,解释了 Maya 和 Mari 中的建模、纹理和照明过程。 介绍 我的名字是西敏-法罗赫-艾哈迈迪,人们都叫我辛巴 在我十几岁的时候,我就意识到我喜欢艺术和创造…...

Redis分布式问题
Redis实现分布式锁 Redis为单进程单线程模式,采用队列模式将并发访问变成串行访问,且多客户端对Redis的连接并不存在竞争关系Redis中可以使用SETNX命令实现分布式锁。当且仅当 key 不存在,将 key 的值设为 value。 若给定的 key 已经存在&…...

synchronized原理
目录 一、基本特点 二、加锁过程 2.1、偏向锁 2.2、轻量级锁 2.3、重量级锁 三、其它的优化操作 3.1、锁消除 3.2、锁粗化 一、基本特点 synchronized有以下特性: 开始是乐观锁,如果锁冲突频繁,就转换为悲观锁。开始是轻量级锁,…...

10G光模块能兼容千兆光口吗
当涉及到光网络设备和光模块的兼容性时,确保正确的匹配是至关重要的。本期文章内容,我们将探讨10G光模块与千兆光口之间的兼容性。 一、10G光模块和千兆光口的基本概念 首先,我们需要了解10G光模块和千兆光口的基本概念。10G光模块是一种用…...

css 显示省略号 和 动态显示省略号
省略是非常常见的功能。 简单的实现省略号 下面的代码就可以实现省略号,超过宽度的时候就会出现省略号 .text-name{//宽高是一定要设置的不然是会无效延伸的width: 200rpx;overflow: hidden;white-space: nowrap;text-overflow: ellipsis;}稍微复杂点的情况&#…...

LeetCode 1253. 重构 2 行二进制矩阵
【LetMeFly】1253.重构 2 行二进制矩阵 力扣题目链接:https://leetcode.cn/problems/reconstruct-a-2-row-binary-matrix/ 给你一个 2 行 n 列的二进制数组: 矩阵是一个二进制矩阵,这意味着矩阵中的每个元素不是 0 就是 1。第 0 行的元素之…...

【八股】【C++】内存
这里写目录标题 内存空间分配new和delete原理C有几种newmalloc / free 与 new / delete区别malloc和free原理?delete和delete[]区别?C内存泄漏malloc申请的存储空间能用delete释放吗?malloc、calloc函数、realloc函数C中浅拷贝与深拷贝栈和队列的区别C里…...

数据库G等待
> db^Cgbasedbtpc:~$ dbaccess db10 -Database selected.> call insert_t();Routine executed.Elapsed time: 811.630 sec 磁盘逻辑日志,无BUF库> ^Cgbasedbtpc:~$ gbasedbtpc:~$ dbaccess db10 -Database selected.> call insert_t();Routine executed.Elapse…...

PCB封装设计指导(一)基础知识
PCB封装设计指导(一)基础知识 PCB封装是PCB设计的基础,也是PCB最关键的部件之一,尺寸需要非常准确且精确,关系到设计,生产加工,贴片等后续一系列的流程。 下面以Allegro为例介绍封装创建前的一些基础知识 1.各个psm文件代表什么 mechanical symbol 是.bsm Package sy…...

Flask框架之Restful--介绍--下载--基本使用
目录 Restful 概念 架构的主要原则 适用场景 协议 数据传输格式 url链接规则 HTTP请求方式 状态码 Restful的基本使用 介绍 优势 缺点 安装 基本使用 注意 Restful 概念 RESTful(Representational State Transfer)是一种用于设计网络应用…...

2023年上海市浦东新区网络安全管理员决赛理论题样题
目录 一、判断题 二、单选题 三、多选题 一、判断题 1.等保1.0至等保2.0从信息系统拓展为网络和信息系统。 正确 (1)保护对象改变 等保1.0保护的对象是信息系统,等保2.0增加为网络和信息系统,增加了云计算、大数据、工业控制系统、物联网、移动物联技术、网络基础…...

SQL语言的四大组成部分——DCL(数据控制语言)
1️⃣前言 SQL语言中的DCL(Data Control Language)是一组用于控制数据库用户访问权限的语言,主要包括GRANT、REVOKE、DENY等关键字。 文章目录 1️⃣前言2️⃣DCL语言3️⃣GRANT关键字4️⃣REVOKE关键字5️⃣DENY关键字6️⃣总结附࿱…...

ChatGPT新功能曝光:可记住用户信息、上传文件和工作区
🦉 AI新闻 🚀 ChatGPT新功能曝光:可记住用户信息、上传文件和工作区 摘要:一张神秘截图曝光了ChatGPT新功能,包括可记住用户信息的"My profile"、上传和管理文件的"My files"以及可以让AI使用不…...

【Unity编辑器扩展】(三)PSD转UGUI Prefab, 一键拼UI解放美术/程序(完结)
工具效果: 第一步,把psd图层转换为可编辑的节点树,并自动解析UI类型、自动绑定UI子元素: 第二步, 点击“生成UIForm"按钮生成UI预制体 (若有UI类型遗漏可在下拉菜单手动点选UI类型): 验证一键生成UI效果: 书接上…...

SpringBoot开发Restful风格的接口实现CRUD功能
基于SpringBoot开发一个Restful接口 前言一、什么是SpringBoot?二、实战---基于SpringBoot开发一个Restful接口1.开发前的准备工作1.1 添加相关依赖 (pom文件) 1.2 创建相关数据库和表1.3 数据库配置文件 2.实战开发---代码逻辑2.1 实体类2.2…...

【Servlet学习三】实现一个内存版本的简易计算器~
目录 一、方式1:使用form表单的形式(不推荐) 🌈1、前端代码:HTML文件 🌈2、后端代码:Calculator_form.java文件 🌈3、最终效果 二、方式2:使用ajax形式(…...

Linux c语言获取本机网关 ip 地址
文章目录 前言一、获取本机网关 ip 地址1.1 代码示例1.2 代码详解介绍 二、使用Netlink套接字实时监控网络事件2.1 简介2.2 示例代码 前言 这篇文章写了获取本机的ip地址和子网掩码:Linux c语言获取本机 ip、子网掩码 一、获取本机网关 ip 地址 使用Netlink套接字…...

nginx部署本地项目如何让异地公网访问?服务器端口映射配置!
接触过IIS或apache的小伙伴们,对nginx是比较容易理解的,nginx有点类似,又有所差异,在选择使用时根据自己本地应用场景来部署使用即可。通过一些对比可能会更加清楚了解: 1.nginx是轻量级,比apache占用更少…...

云时代已至,新一代数据分析平台是如何实现的?
2023 年 5 月,由 Stackoverflow 发起的 2023 年度开发者调查数据显示,PostgreSQL 已经超越 MySQL 位居第一,成为开发人员首选。PostgreSQL 在国内的热度也越来越高。6 月 17 日,PostgreSQL 数据库技术峰会在成都顺利召开。本次大会…...

【C#】简单聊下Framework框架下的事务
框架用的多了,之前版本的事务都忘记了。本次简单聊下.net framework 4.8框架下本身的事务 目录 1、SqlClient2、TransactionScope3、引用 1、SqlClient 在 C# 中,使用 using 块可以方便地实现对资源的自动释放,但它不适用于实现事务处理。为…...

asyncPool并发执行请求函数
asyncPool应用场景 一个不太常见的极端场景,当我们为了某个操作需要发生异步请求的时候,等待所有异步请求都完成时进行某些操作。这个时候我们不在简简单单的发送 1 - 2 个请求而是 5 - 10个(其实极端场景式 很多很多个请求,这个…...

Ubuntu 22.04上安装NFS服务
1、使用如下命令安装NFS服务端软件: # 在主机上运行以下命令 orangepiorangepi5:~$ sudo apt install nfs-server 2、在配置NFS时需要使用用户uid和组gid,可以使用id命令查看 # 在主机上运行id命令 orangepiorangepi5:~$ id uid1000(orangepi) gid100…...

数据结构--双链表
数据结构–双链表 单链表 VS 双链表 单链表:无法逆向检索,有时候不太方便 双链表:可进可退,存储密度更低一丢丢 双链表的定义 typedef struct DNode {ElemType data;struct DNode *prior, *next; }DNode, *DLinkList;双链表的初…...