Python 如何在鼻子中设置 self.maxDiff 以获得完整的差异输出?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14493670/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set self.maxDiff in nose to get full diff output?
提问by Lo?c Séguin-C.
When using nose 1.2.1 with Python 3.3.0, I sometimes get an error message similar to the following one
在 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)
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.
在许多情况下,要弄清楚错误究竟是什么,我需要查看完整的差异输出。但是,我不知道如何设置它self.maxDiff。谷歌搜索鼻子和 maxDiff 没有帮助。在 Python 2.7.1 上使用相同版本的鼻子,完整的差异被打印到屏幕上。
Here is a simple script that generates the error above when run with nosetests-3.3:
这是一个简单的脚本,运行时会生成上述错误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)
采纳答案by vicvicvic
I had the same problem in Python 3 (from reading the other answers here) and using im_classdid not work. The snippet below works in Python 3 (cf. How to find instance of a bound method in Python?):
我在 Python 3 中遇到了同样的问题(从这里阅读其他答案)并且使用im_class不起作用。下面的代码段适用于 Python 3(参见如何在 Python中查找绑定方法的实例?):
assert_equal.__self__.maxDiff = None
As @Louis commented, the convenience functions arebound methods on a Dummyinstance. They all seem to be on the same instance, so changing this for e.g. assert_equalwill change it for assert_dict_equalet cetera. From the Python docs, __self__is available from Python 2.6 and forward.
正如@Louis 所评论的,便利函数是Dummy实例上的绑定方法。他们似乎都在同一个实例上,所以改变这个例如assert_equal会改变它assert_dict_equal等等。来自Python 文档,__self__可从 Python 2.6 及更高版本获得。
回答by Lennart Regebro
You set maxDiffto None.
你设置maxDiff为None.
But you will have to actually use a unittest.TestCasefor your tests for that to work. This should work.
但是您必须实际使用 aunittest.TestCase进行测试才能使其工作。这应该有效。
class MyTest(unittest.TestCase):
maxDiff = None
def test_diff(self):
<your test here>
回答by Memke
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
回答by quodlibetor
In python 2.7.3, nose 1.3.0, doing the following is working for me:
在 python 2.7.3,nose 1.3.0 中,执行以下操作对我有用:
assert_equal.im_class.maxDiff = None
assert_equal(huge_thing, other_huge_thing)
回答by Dobes Vandermeer
This works in python 2.7:
这适用于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.
它将为所有 TestCase 实例设置默认的 maxDiff,包括 assert_equals 和朋友所附加的实例。
回答by Tim Seed
I recently ran into this problem .... I was forcing a MS type line ending ....
我最近遇到了这个问题......我正在强迫一个 MS 类型的行结束......
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 !!
在 PyCharm 中它说文件是相同的!!
I removed the \r - no more crash and tests now pass.
我删除了 \r - 现在不再崩溃并且测试通过了。
Hope that saves someone the 2 hours it cost me.
希望能节省我花费的 2 个小时。

