如何在鼻子中设置 self. maxDiff 以获得完全差异输出?

当在 Python 3.3.0中使用 nose1.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 没有帮助。在 Python2.7.1上,使用相同版本的 nose,将把完整的 diff 打印到屏幕上。

下面是一个简单的脚本,它在使用 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)
46738 次浏览

You set maxDiff to None.

But you will have to actually use a unittest.TestCase for your tests for that to work. This should work.

class MyTest(unittest.TestCase):


maxDiff = None


def test_diff(self):
<your test here>

Here you have it (what google told me):

# 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

In python 2.7.3, nose 1.3.0, doing the following is working for me:

assert_equal.im_class.maxDiff = None
assert_equal(huge_thing, other_huge_thing)

I had the same problem in Python 3 (from reading the other answers here) and using im_class did not work. The snippet below works in Python 3 (cf. How to find instance of a bound method in Python?):

assert_equal.__self__.maxDiff = None

As @Louis commented, the convenience functions are bound methods on a Dummy instance. They all seem to be on the same instance, so changing this for e.g. assert_equal will change it for assert_dict_equal et cetera. From the Python docs, __self__ is available from Python 2.6 and forward.

This works in python 2.7:

    from unittest import TestCase
TestCase.maxDiff = None

It'll set the default maxDiff for all TestCase instances, including the one that assert_equals and friends are attached to.

I recently ran into this problem .... I was forcing a MS type line ending ....

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


self.assertEqual(MSsrt, expected)

That crashed with the horrible errors other are using.

In PyCharm it said the files were identical !!

I removed the \r - no more crash and tests now pass.

Hope that saves someone the 2 hours it cost me.

For someone using the basic unittest lib. Pretty much the same how you can reach that maxDiff field:

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