nosetests 捕获了我的 print 输出,如何绕过?

151 投票
5 回答
45705 浏览
提问于 2025-04-16 17:30

当我输入

$ 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)

这样做感觉有点像是绕过了正常流程,肯定还有更好的方法。请教教我吧。

5 个回答

10

最近在nose中添加了这个功能,取代了原来的--nocapture,现在可以这样做:

nosetests -s
17

使用

--nologcapture 

对我来说有效

229

可以选择以下两种方式:

$ nosetests --nocapture mytest.py

或者:

$ NOSE_NOCAPTURE=1 nosetests mytests.py

(也可以在 nose.cfg 文件中指定,具体可以查看 nosetests --help

撰写回答