Nosetests AssertionError输出格式

时间:2013-12-05 20:10:41

标签: python formatting assertions nosetests python-unittest

嗨所以我想知道是否有办法来修复nosetests断言失败的输出。我有一个名为“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

有谁知道如何让nosetests输出与unittest输出相匹配?当运行很多测试时,这会使结果混乱。

这是在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?")

谢谢!

2 个答案:

答案 0 :(得分:1)

您是否尝试过self.assertEqual,其中self是“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”以获得更简洁的

答案 1 :(得分:1)

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

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