pytest-cov:好用的统计代码测试覆盖率插件

简介:pytest-cov是pytest统计测试代码覆盖率的一个插件。pytest-cov插件生成覆盖率报告。与只使用覆盖率相比,这个插件做了一些额外的事情。

1、可以在子流程中派生或运行东西,并且可以在没有任何麻烦的情况下进行覆盖。

2、支持xdist:可以使用pytest-xist的所有功能,并且仍然可以获得覆盖范围。

3、一致的pytest行为。运行coverage run-m pytest,您将有稍微不同的sys.path(CWD将在其中,与运行pytest时不同)。

4、覆盖率包提供的所有功能都应该可以通过pytest-cov的命令行选项或覆盖率的配置文件工作。

历史攻略:

Python:代码覆盖率工具coverage

容器化CICD+Sonar-Scarnner代码扫描

Py-spy:Python 程序性能监控、分析器

安装:

pip install pytest-cov

目录结构:

myProject2024
├─lib
│  └─my_add.py
├─logs
├─htmlcov
├─report
├─test_case
│  └─test_add.py
├─pytest.ini
└─main.py

使用步骤:

1、pytest.ini

# pytest.ini
[pytest]
markers = serial: mark test to run serially
addopts = --cov=lib --cov-report=html

2、./lib/my_add.py

# -*- coding: utf-8 -*-
# time: 2024/4/4 23:38
# file: my_add.py
# 公众号: 玩转测试开发

class AddDemo:

    def add_demo(self, a, b):
        return a + b

3、./test_case/test_add.py

# -*- coding: utf-8 -*-
# time: 2024/4/4 23:39
# file: test_add.py
# 公众号: 玩转测试开发
from lib.my_add import AddDemo
from logger import log


class TestAdd:
    def test_add_01(self):
        assert (AddDemo().add_demo(2, 3) == 5)

    def test_add_02(self):
        assert (AddDemo().add_demo(1, 8) == 9)

4、main.py

# -*- coding: utf-8 -*-
# time: 2024/03/24 10:30
# file: main.py
# 公众号: 玩转测试开发
import os
import pytest

if __name__ == "__main__":
    # step-1:use pytest run test_case

    pytest.main(["-s", "-n", "auto", "--cov=lib", "--alluredir", "./report"])
    # pytest.main(["-s", "-n", "auto", "--cov=year2024"])

    # step-2:auto report json data,zip to allure-html
    os.system("allure serve report")

5、运行结果:

图片

6、查看cov代码覆盖报告 和 allure报告

cov代码覆盖报告 - 汇总

图片

cov代码覆盖报告 - 明细

图片

allure报告:

图片

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值