Search code examples
pythonpytestpydevpytest-cov

PyDevD is preventing pytest to correctly generate coverage


I am dealing with an issue of generation coverage which does not work as expected. Finally I found out it's somehow connected to pydevd (PyDev debugger) package.

To reproduce my issue you can use following repo: cov-project

There are only two files. One in my_package with

import pydevd  # Just for the purpose to show the problem

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

def mult(num1, num2):
    return num1 * num2

And the test file test_math.py in tests:

from my_package.math import sum, mult

def test_sum():
    assert sum(1, 2) == 3

def test_mult():
    assert mult(2, 3) == 6

When I remove import pydevd from the first file and run python -m pytest --cov=my_package tests I get this output:

Name                     Stmts   Miss  Cover
--------------------------------------------
my_package/__init__.py       0      0   100%
my_package/math.py           4      0   100%
--------------------------------------------
TOTAL                        4      0   100%

Yes, 100% coverage as expected. But when I run the same and keeping importing pydevd I get:

Name                     Stmts   Miss  Cover
--------------------------------------------
my_package/__init__.py       0      0   100%
my_package/math.py           5      4    20%
--------------------------------------------
TOTAL                        5      4    20%

The testing is completely same, the only change is this additional import. Of course, in this setup pydevd is unused and I can simply remove it. But I use it in my proprietary code so I wanted to show the simplest way how to reproduce my error since I cannot generate appropriate coverage report when using pydevd. I could change it to local import as I do not use it in my tests, then it would work, but I'm trying to understand what is happening here and why would pydevd prevent coverage from successful report generation.

Tested:

  • with python 3.8 and 3.9
  • with pydevd 2.2.0 and 2.3.0
  • pytest 6.2.2
  • pytest-cov 2.11.1

Solution

  • This was really a bug in pydevd.

    I've fixed it in https://github.com/fabioz/PyDev.Debugger/commit/be32cd821f2574566385deeec23081365bf16722

    You can apply the same fix to your local version in the meanwhile.

    To find out the file for the fix you can do:

    import pydevd
    from _pydevd_bundle import pydevd_constants
    print(pydevd_constants.__file__)