pytest使用可变的内省声明消息自定义 [英] pytest assert message customization with variable introspection

查看:168
本文介绍了pytest使用可变的内省声明消息自定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

pytest文档中它表示您可以在assert失败时自定义输出消息.我想在测试返回错误状态代码的REST API方法时自定义assert消息:

def test_api_call(self, client):
    response = client.get(reverse('api:my_api_call'))
    assert response.status_code == 200

所以我试图在conftest.py

中放置一段这样的代码

def pytest_assertrepr_compare(op, left, right):
    if isinstance(left, rest_framework.response.Response):
        return left.json()

但是问题是leftresponse.status_code的实际值,因此它是int而不是Response.但是,默认的输出消息会抛出类似的内容:

E assert 400 == 201 E +其中400 = .status_code

说错误400来自对象Response的属性status_code.

我的观点是,对要评估的变量有一种自省.因此,如何以一种舒适的方式自定义断言错误消息,以获得与上面发布的示例类似的输出?

解决方案

您可以使用Python内置功能来显示自定义异常消息:

assert response.status_code == 200, "My custom msg: actual status code {}".format(response.status_code)

或者您可以构建一个辅助断言函数:

def assert_status(response, status=200):  # you can assert other status codes too
    assert response.status_code == status, \
        "Expected {} actual status {}. Response text {}".format(status, response.status_code, response.text)

# here is how you'd use it
def test_api_call(self, client):
    response = client.get(reverse('api:my_api_call'))
    assert_status(response)

还结帐: https://wiki.python.org/moin/UsingAssertionsEffectively

In the pytest documentation it says that you can customize the output message when an assert fails. I want to customize the assert message when testing a REST API method it returns an invalid status code:

def test_api_call(self, client):
    response = client.get(reverse('api:my_api_call'))
    assert response.status_code == 200

So I tried to put a piece of code like this in conftest.py

def pytest_assertrepr_compare(op, left, right):
    if isinstance(left, rest_framework.response.Response):
        return left.json()

But the problem is left is the actual value of response.status_code so it's an int instead of a Response. However the default output messages throws something like:

E assert 400 == 201 E + where 400 = .status_code

Saying that the error 400 comes from an attribute status_code of a object Response.

My point is that there is a kind of introspection of the variable being evaluated. So, how can I customize the assert error message in a comfortable way to get a similar output to example posted above?

解决方案

you could use Python built-in capability to show custom exception message:

assert response.status_code == 200, "My custom msg: actual status code {}".format(response.status_code)

Or you can built a helper assert functions:

def assert_status(response, status=200):  # you can assert other status codes too
    assert response.status_code == status, \
        "Expected {} actual status {}. Response text {}".format(status, response.status_code, response.text)

# here is how you'd use it
def test_api_call(self, client):
    response = client.get(reverse('api:my_api_call'))
    assert_status(response)

also checkout: https://wiki.python.org/moin/UsingAssertionsEffectively

这篇关于pytest使用可变的内省声明消息自定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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