Pytest py.test给出Coverage.py警告:从未导入模块sample.py

Pytest py.test给出Coverage.py警告:从未导入模块sample.py,pytest,coverage.py,Pytest,Coverage.py,我从这个线程运行了一个示例代码。 但是,当我执行这个命令时,py.test.py--cov=sample.py 它给了我一个警告,因此,没有创建任何报告 platform linux2 -- Python 2.7.12, pytest-3.2.3, py-1.4.34, pluggy-0.4.0 rootdir: /media/sf_Virtual_Drive/ASU/CSE565_testand validation/Assignments/temp, inifile: plugins:

我从这个线程运行了一个示例代码。

但是,当我执行这个命令时,
py.test.py--cov=sample.py
它给了我一个警告,因此,没有创建任何报告

platform linux2 -- Python 2.7.12, pytest-3.2.3, py-1.4.34, pluggy-0.4.0
rootdir: /media/sf_Virtual_Drive/ASU/CSE565_testand 
validation/Assignments/temp, inifile:
plugins: cov-2.5.1
collected 3 items                                                                                                                                                                                                                      

test.py ...Coverage.py warning: Module sample.py was never imported. (module-not-imported)
Coverage.py warning: No data was collected. (no-data-collected)
有人知道为什么
coverage.py
不起作用吗


因此,如果我单独运行
coverage run-m py.test test.py
,它不会显示任何警告。

简短回答:您需要使用模块名而不是文件名运行:
pytest--cov sample test.py

您链接的答案()中的一条注释解释说,如果您试图获取覆盖率的文件是由测试导入的模块,那么这似乎不起作用。我能够重现:

/sample.py

/test.py

我得到了同样的错误:

$ pytest --cov sample.py test.py
========================================================================================== test session starts ===========================================================================================
platform darwin -- Python 3.7.2, pytest-4.3.1, py-1.8.0, pluggy-0.9.0
rootdir: /path/to/directory, inifile:
plugins: cov-2.6.1
collected 1 item

test.py .                                                                                                                                                                                          [100%]Coverage.py warning: Module sample.py was never imported. (module-not-imported)
Coverage.py warning: No data was collected. (no-data-collected)
/path/to/directory/.venv/lib/python3.7/site-packages/pytest_cov/plugin.py:229: PytestWarning: Failed to generate report: No data to report.

  self.cov_controller.finish()
WARNING: Failed to generate report: No data to report.
但是,当使用模块名称时:

pytest --cov sample test.py
========================================================================================== test session starts ===========================================================================================
platform darwin -- Python 3.7.2, pytest-4.3.1, py-1.8.0, pluggy-0.9.0
rootdir: /path/to/directory, inifile:
plugins: cov-2.6.1
collected 1 item

test.py .                                                                                                                                                                                          [100%]

---------- coverage: platform darwin, python 3.7.2-final-0 -----------
Name        Stmts   Miss  Cover
-------------------------------
sample.py       2      0   100%
文档似乎表明您可以使用
路径
,但它可能并非在所有情况下都有效…

tl;博士 使用
coverage
生成统计文件
.coverage
,然后创建仅适用于特定文件的报告

coverage run -m pytest .\test\test_named_prng.py
coverage html --include=named_prng.py 
处境 假设包中有一些python文件,并且在单个测试文件中也有测试用例(
test/test\u name_prng.py
)。您希望在包中的一个特定文件(
named_prng.py
)上测量测试文件的代码覆盖率

这里导入
examples.py
namedpng/\uuuu init\uuuuuuuuuuuuuuuuupy
,其中另一个init文件为空。

问题 您的问题是,使用
pytest
coverage
时,您无法将报告范围限定到您的特定文件(
named_prng.py
),因为从包中导入的所有其他文件也包含在报告中

根本原因 如果在要导入的模块所在的级别中有一个
\uuuuu init\uuuuuuuuuupy
,则在执行
\uuuuu init\uuuuuuuuupy
时,
\uuuuuu init\uuuuuuuuuuuuuupy.py
可能会导入比需要更多的文件。有一些选项可以告诉pytest和coverage来限制您要调查的模块,但是如果它们涉及包中的其他模块,也将对它们进行分析

pytest症状 如果要在其上创建覆盖率测试的(子)模块是从
\uu init\uuuuuuuuuuuuupy
导入的,则在使用
-cov
选项发布
-pytest
时使用的包
-cov

\namedPrng
│   examples.py
│   named_prng.py
│   README.md
│   timeit_meas.py
│   __init__.py
│
└───test
        test_named_prng.py
        __init__.py
如果您使用

除了
timeit_meas.py
文件之外,每个
.py
文件都会收到一份报告,因为它从未被导入:测试、它的init、导入的名为_prng.py的
以及它的init

如果使用运行pytest

pytest .\test\test_named_prng.py --cov=./ --cov-report=html
然后您明确地告诉
coverage
(通过
pytest
调用)包含级别中的所有内容,因此每个
.py
文件都将包含在报告中

您希望告诉coverage仅在名为_prng.py的
的源代码上创建报告,但如果您将模块指定为
--cov
,则

pytest .\test\test_named_prng.py --cov --cov-report=html
pytest .\test\test_named_prng.py --cov=named_prng --cov-report=html
或者使用
--cov=named_prng.py
您将收到一条警告:

Coverage.py warning: Module named_prng.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.
覆盖范围
的症状
可以分别运行覆盖和报告,并希望可以将更详细的选项传递给覆盖

发行

coverage run -m pytest .\test\test_named_prng.py
coverage html
在5个
.py
文件上会得到相同的报告。如果您试图告诉
coverage
仅使用
named_prng.py
by

coverage run --source=named_prng -m pytest .\test\test_named_prng.py
或者使用
--source=named_prng.py
,您将收到警告

Coverage.py warning: Module named_prng.py was never imported. (module-not-imported)
Coverage.py warning: No data was collected. (no-data-collected)
并且不会创建任何报告

解决方案 您需要使用
--include
开关进行
覆盖
,不幸的是,该开关无法在CLI中传递到
pytest

使用
coverage
CLI 您可以在代码覆盖率计算期间限制调查范围:

coverage run --include=named_prng.py -m pytest .\test\test_named_prng.py
coverage html
或在报告时间

coverage run -m pytest .\test\test_named_prng.py
coverage html --include=named_prng.py 
使用
pytest
+设置文件 可以通过配置文件调用带有详细配置的
pytest
。在发布
pytest
的地方,设置包含内容的
.coveragerc
文件

[run]
include = named_prng.py
检查可能的选项和模式。

我认为
--cov
需要模块名,而不是文件名。请改为尝试
--cov=sample
coverage run -m pytest .\test\test_named_prng.py
coverage html --include=named_prng.py 
[run]
include = named_prng.py