How to set self.maxDiff in nose to get full diff o

2019-01-23 00:44发布

问题:

When using nose 1.2.1 with Python 3.3.0, I sometimes get an error message similar to the following one

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

In many situations, to figure out what the error really is, I need to see the full diff output. However, I have no idea of how to set that self.maxDiff. Googling for nose and maxDiff does not help. With the same version of nose on Python 2.7.1 the full diff is printed to screen.

Here is a simple script that generates the error above when run with 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)

回答1:

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.



回答2:

You set maxDiff to None.

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

class MyTest(unittest.TestCase):

    maxDiff = None

    def test_diff(self):
          <your test here>


回答3:

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.



回答4:

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


回答5:

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)


标签: python nose