如何在 python unittest 输出中配置 diff window?

How to configure diff window in python unittest output?

我正在使用内置的 unittest 库 (python 3.5) 和一些测试用例比较(列表)字典来测试我的应用程序。当这些测试失败时,输出不是很有帮助:

First differing element 1:
{'emi[1557 chars]al': 509201.03, 'remaining_time': None, 'nomin[1213 chars]alse}
{'emi[1557 chars]al': '509,201.03', 'remaining_time': None, 'no[1218 chars]alse}

很容易看出哪个列表元素是错误的,但是不同字典条目的键名的有限差异 window 削减。我知道我可以用 self.maxDiff = None 显示完整的差异,但这不是我想要的。我喜欢有限的 diff window 我只是不喜欢它在 diff 中的位置。

我能以某种方式配置 diff window 的位置吗?或者,在比较字典时,如何通过 python 测试获得信息丰富的测试失败报告?

此功能是作为问题 #18996--and I can see why... trying to find the difference in a long sequence of characters is definitely not fun. Someone else complained about the lack of control over the shortening in the issue and issue #21820 的一部分添加的,因此已打开。

进行缩短的代码是 here。您也许可以在那里进行猴子修补,但我不建议您这样做。或者,您可以编写自己的方法或扩展 TestCase class 来比较字典列表并完全控制结果。