pytest-cov:好用的统计代码测试覆盖率插件
pytest-cov是pytest统计测试代码覆盖率的一个插件。pytest-cov插件生成覆盖率报告。运行coverage run-m pytest,您将有稍微不同的sys.path(CWD将在其中,与运行pytest时不同)。4、覆盖率包提供的所有功能都应该可以通过pytest-cov的命令行选项或覆盖率的配置文件工作。2、支持xdist:可以使用pytest-xist的所有功能,并且仍然可以
简介:pytest-cov是pytest统计测试代码覆盖率的一个插件。pytest-cov插件生成覆盖率报告。与只使用覆盖率相比,这个插件做了一些额外的事情。
1、可以在子流程中派生或运行东西,并且可以在没有任何麻烦的情况下进行覆盖。
2、支持xdist:可以使用pytest-xist的所有功能,并且仍然可以获得覆盖范围。
3、一致的pytest行为。运行coverage run-m pytest,您将有稍微不同的sys.path(CWD将在其中,与运行pytest时不同)。
4、覆盖率包提供的所有功能都应该可以通过pytest-cov的命令行选项或覆盖率的配置文件工作。
历史攻略:
安装:
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报告:

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐

所有评论(0)