在Python 3.3.0中使用nose 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上使用相同版本的nose,将完整差异打印到屏幕上。
这是一个简单的脚本,在使用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)
答案 0 :(得分:74)
您将maxDiff
设置为None
。
但是你必须实际使用unittest.TestCase
进行测试才能发挥作用。这应该有用。
class MyTest(unittest.TestCase):
maxDiff = None
def test_diff(self):
<your test here>
答案 1 :(得分:25)
这适用于python 2.7:
from unittest import TestCase
TestCase.maxDiff = None
它将为所有TestCase实例设置默认的maxDiff,包括assert_equals和friends附加到的实例。
答案 2 :(得分:19)
我在Python 3中遇到了同样的问题(从这里阅读其他答案)并且使用im_class
不起作用。下面的代码片段适用于Python 3(参见How to find instance of a bound method in Python?):
assert_equal.__self__.maxDiff = None
正如@Louis评论的那样,方便函数是<{1}}实例上的绑定方法。它们似乎都在同一个实例上,因此将其更改为例如Dummy
会更改assert_equal
等等。从Python docs开始,assert_dict_equal
可从Python 2.6获得并转发。
答案 3 :(得分: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
答案 4 :(得分:2)
在python 2.7.3中,鼻子1.3.0,执行以下操作对我有用:
assert_equal.im_class.maxDiff = None
assert_equal(huge_thing, other_huge_thing)
答案 5 :(得分:1)
对于使用基本 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()
答案 6 :(得分:0)
我最近遇到了这个问题....我强迫MS类型行以....
结尾MSstr="""hi\r
there\r"""
expected="""hi
there"""
self.assertEqual(MSsrt, expected)
由于其他正在使用的可怕错误而崩溃。
在PyCharm中说文件是相同的!
我删除了\ r-不再崩溃,并且测试现在通过。
希望可以为某人节省2个小时的费用。