nosetests捕捉了我的打印语句的输出。如何规避这个问题?

151

当我打字时

$ nosetests -v mytest.py

当所有测试通过时,我的所有打印输出都被捕获了。 我想要在一切都通过的情况下看到打印输出。

所以我做的是强制引发一个断言错误,以查看输出,就像这样。

class MyTest(TestCase):

    def setUp(self):
        self.debug = False

    def test_0(self):
        a = .... # construct an instance of something
        # ... some tests statements
        print a.dump()
        if self.debug:
            eq_(0,1)

感觉很hackish,一定有更好的方法。请启发我。


有没有想过如何通过编程实现它? - Yauhen Yakimovich
5个回答

229

两种可能:

$ nosetests --nocapture mytest.py

或者:

$ NOSE_NOCAPTURE=1 nosetests mytests.py

(也可以在nose.cfg文件中指定,参见nosetests --help


3
感谢您提供有用的答案。我还发现了一篇文章,介绍了如何将这个参数传递给nose.main():https://dev59.com/K2w05IYBdhLWcg3wuUEL - David Hall
2
如果有人想查看源代码,请访问以下网址:http://nose.readthedocs.org/en/latest/plugins/capture.html - Ceasar
12
这个命令的简短版本是 nosetests -s。如果需要其他标准选项,请查看 -h 帮助或者 basic usage 帮助页面。 - dbn
python3.5 -m "nose" --nocapture - Alex Punnen
2
即使使用了这个选项,我的打印语句在测试通过时仍然无法输出。 - John Smith Optional

17

使用

--nologcapture 

它对我起作用了。


10

最近在nose中加入了以下内容,代替使用--nocapture:

nosetests -s

这并没有提供问题的答案。如果您想对作者进行批评或请求澄清,请在他们的帖子下留言。 - Bhargav Rao
7
“执行这个命令‘nosetests -s’”就能回答这个问题(尽管语法有点问题)。我不确定你为什么反对。” - abcd
2
请注意,-s--nocapture标志的单字母缩写,根据文件中的说明。 - joelostblom

3
为了与http://travis-ci.org集成,在.travis.yml中加入以下内容:
script:  "python setup.py nosetests -s"

其中setup.py包含以下内容:

setup(
    ...
    tests_require=['nose>=1.0'],
    test_suite='nose.collector',
)

1
尝试这个,
nosetests -v 2 -s yourtest

标志需要顺序。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接