如果你仔细研究 PyCharm 源代码 https://github.com/JetBrains/intellij-community/blob/master/python/helpers/pycharm/teamcity/pytest_plugin.py#L259-L272,从整体上pytest
输出,PyCharm 使用单行来解析数据以显示在Click to see difference
对话。这是AssertionError: <message>
line:
def test_spam():
> assert v1 == v2
E AssertionError: assert {'foo': 'bar'} == {'foo': 'baz'}
E Differing items:
E {'foo': 'bar'} != {'foo': 'baz'}
E Use -v to get the full diff
如果您想查看完整的 diff 行而不被截断,您需要在输出中自定义此行。对于单个测试,可以通过向assert
陈述:
def test_eggs():
assert a == b, '{0} != {1}'.format(a, b)
如果您想将此行为应用于所有测试,请定义自定义pytest_assertrepr_compare https://docs.pytest.org/en/latest/reference.html#_pytest.hookspec.pytest_assertrepr_compare钩。在里面conftest.py
file:
# conftest.py
def pytest_assertrepr_compare(config, op, left, right):
if op in ('==', '!='):
return ['{0} {1} {2}'.format(left, op, right)]
现在,如果时间过长,值的相等比较仍将被删除;要显示完整的行,您仍然需要增加详细程度-vv
flag.
现在对中的值进行相等比较AssertionError
行不会被剥离,完整的差异显示在Click to see difference
对话框,突出显示差异部分: