【问题标题】:Pytest-Django: Uncaught Exception is unfortunately hiddenPytest-Django:不幸的是,未捕获的异常被隐藏了
【发布时间】:2021-02-18 21:09:13
【问题描述】:

我使用 pytest-django,不幸的是,未捕获的异常被隐藏了

@pytest.mark.django_db
def test_foo(client):
    url = reverse('foo')
    response = client.get(url)
    assert response.status_code == 200

我明白了:

E       assert 500 == 200
Assertion failed

如何配置 pytest 以显示真正的异常(而不是 http 500 响应)?

【问题讨论】:

    标签: pytest pytest-django


    【解决方案1】:

    由于pytest 使用assert 语句进行测试,我们可以利用assert 语句的扩展形式,如here 所示。

    在你的情况下,它看起来像:

    @pytest.mark.django_db
    def test_foo(client):
        url = reverse('foo')
        response = client.get(url)
        assert response.status_code == 200, response.exception_reason
    

    在您的情况下检索Exception 的语法可能略有不同,但希望您了解需要更改的内容。

    【讨论】:

    • 我猜“exception_reason”是一个字符串。我想看看原始未捕获异常的整个漂亮堆栈跟踪。
    • 如果您想要比扩展断言提供的更多信息,那么您可能有兴趣使用这个hook
    猜你喜欢
    • 1970-01-01
    • 2020-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-25
    • 1970-01-01
    • 2018-10-10
    • 2016-05-09
    相关资源
    最近更新 更多