【问题标题】:WARNING: Failed to generate report: No data to report error in python using pytest module警告:无法生成报告:使用 pytest 模块在 python 中没有数据报告错误
【发布时间】:2020-07-01 17:49:21
【问题描述】:

示例.py 代码:

def sum(num1, num2):
    return num1 + num2


def sum_only_positive(num1, num2):
    if num1 > 0 and num2 > 0:
        return num1 + num2
    else:
        return None

test_sample.py code

from . import sample

import pytest

def test_sum():
    assert sample.sum(5, 5) == 10

def test_sum_positive_ok():
    assert sample.sum_only_positive(2, 2) == 4

def test_sum_positive_fail():
    assert sample.sum_only_positive(-1, 2) is None

覆盖命令:pytest test_sample.py --cov=sample.py

错误:

platform linux -- Python 3.5.2, pytest-5.4.1, py-1.8.1, pluggy-0.13.1
rootdir: /home/apathapa/unit_test/warriorframework_py3
plugins: cov-2.8.1
collected 3 items                                                                                                                            

test_sample.py ...                                                                                                                     [100%]Coverage.py warning: Module sample.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.

/home/apathapa/ut/lib/python3.5/site-packages/pytest_cov/plugin.py:254: PytestWarning: Failed to generate report: No data to report.

  self.cov_controller.finish()


----------- coverage: platform linux, python 3.5.2-final-0 -----------
Name    Stmts   Miss  Cover
---------------------------


============================================================= 3 passed in 0.13s ==============================================================

谁能帮我解决这个错误?

【问题讨论】:

    标签: python pytest coverage.py pytest-cov


    【解决方案1】:

    --cov 接受目录或包名,而不是单个文件。这意味着--cov=sample.py 查找名为sample 的包(目录)并在其中查找名为py.py 的模块(文件)来记录覆盖率,但失败了。要么使用

    $ pytest --cov=sample
    

    $ pytest --cov=.
    

    【讨论】:

    • 我得到了这个错误,只是在这样的目录名称后面加上一个正斜杠(/):pytest --cov=sample/。因此,如果有人遇到此问题,只需删除正斜杠即可。
    【解决方案2】:

    遇到了同样的问题,通过运行解决了: coverage run -m pytest .

    【讨论】:

    • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
    • 请在您的代码中添加一些解释。
    【解决方案3】:

    将测试文件重命名为“test_”以运行以下命令。

    pytest --cov=

    示例: 假设我们在一个目录中有两个文件:

    1. sample.py:包含 Python 代码
    2. test_sample.py:包含 sample.py 的单元测试 那么覆盖命令应该是:

    pytest --cov=sample

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-21
      • 2015-05-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多