【问题标题】:nosetests is capturing the output of my print statements. How to circumvent this?nosetests 正在捕获我的打印语句的输出。如何规避这一点?
【发布时间】:2011-05-12 08:16:51
【问题描述】:

当我输入时

$ 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了,一定有更好的方法。请赐教。

【问题讨论】:

  • 知道如何以编程方式进行吗?

标签: python nosetests


【解决方案1】:

要么:

$ nosetests --nocapture mytest.py

或者:

$ NOSE_NOCAPTURE=1 nosetests mytests.py

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

【讨论】:

  • 感谢您的有用回答。我还发现知道我可以将这个参数传递给 nose.main() 很有帮助,如帖子中所述:stackoverflow.com/questions/7070501/…
  • 如果有人想查看源代码:nose.readthedocs.org/en/latest/plugins/capture.html
  • 这个命令的简短版本是nosetests -s。有关其他标准选项,请参阅 -h 帮助或 basic usage 帮助页面。
  • python3.5 -m "nose" --nocapture
  • 对我不起作用,即使使用此选项,我的打印语句在测试通过时也不会打印
【解决方案2】:

使用

--nologcapture 

它对我有用

【讨论】:

    【解决方案3】:

    这是最近添加到鼻子而不是--nocapture 这样做:

    nosetests -s
    

    【讨论】:

    • 这并没有提供问题的答案。要批评或要求作者澄清,请在其帖子下方发表评论。
    • @BhargavRao "do this nosetests -s" 回答了这个问题(尽管几乎没有考虑语法)。我不知道你为什么反对。
    • 注意-s--nocapture标志的单字母缩写per the documentation
    【解决方案4】:

    为了与http://travis-ci.org整合,我把这个放到.travis.yml中:

    script:  "python setup.py nosetests -s"
    

    其中 setup.py 包含:

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

    【讨论】:

      【解决方案5】:

      试试这个,

      nosetests -v 2 -s yourtest
      

      标志需要顺序。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-12-09
        • 1970-01-01
        • 2014-08-18
        • 2023-03-06
        • 2023-03-23
        • 2011-04-23
        • 1970-01-01
        相关资源
        最近更新 更多