【问题标题】:Nosetests AssertionError Output FormatNosetests AssertionError 输出格式
【发布时间】:2013-12-05 20:10:40
【问题描述】:

嗨,我想知道是否有办法修复鼻子测试断言失败的输出。我有一个名为“t.py”的简单脚本:

import unittest
from nose.tools import assert_equal

class x(unittest.TestCase):
    """
    Testing
    """
    def test(self):
        assert_equal(1, 2)

如果我使用命令“nosetests t”运行它,那么我会得到以下结果。

AssertionError: 1 != 2
    '1 != 2' = '%s != %s' % (safe_repr(1), safe_repr(2))
    '1 != 2' = self._formatMessage('1 != 2', '1 != 2')
>>  raise self.failureException('1 != 2')

而不是运行“python -m unittest t”时得到的输出

AssertionError: 1 != 2

有谁知道我如何使鼻子测试输出与单元测试输出匹配?当运行许多测试时,这会使结果混乱。

这是使用 python 2.7.3,nose==1.3.0,在 ubuntu VM 内的 virtualenv 中。

编辑:

我对 Nose 尝试提供更多信息没有意见。我有一个不断增长的测试套件,额外的信息可能会有所帮助。只是它提供了相当无用的信息。我真的不需要多次查看相同的信息。如果有多个测试,或者特别是如果有如下附加消息,则很难发现问题:

assert_equal(1,2, "Something is messed up before this assertion. Now here is a bunch of info to help with debugging. This is really quite a lot of info you know. Yes it is very long isn't it?")

然后我明白了:

assert_equal(1,2, "Something is messed up before this assertion. Now here is a bunch of info to help with debugging. This is really quite a lot of info you know. Yes it is very long isnt it?")
AssertionError: Something is messed up before this assertion. Now here is a bunch of info to help with debugging. This is really quite a lot of info you know. Yes it is very long isn't it?
    '1 != 2' = '%s != %s' % (safe_repr(1), safe_repr(2))
    "Something is messed up before this assertion. Now here is a bunch of info to help with debugging. This is really quite a lot of info you know. Yes it is very long isnt it?" = self._formatMessage("Something is messed up before this assertion. Now here is a bunch of info to help with debugging. This is really quite a lot of info you know. Yes it is very long isnt it?", '1 != 2')
>>  raise self.failureException("Something is messed up before this assertion. Now here is a bunch of info to help with debugging. This is really quite a lot of info you know. Yes it is very long isnt it?")

谢谢!

【问题讨论】:

    标签: python formatting assertions nosetests python-unittest


    【解决方案1】:

    您是否尝试过 self.assertEqual where self is "unittest.TestCase"?

    为此

    import unittest
    class x(unittest.TestCase):
      def test_1(self):
          self.assertEqual(1,2)
    

    我得到这个输出

    laptop:~/workspace/test$ nosetests j.py
    F
    ======================================================================
    FAIL: test_1 (j.x)
    ----------------------------------------------------------------------
    Traceback (most recent call last):
      File "/home/workspace/test/j.py", line 4, in test_1
        self.assertEqual(1,2)
    AssertionError: 1 != 2
    
    ----------------------------------------------------------------------
    Ran 1 test in 0.001s
    
    FAILED (failures=1)
    

    编辑: 您可能还喜欢 "nosetests -q, --quiet" 以减少冗长

    【讨论】:

    • 它对我不起作用。也许我有一些我看不到的奇怪的配置选项集。
    • 啊刚刚意识到有人在项目中留下了 setup.cfg 文件。它在其中指定了“--detailed-errors”标志。删除这个有帮助。现在我不确定是否接受这个答案哈哈它有点帮助
    【解决方案2】:

    nosetests 试图比默认的 python assert 更有帮助。面对默认断言错误消息时,您通常需要更多信息来追踪错误。在您必须找出几十个真正的断言错误之前,您可能不会完全理解这一点。

    您不必担心结果会混乱,因为您选择一个并修复它(更快,因为您有更多信息),通常情况是没有很多(或更好的零)

    【讨论】:

    • 我不同意。有很多测试,所以一次可能会有很多失败。他们是其他人的东西的测试。我的工作是以易于阅读的方式报告事情,以便其他开发人员快速跟踪问题。我总是有额外的消息来提供调试信息(我现在意识到应该在原始问题中。)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-04
    • 1970-01-01
    • 1970-01-01
    • 2017-03-25
    • 2015-04-22
    • 2018-04-17
    • 2011-12-06
    相关资源
    最近更新 更多