python-如何在Django单元测试中输出完整的差异?
作者:互联网
当我在REPL中将assertEqual()
与两个字典一起使用时,它向我显示了一个差异,例如:
>>> import unittest
>>> class A(unittest.TestCase):
... pass
...
>>> a = A()
>>> d1 = dict(zip(range(10), range(1000000, 1000010)))
>>> d2 = dict(zip(range(3, 13), range(1000003, 1000013)))
>>> a.assertEqual(d1, d2)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.5/unittest/case.py", line 820, in assertEqual
assertion_func(first, second, msg=msg)
File "/usr/lib/python3.5/unittest/case.py", line 1111, in assertDictEqual
self.fail(self._formatMessage(msg, standardMsg))
File "/usr/lib/python3.5/unittest/case.py", line 665, in fail
raise self.failureException(msg)
AssertionError: {0: 1000000, 1: 1000001, 2: 1000002, 3: 10[73 chars]0009} != {3: 1000003, 4: 1000004, 5: 1000005, 6: 10[76 chars]0012}
- {0: 1000000,
- 1: 1000001,
- 2: 1000002,
- 3: 1000003,
? ^
+ {3: 1000003,
? ^
4: 1000004,
5: 1000005,
6: 1000006,
7: 1000007,
8: 1000008,
- 9: 1000009}
? ^
+ 9: 1000009,
? ^
+ 10: 1000010,
+ 11: 1000011,
+ 12: 1000012}
当我在Django单元测试中执行相同操作时,有时会打印diff,有时仅打印第一行.我想知道如何使它始终打印差异.
我使用./manage.py test -v 3运行Django测试.
解决方法:
如果将maxDiff
设置为None,则将显示完整差异.
class A(unittest.TestCase):
maxDiff = None
...
标签:python-unittest,unit-testing,python,django 来源: https://codeday.me/bug/20191027/1941406.html