pytest-dev / pytest-cov

Coverage plugin for pytest.
MIT License
1.76k stars 211 forks source link

Some line are actually ran but reported as missing when using with `line_profiler` #597

Closed gameliee closed 1 month ago

gameliee commented 1 year ago

Summary

some lines are actually ran, but reported as missing in coverage.

Expected vs actual result

Expected cov: 100% Actual cov: 76%

Reproducer

reproducer.py

import time
import line_profiler
import pytest

class SlowClass:
    def slowww(self):
        for i in range(10):
            time.sleep(0.1)

@pytest.fixture
def sl():
    profiler = line_profiler.LineProfiler()
    sl = SlowClass()
    # patch methods that we want to profile it
    sl.slowww = profiler(sl.slowww)
    yield sl
    profiler.print_stats()
    print("im here")

def test_profile(sl: SlowClass):
    sl.slowww()

run above code with

❯ pytest -s --cov --cov-report term-missing reproducer.py 
======================================= test session starts =======================================
platform linux -- Python 3.10.4, pytest-7.3.1, pluggy-1.0.0
rootdir: /home/xxxxxxxxxx/new_folder
plugins: cov-4.1.0
collected 1 item                                                                                  

lazada.py .Timer unit: 1e-09 s

Total time: 1.00181 s
File: /home/xxxxxxxxxx/new_folder/reproducer.py
Function: slowww at line 7

Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================
     7                                               def slowww(self):
     8        10      27693.0   2769.3      0.0          for i in range(10):
     9        10 1001781423.0 100178142.3    100.0              time.sleep(0.1)

im here

---------- coverage: platform linux, python 3.10.4-final-0 -----------
Name          Stmts   Miss  Cover   Missing
-------------------------------------------
conftest.py       0      0   100%
lazada.py        17      4    76%   8-9, 19-20
-------------------------------------------
TOTAL            17      4    76%

======================================== 1 passed in 1.03s ========================================

Versions

`pip list` ```text Package Version ------------------ --------- black 23.3.0 certifi 2022.6.15 cfgv 3.3.1 charset-normalizer 3.1.0 click 8.1.3 coverage 7.2.7 distlib 0.3.6 dynaconf 3.1.12 exceptiongroup 1.1.1 filelock 3.12.0 fire 0.5.0 flake8 6.0.0 identify 2.5.24 idna 3.4 iniconfig 2.0.0 line-profiler 4.0.3 mccabe 0.7.0 mypy-extensions 1.0.0 nodeenv 1.8.0 packaging 23.1 pathspec 0.11.1 pip 21.2.4 platformdirs 3.5.1 pluggy 1.0.0 pre-commit 3.3.2 pycodestyle 2.10.0 pydantic 1.10.8 pyflakes 3.0.1 pytest 7.3.1 pytest-cov 4.1.0 PyYAML 6.0 requests 2.31.0 setuptools 61.2.0 six 1.16.0 termcolor 2.3.0 tomli 2.0.1 typing_extensions 4.6.3 urllib3 2.0.2 virtualenv 20.23.0 wheel 0.37.1 ```
❯ python --version
Python 3.10.4
❯ pytest --version
pytest 7.3.1

Config

No tox.ini, pytest.ini, .coveragerc, setup.cfg or any relevant configuration.

Other

Please note that when I commend the line sl.slowww = profiler(sl.slowww) in the reproducer.py, the cov result will be 100%

ionelmc commented 1 month ago

The profiler uninstalls the coverage hooks, there is nothing this plugin can do to prevent that.