36

When running tests with py.test there is a --pdb option to enter pdb on failure.

Is there a similar way to enter the debugger when running the same test from within PyCharm?

Johan Lübcke
  • 19,666
  • 8
  • 38
  • 35
  • Possible duplicate of [Debugging pytest post mortem exceptions in pycharm/pydev](http://stackoverflow.com/questions/14086067/debugging-pytest-post-mortem-exceptions-in-pycharm-pydev) – Piotr Dobrogost Oct 02 '15 at 08:42

2 Answers2

23

There is a py.test plugin, pytest-pycharm, that will halt the PyCharm debugger when a test emits an uncaught exception.

Johan Lübcke
  • 19,666
  • 8
  • 38
  • 35
  • Great! Thanks a lot! Can you give a couple of words on how it is working?.. By just being installed in Python and without any modifications to projects? – Mikhail Apr 20 '15 at 19:44
  • It is enough to have it installed so that py.test finds it as a plugin an use the callback when an exception is encountered. So there is no need to modify any project apart from having it installed in the python runtime. – Johan Lübcke May 07 '15 at 07:41
  • 2
    Is there a way to stop on breakpoints (set in tests from within PyCharm)? – Piotr Dobrogost Oct 02 '15 at 08:35
  • This doesn't seem to work when using `pyenv`, as it uses shell-script shims to wrap python programs such as `pytest`, and his means that PyCharm is not directly running a Python program, so cannot engage the debugger. – davidA Jan 11 '17 at 21:15
  • Doesn't work for me, at least not when I simply select 'Debug [mytest]'. – karu Jun 15 '18 at 08:51
0

Follow the steps below to setup the correct run configuration:

  1. Run > Edit Configurations...
  2. Click the '+' button to add a new configuration.
  3. Name the configuration and specify the configuration parameters below:

Script: The path to your py.test executable (find by typing which py.test)

Script Parameters: This parameter is your test case followed by the --pdb option (ie /Users/Johan/projects/misc/testing.py --pdb)

After setting up the configuration, you can run the test case from within PyCharm. If a test case fails, your PyCharm run window will show the pdb prompt.

enter image description here

garnertb
  • 9,454
  • 36
  • 38
  • 7
    Maybe I was not clear enough in my question, but I was hoping for a way to enter the _PyCharm_ debugger on failure. But this answer is still better than nothing. – Johan Lübcke Jun 19 '14 at 10:37
  • In PyCharm 2016.2, I see "entering PDB" in the Run window, and I can type text (echoes in green), but it does not seem to be an active pdb prompt. – davidA Nov 22 '16 at 23:54