Pycharm 单元测试交互式调试命令行不起作用 [英] Pycharm unit test interactive debug command line doesn't work
问题描述
在调试单元测试时(通过 pycharm 测试运行器),可以打开交互式命令行,但是(与调试常规脚本时不同)输入的命令不会产生任何输出.事实上,似乎 stdout 正在某处被捕获,因为 stderr 按预期工作:
<预><代码>>>>打印一个">>>导入系统>>>sys.stderr.write("moof")莫夫>>>sys.stdout.write("moof")>>>sys.stderr.write("测试")测试这是预期的行为吗?我真的很喜欢交互式调试控制台,如果它在调试单元测试时也表现得很好,那就太棒了.
这可能是因为您的测试运行程序正在捕获 stdout 而不是 stderr.
我使用 py.test 其中 同时捕获标准输出和标准错误,所以我根本看不到任何输出.如果我想查看输出,我必须将 -s
标志传递给我的 py.test 运行器,这可以通过修改运行/调试配置并将此标志添加到选项字段来完成.(运行 > 编辑配置 > 默认值 > Python 测试 > py.test > 将 -s
添加到附加参数"字段.)
注意:-s
标志同样可以与 鼻子测试
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.
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
这篇关于Pycharm 单元测试交互式调试命令行不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!