87

在 Python 3.3.0 中使用鼻子 1.2.1 时,有时会收到类似于以下的错误消息

======================================================================
FAIL: maxdiff2.test_equal
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/local/lib/python3.3/site-packages/nose/case.py", line 198, in runTest
    self.test(*self.arg)
  File "/Users/loic/cmrsj/Calculus_II/scrap/maxdiff2.py", line 32, in test_equal
    assert_equal(str1, str2)
AssertionError: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec a
diam lectus.\n [truncated]... != 'Suspendisse lectus leo, consectetur in tempor sit
amet, placerat quis neque.\nE [truncated]...
Diff is 1780 characters long. Set self.maxDiff to None to see it.

----------------------------------------------------------------------
Ran 1 test in 0.064s

FAILED (failures=1)

在许多情况下,要弄清楚错误到底是什么,我需要查看完整的 diff 输出。但是,我不知道如何设置它self.maxDiff。谷歌搜索鼻子和 maxDiff 没有帮助。在 Python 2.7.1 上使用相同版本的鼻子,完整的差异会打印到屏幕上。

这是一个简单的脚本,在运行时会生成上述错误nosetests-3.3

from nose.tools import assert_equal

def test_equal():
    str1 = """\
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec a diam lectus.
Sed sit amet ipsum mauris. Maecenas congue ligula ac quam viverra nec
consectetur ante hendrerit. Donec et mollis dolor. Praesent et diam eget libero
egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem
lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean ut gravida
lorem. Ut turpis felis, pulvinar a semper sed, adipiscing id dolor.
Pellentesque auctor nisi id magna consequat sagittis. Curabitur dapibus enim
sit amet elit pharetra tincidunt feugiat nisl imperdiet. Ut convallis libero in
urna ultrices accumsan. Donec sed odio eros. Donec viverra mi quis quam
pulvinar at malesuada arcu rhoncus. Cum sociis natoque penatibus et magnis dis
parturient montes, nascetur ridiculus mus. In rutrum accumsan ultricies. Mauris
vitae nisi at sem facilisis semper ac in est."""

    str2 = """\
Suspendisse lectus leo, consectetur in tempor sit amet, placerat quis neque.
Etiam luctus porttitor lorem, sed suscipit est rutrum non. Curabitur lobortis
nisl a enim congue semper. Aenean commodo ultrices imperdiet. Vestibulum ut
justo vel sapien venenatis tincidunt. Phasellus eget dolor sit amet ipsum
dapibus condimentum vitae quis lectus. Aliquam ut massa in turpis dapibus
convallis. Praesent elit lacus, vestibulum at malesuada et, ornare et est. Ut
augue nunc, sodales ut euismod non, adipiscing vitae orci. Mauris ut placerat
justo. Mauris in ultricies enim. Quisque nec est eleifend nulla ultrices
egestas quis ut quam. Donec sollicitudin lectus a mauris pulvinar id aliquam
urna cursus. Cras quis ligula sem, vel elementum mi. Phasellus non ullamcorper
urna."""

    assert_equal(str1, str2)
4

7 回答 7

122

你设置maxDiffNone.

但是您必须实际使用 aunittest.TestCase进行测试才能使其正常工作。这应该有效。

class MyTest(unittest.TestCase):

    maxDiff = None

    def test_diff(self):
          <your test here>
于 2013-01-24T04:18:57.340 回答
34

这适用于 python 2.7:

    from unittest import TestCase
    TestCase.maxDiff = None

它将为所有 TestCase 实例设置默认的 maxDiff,包括 assert_equals 和朋友附加到的那个。

于 2014-05-12T20:07:44.547 回答
28

我在 Python 3 中遇到了同样的问题(通过阅读此处的其他答案)并且使用im_class不起作用。下面的代码片段适用于 Python 3(参见如何在 Python 中查找绑定方法的实例?):

assert_equal.__self__.maxDiff = None

正如@Louis 评论的那样,便利函数Dummy实例上的绑定方法。他们似乎都在同一个实例上,所以改变这个例如assert_equal会改变它assert_dict_equal等等。从Python 文档__self__可从 Python 2.6 及更高版本获得。

于 2014-02-06T22:44:43.237 回答
4

在这里你有它(谷歌告诉我的):

# http://pdf2djvu.googlecode.com/hg/tests/common.py
try:
    from nose.tools import assert_multi_line_equal
except ImportError:
    assert_multi_line_equal = assert_equal
else:
    assert_multi_line_equal.im_class.maxDiff = None
于 2013-02-18T20:35:59.410 回答
2

在 python 2.7.3,nose 1.3.0 中,执行以下操作对我有用:

assert_equal.im_class.maxDiff = None
assert_equal(huge_thing, other_huge_thing)
于 2013-09-12T21:49:56.867 回答
2

对于使用基本unittest库的人。maxDiff到达该领域的方式几乎相同:

import unittest

class Test_Something(unittest.TestCase):  
    def test_first_test(self):
        huge_custom_object = CustomObject()
        expected_object = CustomObject(customVars)

        self.assertEqual.__self__.maxDiff = None
        self.assertEqual(huge_custom_object , expected_object )

if __name__ == '__main__':
    unittest.main()
于 2020-12-30T07:37:08.940 回答
-4

我最近遇到了这个问题....我强制使用 MS 类型的行结尾....

MSstr="""hi\r
there\r"""
expected="""hi
there"""

self.assertEqual(MSsrt, expected) 

这与其他人正在使用的可怕错误一起崩溃。

在 PyCharm 中,它说文件是相同的!

我删除了 \r - 没有更多的崩溃和测试现在通过了。

希望这可以节省我花费的 2 个小时。

于 2020-03-23T08:57:47.557 回答