【问题标题】:Why code coverage not 100%? pytest-cov为什么代码覆盖率不是 100%? pytest-cov
【发布时间】:2018-07-17 17:02:17
【问题描述】:

为什么我没有获得 100% 的代码覆盖率?!

所有方法都经过测试...

代码覆盖率是如何计算的?

代码

import os

def get_root_dir():
    return os.path.abspath(os.path.join(os.path.sep, os.path.dirname(os.path.realpath(__file__)), '../../'))


def get_coverage_report_dir():
    return os.path.join(os.path.sep, get_root_dir(), 'coverage_report')

测试

import unittest

class TestPaths(unittest.TestCase):

def test_paths(self):

    import src.utils.paths as paths
    self.assertTrue(paths.get_root_dir().endswith('myproject'))
    self.assertTrue(paths.get_root_dir() in paths.get_coverage_report_dir() and paths.get_coverage_report_dir().endswith('coverage_report'))

报告

---------- coverage: platform win32, python 2.7.14-final-0 -----------
Name                               Stmts   Miss  Cover
------------------------------------------------------
src\utils\__init__.py                  0      0   100%
src\utils\example_util_module.py       2      0   100%
src\utils\paths.py                     5      3    40%
------------------------------------------------------
TOTAL                                  7      3    57%

【问题讨论】:

  • 显示报告。
  • 完成!请参阅@JonathonReinhart
  • 使用--cov-report html 参数获取完整的每行报告。

标签: python code-coverage pytest-cov


【解决方案1】:

html 输出会显示缺失的行

pytest --cov=paths --cov-report=html

然后从新创建的 htmlcov 文件夹中打开 index.html。

这是一个演示实现 100% 测试覆盖率的视频。

https://youtu.be/7BJ_BKeeJyM

【讨论】:

    猜你喜欢
    • 2020-09-27
    • 2015-06-18
    • 2017-03-30
    • 2023-03-28
    • 1970-01-01
    • 2022-06-11
    • 2023-02-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多