pytest --cov 告诉我我还没有导入我拥有的东西

问题描述 投票:0回答:3

这是我运行的内容和得到的结果:

me $ pytest --cov MakeInfo.py
================================================================================ test session starts =================================================================================
platform darwin -- Python 3.7.4, pytest-6.2.5, py-1.10.0, pluggy-0.13.0
rootdir: /Users/me/Documents/workspace-vsc/Pipeline/src/python
plugins: cov-2.12.1, arraydiff-0.3, remotedata-0.3.2, doctestplus-0.4.0, openfiles-0.4.0
collected 42 items                                                                                                                                                                   

test_MakeInfo.py ............                                                                                                                                                  [ 28%]
test_MakeJSON.py ...                                                                                                                                                           [ 35%]
test_convert_refflat_to_bed.py ..                                                                                                                                              [ 40%]
test_generate_igv_samples.py ...                                                                                                                                               [ 47%]
test_sample_info_to_jsons.py ....                                                                                                                                              [ 57%]
read_coverage/test_intervaltree.py .........                                                                                                                                   [ 78%]
util/test_util.py .........                                                                                                                                                    [100%]**Coverage.py warning: Module MakeInfo.py was never imported**. (module-not-imported)
Coverage.py warning: No data was collected. (no-data-collected)
WARNING: Failed to generate report: No data to report.

/Users/me/opt/anaconda3/lib/python3.7/site-packages/pytest_cov/plugin.py:271: PytestWarning: Failed to generate report: No data to report.

  self.cov_controller.finish()

这是 test_MakeInfo.py 的顶部:

import pytest
import os
import sys
import json

import MakeInfo
from MakeInfo import main, _getNormalTumorInfo

我在寻找什么:告诉我 test_MakeInfo.py 中的测试覆盖了多少 MakeInfo.py

我得到的是:困惑

我的命令是否符合我的要求?没有任何内容调用 MakeInfo.py,它是独立的并从命令行调用。所以当然其他测试都不包括它。

有没有办法告诉 pytest --cov 查看这个测试文件和源文件,并忽略其他所有内容?

python pytest code-coverage
3个回答
2
投票

尝试将调用更改为 python 模块名称

MakeInfo
而不是文件名
MakeInfo.py
。或者,如果它位于内部子目录中,请使用点
.
表示法,例如
some_dir.some_subdir.MakeInfo

pytest --cov MakeInfo

2
投票

所以,事实证明,在这种情况下,参数的顺序是关键

pytest test_MakeInfo.py --cov

在我的一个测试文件上运行 pytest,并为我提供一个源文件的覆盖率信息

pytest --cov test_MakeInfo.py

尝试针对每个测试文件运行(其中一个是由我团队中的其他人编写的,并且显然使用了不同的测试工具,因此当 pytest 尝试运行它时它会抛出错误)

pytest --cov MakeInfo

是其中的一部分:它运行所有测试,包括失败的测试,但随后为我提供了我想要的覆盖率信息

所以,如果您有多个针对单个源文件的测试文件,@Niel 的答案就是您想要的


0
投票

只是为了添加可能出现此消息的另一个原因。

该报告在我的本地 Windows 计算机上运行良好。然而,当它通过我们的 CI 管道(Bitbucket Pipelines)运行时,命令失败了。我使用的命令是

python -m pytest --cov=pyMymodule --cov-fail-under=95 --junitxml=test-reports/report.xml tests

最终,我意识到我打错了模块名称,

pyMymodule
而不是
pyMyModule
。 Windows/NTFS 不区分大小写,但是,我的管道运行的是 Alpine Linux。使用模块名称的正确大小写为我解决了问题。

© www.soinside.com 2019 - 2024. All rights reserved.