WARNING: Failed to generate report: No data to report error in python using pytest module
Asked Answered
I

3

19

sample.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

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

Coverage command:

pytest test_sample.py --cov=sample.py

Error:

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 =========

Can anyone help me how to resolve this error?

Infatuate answered 20/3, 2020 at 15:7 Comment(0)
M
50

--cov accepts either directories or package names, not single files. This means that --cov=sample.py looks for a package (directory) named sample and inside it, a module (file) named py.py to record coverage, and fails. Either use

$ pytest --cov=sample

or

$ pytest --cov=.
Morris answered 20/3, 2020 at 16:59 Comment(1)
I got this error just by including a forward slash(/) after the directory name like this: pytest --cov=sample/. So, if anyone is facing this issue, just remove the forward slash.Tunnell
A
3

Had the same issue, and got solved by running: coverage run -m pytest .

Atonement answered 23/9, 2021 at 2:19 Comment(3)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Savoy
Please add some explanation to your code.Curricle
This worked for me - thank you for your answer. Afterward, I ran coverage report -m to print the report to the consoleBreland
C
3

Rename test files as 'test_' in order to run the below command.

pytest --cov=<package (directory) or module (file)>

Example: Suppose we have two files in a directory:

  1. sample.py: contains Python code
  2. test_sample.py: contains unit tests for sample.py then coverage command should be:

pytest --cov=sample

Callisto answered 30/12, 2021 at 18:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.