Pycharm unit test interactive debug command line doesn't work
Asked Answered
U

4

44

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.

Umeh answered 23/7, 2013 at 9:16 Comment(0)
P
91

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

Phyllode answered 23/7, 2013 at 12:33 Comment(4)
(Run > Edit Configurations > Defaults > Python tests > py.test > add -s to the options field.)Brazil
(Run > Edit Configurations > Defaults > Python tests > py.test > add -s to the options field------>(Additional Arguments).) settings default run tests by pytest: (Preferences > Tools > Python Intergrated Tools > default test runner)Brazil
In addition to modifying the defaults, you need to modify the existing run configurations as well. Run > Edit Configurations > Python tests > "py.test in my_test_case.py" Add -s to the "Additional Arguments" field.Sinistrad
In PyCharm 2019.1 this becomes Run > Edit Configurations > Templates > Python tests > pytest to get to the appropriate dialog for the defaults.Ridgeling
F
4

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.

Fellatio answered 9/7, 2018 at 15:52 Comment(0)
M
3

For unittest you can add '--capture=no' to Pycharm Run/Debug configuration - this will show all output in realtime

Mat answered 6/3, 2015 at 16:27 Comment(0)
O
0

Unittests usually cannot be used in combination with debuggers, unless you explicitly delcare them as to be debugged. For this, use the decorator "@unittest.debug".

s. also https://docs.python.org/3/library/unittest.html#organizing-tests for more information on this topic.

Opportunity answered 3/8, 2020 at 16:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.