【问题标题】:Python coverage report covering only test filePython 覆盖率报告仅涵盖测试文件
【发布时间】:2020-03-12 16:16:14
【问题描述】:

我对为开源项目做出贡献还很陌生,我正在尝试获取一些覆盖率报告,以便找出需要更多/更好测试的内容。但是,我无法全面覆盖测试。这是给pytorch

例如,假设我想获取 test_indexing_py 的覆盖率报告。

我运行命令:

pytest test_indexing.py --cov=../ --cov-report=html

导致:

================================================= test session starts =================================================
platform win32 -- Python 3.7.4, pytest-5.2.1, py-1.8.0, pluggy-0.13.0
rootdir: C:\Projects\pytorch
plugins: hypothesis-5.4.1, arraydiff-0.3, cov-2.8.1, doctestplus-0.4.0, openfiles-0.4.0, remotedata-0.3.2
collected 62 items

test_indexing.py ............................s.................................                                  [100%]

----------- coverage: platform win32, python 3.7.4-final-0 -----------
Coverage HTML written to dir htmlcov


=========================================== 61 passed, 1 skipped in 50.43s ============================================

好的,看起来测试已经运行。现在,当我检查 html coverage report 时,我只获得了测试文件的覆盖率,而不是测试的类(测试按覆盖率百分比排序)。

如您所见,我只获得了 test_indexing.py 的报道。如何获得包含测试类的完整覆盖率报告?

任何指导将不胜感激。

【问题讨论】:

  • 传递包名而不是路径,例如pytest ... --cov=pytorch
  • @hoefling 谢谢。这是我使用的特定命令:pytest test_indexing.py --cov-report html --cov=torch

标签: python unit-testing pytorch pytest code-coverage


【解决方案1】:

我认为这是因为您要求检查测试运行目录的覆盖率,即test_indexing.py 所在的位置。

更好的方法是从根目录本身运行测试,而不是测试目录,它有几个优点,比如读取配置文件等等。

关于您的问题,请尝试从根目录运行测试并尝试

  • pytest path/to/test/ --cov --cov-report=html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-28
    • 1970-01-01
    • 2018-05-21
    • 2018-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多