为了账号安全,请及时绑定邮箱和手机立即绑定

Python:为什么 unittest assertEqual 在这些列表上抛出错误而不是失败?

Python:为什么 unittest assertEqual 在这些列表上抛出错误而不是失败?

互换的青春 2021-06-07 14:26:59
Python 2.7 和 3.4:为什么test_unexpected_error测试会抛出错误而不是失败?import unittestclass TestLists(unittest.TestCase):    def test_unexpected_error(self):        self.assertEqual([0] * 1000 + [1], [1] + [0] * 1000)    def test_fails_as_expected(self):        self.assertEqual([0] * 1000 + [1], [0] * 1000 + [0])if __name__ == '__main__':    unittest.main()错误是:RuntimeError: maximum recursion depth exceeded while calling a Python object回溯(删节 - 递归错误是递归错误是递归错误是......)Traceback (most recent call last):  File "sotest.py", line 7, in test_unexpected_error    self.assertEquals([0] * 1000 + [1], [1] + [0] * 1000)  File "/usr/lib/python2.7/unittest/case.py", line 513, in assertEqual    assertion_func(first, second, msg=msg)  File "/usr/lib/python2.7/unittest/case.py", line 743, in assertListEqual    self.assertSequenceEqual(list1, list2, msg, seq_type=list)  File "/usr/lib/python2.7/unittest/case.py", line 722, in assertSequenceEqual    pprint.pformat(seq2).splitlines()))  File "/usr/lib/python2.7/difflib.py", line 920, in compare    for line in g:  File "/usr/lib/python2.7/difflib.py", line 1038, in _fancy_replace    for line in self._fancy_helper(a, best_i+1, ahi, b, best_j+1, bhi):  File "/usr/lib/python2.7/difflib.py", line 1051, in _fancy_helper    for line in g:  File "/usr/lib/python2.7/difflib.py", line 1038, in _fancy_replace    for line in self._fancy_helper(a, best_i+1, ahi, b, best_j+1, bhi):  File "/usr/lib/python2.7/difflib.py", line 1051, in _fancy_helper    for line in g:  File "/usr/lib/python2.7/difflib.py", line 1038, in _fancy_replace    for line in self._fancy_helper(a, best_i+1, ahi, b, best_j+1, bhi):  File "/usr/lib/python2.7/difflib.py", line 1051, in _fancy_helper    for line in g:
查看完整描述

1 回答

?
慕的地6264312

TA贡献1817条经验 获得超6个赞

这看起来是一个已知的错误

如果您不希望单元测试在失败时显示列表之间的差异,您可以执行以下替代测试:

self.assertTrue([0] * 1000 + [1] == [1] + [0] * 1000)


查看完整回答
反对 回复 2021-06-09
  • 1 回答
  • 0 关注
  • 104 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信