如何获取PyCharm以显示pytest的整个错误差异? [英] How do I get PyCharm to show entire error diffs from pytest?

查看:90
本文介绍了如何获取PyCharm以显示pytest的整个错误差异?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用

这没用,因为它没有告诉我有什么不同.对于任何大于单个字符串或数字的差异,我都会得到这种行为.

我假设Pycharm和/或pytest试图消除大输出的差异的非信息性部分.但是,这里太激进了,没了一切.

如何获取Pycharm和/或pytest来显示全部差异?

我尝试将 -vvv 添加到pytest的其他参数中,但这没有效果.


自从原始帖子以来,我验证了从命令行运行单元测试时看到的行为是相同的.因此,这是pytest而不是Pycharm的问题.

看完到目前为止我给出的答案后,我想我真正要问的是在pytest中是否可以设置 maxDiff = None 而无需更改测试的源代码?"通过阅读有关pytest的印象,我可以理解 -vv 开关是控制此设置的原因,但事实并非如此.

解决方案

I am using Pycharm to run my pytest unit tests. I am testing a REST API, so I often have to validate blocks of JSON. When a test fails, I'll see something like this:

FAILED
test_document_api.py:0 (test_create_documents)
{'items': [{'i...ages': 1, ...} != {'items': [{'...ages': 1, ...}

Expected :{'items': [{'...ages': 1, ...}
Actual   :{'items': [{'i...ages': 1, ...}
 <Click to see difference>

When I click on the "Click to see difference" link, most of the difference is converted to points of ellipses, like so

This is useless since it doesn't show me what is different. I get this behavior for any difference larger than a single string or number.

I assume Pycharm and/or pytest tries to elide uninformative parts of differences for large outputs. However, it's being too aggressive here and eliding everything.

How do I get Pycharm and/or pytest to show me the entire difference?

I've tried adding -vvv to pytest's Additional Arguments, but that has no effect.


Since the original post I verified that I see the same behavior when I run unit tests from the command line. So this is an issue with pytest and not Pycharm.

After looking at the answers I've got so far I guess what I'm really asking is "in pytest is it possible to set maxDiff=None without changing the source code of your tests?" The impression I've gotten from reading about pytest is that the -vv switch is what controls this setting, but this does not appear to be the case.

解决方案

If you look closely into PyCharm sources, from the whole pytest output, PyCharm uses a single line the to parse the data for displaying in the Click to see difference dialog. This is the 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

If you want to see the full diff line without truncation, you need to customize this line in the output. For a single test, this can be done by adding a custom message to the assert statement:

def test_eggs():
    assert a == b, '{0} != {1}'.format(a, b)

If you want to apply this behaviour to all tests, define custom pytest_assertrepr_compare hook. In the conftest.py file:

# conftest.py
def pytest_assertrepr_compare(config, op, left, right):
    if op in ('==', '!='):
        return ['{0} {1} {2}'.format(left, op, right)]

The equality comparison of the values will now still be stripped when too long; to show the complete line, you still need to increase the verbosity with -vv flag.

Now the equality comparison of the values in the AssertionError line will not be stripped and the full diff is displayed in the Click to see difference dialog, highlighting the diff parts:

这篇关于如何获取PyCharm以显示pytest的整个错误差异?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆