Unable to debug in pycharm with pytest

25,079

Solution 1

For my situation, I found what the problem is:

If there is --cov in pytest.ini, then breakpoints in pycharm won't work, after deleting all --cov in pytest.ini, the breakpoints in pycharm can work.

Reason:

"The coverage module and pycharm's debugger use the same tracing api (sys.settrace) - they don't work together. " -- https://github.com/pytest-dev/pytest-cov/issues/131

References:

How to debug py.test in PyCharm when coverage is enabled

Solution 2

The root issue is debugging does not work if you have coverage enabled by default (usually on pytest.ini).

To disable just on pycharm, just add --no-cov on the Additional Arguments on the Run/Debug Configurations.
Update Templates -> Python tests -> pytest, so every new test gets this configuration.
After this, delete your current debug settings and redebug it.

enter image description here

Pycharm 2018.3.x (still works in 2020.x)

Solution 3

TL;DR: Disable the "Gevent compatible" flag in the "Build, execution, Deployment" -> "Python Debugger".

It seems that at some point I enabled the "Gevent compatible" debugger in pycharm, and since then pytest-pycharm stopped working. Disabling it will make pytest-pycharm work again. I hope this will solve the issue for some of you.

Solution 4

I know that you had it right, but for me actually setting the Default test runner to pytest solved the problem. In PyCharm: Settings -> Python Integrated Tools -> Testing section -> Default test runner -> choose pytest from the dropdown menu -> Apply. And it instantly works.

Solution 5

I'd like to add to this conversation that these fixes does not seem to work in the case a single test function is launched in PyCharm (rather than the whole test file).

I yet haven't found a solution online to activate breakpoints when debugging a single test function instead of the whole file, and if someone has a solution, I would be interested. If I find it myself, I'll try to update this post.

Share:
25,079
davyria
Author by

davyria

Software Engineer. Eager to learn and to teach.

Updated on July 05, 2022

Comments

  • davyria
    davyria almost 2 years

    I cannot debug in PyCharm using py.test. All the test suite is running ok in "Debug mode" but it doesn't stop on breakpoints.

    Debug Mode

    I also have py.test as the default test runner.

    Maybe this is not important, but debugging works correctly in my Django server.

    Any ideas?

    Configuration picture of enable_breakpoints_and_the_mode_of_pycharm_is_debug

    References:

    pycharm-enabling-disabling-and-removing-breakpoints

    Run/Debug Configuration: py.test

  • madzohan
    madzohan over 6 years
    py.test test_dir --no-cov
  • silver est
    silver est over 6 years
    Where is the pytest.ini file?
  • Cloud
    Cloud over 6 years
    @silverest Create by yourself, you can see docs.pytest.org/en/latest/customize.html#finding-the-rootdir‌​.
  • saaj
    saaj about 6 years
    This also solves the same issue in PyDev (because both use pydevd). It's also worth looking around for pytest.ini or the like, which may impose coverage reporting.
  • Emer
    Emer over 5 years
    I have PyCharm 2018.2.1 and the option is already disabled by default
  • roskakori
    roskakori over 5 years
    Thanks, works for me. This preferable to deactivating coverage in the pytest.ini as suggested in the accepted answer. Now running the tests from the command line or during the CI builds still results in coverage reports.
  • Austen Hoogen
    Austen Hoogen about 5 years
    Problem solved. But being new to PyCharm I had a hell of a time finding out how to get to the Run/Debug Configurations (navigation bar was hidden, and I didn't know...) jetbrains.com/help/pycharm/…
  • n1000
    n1000 over 4 years
    I had to install pytest-cov to try this. However, it did not work for me.
  • Mark
    Mark over 4 years
    I believe I enabled this to try to resolve a warning or error in a previous version and upon upgrading hardly any breakpoints would work (only those in my main.py). Unchecking it restored expected operation.
  • Ursin Brunner
    Ursin Brunner about 4 years
    Thanks a lot Alessio, this solved my problem. And yes this flag is indeed disabled by default, but I for example needed to enable it to debug large deep learning models with PyTorch (stackoverflow.com/a/47801392/1081551). So it's quite critical to understand when to enable/disable it
  • chumbaloo
    chumbaloo almost 4 years
    @madzohan and joaoricardo000 answers I think is the best. As this can be applied in the debug configuration as an additional argument, without changing the pytest.ini file which is likely to be in version control.
  • Shani Shalgi
    Shani Shalgi over 3 years
    In Pycharm Pro 2020.2 it is in "Run Configuration Templates for New Projects" under "New Project Settings" in the File menu, or you can get to it when pressing "Add configuration" in the run menu.
  • Lost Crotchet
    Lost Crotchet over 2 years
    Usually when I get this problem, I add --no-cov in and it fixes it, but this time it doesn't. Anyone else have this?
  • Daniel Ben Zaken
    Daniel Ben Zaken over 2 years
    I added --no-cov to the pyproject.toml file in a kedro project and it solved my problem. Thanks!
  • mcsj120
    mcsj120 over 2 years
    I was having this same issue where a single text fixture wouldn't stop on breakpoints and used @mrbattle 's solution by deleting all of the pycache files using the command find . -name __pycache__ -type d -exec rm -rf {} \; (Mac) Afterwards, I was able to hit breakpoints.
  • Tonatio
    Tonatio about 2 years
    Running the command to remove __pycache__ recursively solved the problem.