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.
https://i.stack.imgur.com/gPHyL.png
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?
https://i.stack.imgur.com/vP9ou.png
References:
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
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.
https://i.stack.imgur.com/DmgRr.png
Pycharm 2018.3.x
(still works in 2020.x)
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.
pytest-cov
to try this. However, it did not work for me.
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.
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.
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:
find . -name __pycache__ -type d -exec rm -rf {} \; [linux, mac] (see How to remove folders with a certain name)
FOR /d /r . %d IN ("__pycache__") DO @IF EXIST "%d" rd /s /q "%d" [win] (see Batch command to delete all subfolders with a specific name)
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!
__pycache__
recursively solved the problem.
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.
find . -name __pycache__ -type d -exec rm -rf {} \;
(Mac) Afterwards, I was able to hit breakpoints.
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.
Success story sharing
py.test test_dir --no-cov
pytest.ini
or the like, which may impose coverage reporting.