简体   繁体   English

尝试使用Nose捕获详细的stdout输出

[英]Trying to capture verbose stdout output with Nose

After running into trouble trying to set the python unittest discover pathway for my project, I'm trying again with nose. 在尝试为我的项目设置python unittest发现路径遇到麻烦之后,我再次尝试使用鼻子。 I am trying to print out verbose output which is supposedly captured by default by Nose (according to http://nose.readthedocs.org/en/latest/plugins/capture.html ) 我正在尝试打印出详细的输出,该输出应该是Nose默认捕获的(根据http://nose.readthedocs.org/en/latest/plugins/capture.html

I have: 我有:

arg =sys.argv[:1]
arg.append('--verbosity=2')
out = nose.run(module=ft1.test_y1, argv=arg)

but 'out' is a boolean 但是'out'是一个布尔值

How can I get it working? 我该如何运作?

Your best bet would be to disable the plugin and capture standard output with some other means, for example as described here : 你最好的选择是描述禁用插件并捕获了一些其他方式的标准输出,例如这里

import sys
import nose
from cStringIO import StringIO    

def basic_test():
    print "hello"

if __name__=="__main__":
    module_name = sys.modules[__name__].__file__

    old_stdout = sys.stdout
    sys.stdout = mystdout = StringIO()
    result = nose.run(argv=[sys.argv[0],
                            module_name,
                            '-s'])
    sys.stdout = old_stdout
    print mystdout.getvalue()

When running it like this you will get: 这样运行时,您将获得:

.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
hello

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM