Python 覆盖率报告仅覆盖测试文件

标签 python unit-testing pytorch pytest code-coverage

我对开源项目的贡献还很陌生,我正在尝试获取一些覆盖率报告,以便我可以找出需要更多/更好测试的内容。但是,我无法完全覆盖测试。这是给 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 的覆盖范围。我如何获得完整的覆盖报告,包括测试的类?

任何指导将不胜感激。

最佳答案

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

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

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

  • pytest path/to/test/ --cov --cov-report=html
  • 关于Python 覆盖率报告仅覆盖测试文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60658028/

    相关文章:

    python - 如何比较python中的列表/集合列表?

    c - Valgrind 使用 g_test_trap_subprocess () 给出可能丢失的内存

    javascript - AngularJs Jasmine 单元测试因意外请求而失败

    java - 如何确保方法上存在 @Scheduled 注释

    python - 跨多个进程训练模型时,在 PyTorch 中使用 tensor.share_memory_() 与 multiprocessing.Queue

    python - 我如何在 python 中编写一个简单的 IRC 机器人?

    python - flask 中的多选字段

    python - 使用来自 API Get 请求的数据填充 Django 数据库

    谷歌colab上的pytorch几何 "Detected that PyTorch and torch_sparse were compiled with different CUDA versions"

    python - 如何表示 PyTorch LSTM 3D 张量?