89

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

davyria
  • 1,343
  • 2
  • 13
  • 23

9 Answers9

151

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

Neuron
  • 5,141
  • 5
  • 38
  • 59
Cloud
  • 2,859
  • 2
  • 20
  • 23
  • 13
    `py.test test_dir --no-cov` – madzohan Oct 31 '17 at 16:09
  • 6
    Where is the pytest.ini file? – silver est Jan 02 '18 at 11:46
  • 1
    @silverest Create by yourself, you can see https://docs.pytest.org/en/latest/customize.html#finding-the-rootdir. – Cloud Jan 03 '18 at 03:52
  • 1
    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. – saaj May 09 '18 at 15:12
  • @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. – chumbaloo Jul 10 '20 at 05:49
  • 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? – Lost Crotchet Nov 15 '21 at 18:23
76

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)

joaoricardo000
  • 4,764
  • 4
  • 25
  • 36
  • 2
    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. – roskakori Mar 06 '19 at 13:17
  • 3
    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...) https://www.jetbrains.com/help/pycharm/creating-and-editing-run-debug-configurations.html – Austen Hoogen May 31 '19 at 07:39
  • 3
    I had to [install `pytest-cov` to try this](https://stackoverflow.com/questions/34870962/how-to-debug-py-test-in-pycharm-when-coverage-is-enabled#comment97436285_52295919). However, it did not work for me. – n1000 Nov 18 '19 at 13:55
  • 1
    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. – Shani Shalgi Oct 01 '20 at 09:59
  • 1
    I added --no-cov to the pyproject.toml file in a kedro project and it solved my problem. Thanks! – Daniel Ben Zaken Jan 13 '22 at 09:35
12

None of the solutions mentioned here or elsewhere worked for me.

I have multiple copies of the same project in different folders. Some copies were fine, others exhibited the following behavior:

  • debugger would ignore any breakpoints in tests (regardless if these were run as single functions, classes, modules, batches)

  • breakpoints set in actual code called by these tests were hit

What did work for me (4 broken repos and counting):

Delete contents of __pycache__ folders in your project, starting out from the directory where your undebuggable test module is located. Remove the .pyc file with the name of the undebuggable module first. If this doesn't help, identify any own imports used in the test, locate and delete the __pycache__/.pyc for them as well. (Note: use a file browsing tool like File Explorer because Pycharm doesn't show these directories in the project view.)

If you're still out of luck or just plain lazy (like me), wipe all the __pycache__ folders you have in your repo. You can do this via your command line/terminal, just navigate yourself to the repo folder and then:

EDIT: Made the description a bit detailed after recent findings, added a quick wipe hint. Noticed afterwards that @mcsj120 already suggested this in Antonin's post, so kudos to him!

mrbattle
  • 121
  • 1
  • 5
8

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.

Alessio
  • 341
  • 2
  • 5
  • 7
  • 3
    I have PyCharm 2018.2.1 and the option is already disabled by default – Emer Oct 02 '18 at 09:40
  • 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. – Mark Mar 15 '20 at 21:32
  • 1
    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 (https://stackoverflow.com/a/47801392/1081551). So it's quite critical to understand when to enable/disable it – Ursin Brunner Jun 18 '20 at 13:28
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.

Or b
  • 666
  • 1
  • 5
  • 22
3

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.

Antonin G.
  • 374
  • 2
  • 8
  • 2
    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. – mcsj120 Feb 04 '22 at 17:37
2

For me the solution was to just create empty pytest.ini file in the test folder. After doing this I can also launch debug for specific functions.

eXTure
  • 21
  • 2
1

I have tried it all of the above and nothing worked.

Realized I was using "Module name" target instead of "Script path" target which was shown in the screenshot of one of the answers above.

I changed to Script path and specified target - this is important since the tests worked without it but breakpoints didn't.

Also for some reason existing breakpoints didn't work, but new ones did (so I just refreshed existing ones and they worked as intended)

Use Script path

wakafa
  • 31
  • 5
0

For me it worked when I installed the module pytest-cov. Before Pycharm was not breaking on break-points.

pip install pytest-cov
datenhahn
  • 61
  • 1
  • 2