Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pycharm unit test interactive debug command line doesn't work

When debugging unit tests (through the pycharm test runner), one can turn on the interactive command line, but (unlike when debugging regular scripts) the commands entered don't produce any output. As a matter of fact, it appears that stdout is being captured somewhere, because stderr works as expected:

>>> print "a" >>> import sys >>> sys.stderr.write("moof") moof >>> sys.stdout.write("moof") >>> sys.stderr.write("test") test 

Is this expected behavior? I really like the interactive debug console and it would be awesome if it behaved nice when debugging unit tests as well.

like image 811
SleepingPills Avatar asked Jul 23 '13 09:07

SleepingPills


People also ask

How do I debug a test in PyCharm?

Debug failed testsOpen the test file in the editor. Right-click it and select the Debug <test name>. PyCharm stops on every failed test and shows the reason for the failure. Inspect the Variables pane of the Debugger tool window to get more details about the problems.

How do I run unit test in PyCharm?

PyCharm makes it easy to select just one test to run. In fact, there are several ways to do it: With the cursor anywhere in the test you want to focus on, right-click and choose to run that in the test runner. Right-click on the test in the test tool listing and choose to run it.


2 Answers

This is likely because your test runner is capturing stdout but not stderr.

I use py.test which captures both stdout and stderr so I see no output at all. If I want to see output I have to pass the -s flag to my py.test runner which can be done by modifying the run/debug configuration and adding this flag to the options field. (Run > Edit Configurations > Defaults > Python tests > py.test > add -s to the "additional arguments" field.)

>>> print 'a' a >>> import sys >>> sys.stderr.write('moof') moof >>> sys.stdout.write('moof') moof >>> sys.stderr.write('test') test 

Note: the -s flag can equally be used with nose tests

like image 101
Inti Avatar answered Sep 24 '22 02:09

Inti


If you don't want to change run/debug configuration every time you run tests, you can set JB_DISABLE_BUFFERING environment variable as "-s". It is useful if you use ^+SHIFT+D shortcut to debug tests of current file in editor.

like image 40
SilentGuy Avatar answered Sep 23 '22 02:09

SilentGuy