pytest提出了例外

发布于 2025-02-11 07:52:55 字数 1251 浏览 2 评论 0原文

我正在为此功能编写测试:

def get_current_active_user(current_user: models.User = Depends(get_current_user)) -> models.User:
    if not current_user.is_active:
        raise HTTPException(status_code=400, detail="Inactive user")
    return current_user

我正在尝试检查httpexception的参数是否正确。这是我的考验:

def test_get_current_active_user_not_active():
    user: models.User = UserFactory()
    user.is_active = False

    with pytest.raises(HTTPException) as exc_info:
        get_current_active_user(current_user=user)
    
    assert exc_info.value == HTTPException(status_code=400, detail='Inactive user')

由于某种原因,尽管值是相同的,但这种断言永远不会正确。调试会议:

>exc_info.value
HTTPException(status_code=400, detail='Inactive user')

>type(exc_info.value)
<class 'fastapi.exceptions.HTTPException'>

>type(HTTPException(status_code=400, detail='Inactive user'))
<class 'fastapi.exceptions.HTTPException'>

>exc_info.value == HTTPException(status_code=400, detail='Inactive user')
False

>str(exc_info.value) == "HTTPException(status_code=400, detail='Inactive user')"
False

非常感谢为什么发生这种情况的任何想法。

I am writing test for this function:

def get_current_active_user(current_user: models.User = Depends(get_current_user)) -> models.User:
    if not current_user.is_active:
        raise HTTPException(status_code=400, detail="Inactive user")
    return current_user

I am trying to check if the arguments of HTTPException are right. That is my test:

def test_get_current_active_user_not_active():
    user: models.User = UserFactory()
    user.is_active = False

    with pytest.raises(HTTPException) as exc_info:
        get_current_active_user(current_user=user)
    
    assert exc_info.value == HTTPException(status_code=400, detail='Inactive user')

For some reason, this assert is never true, although the values are the same. Debugging session:

>exc_info.value
HTTPException(status_code=400, detail='Inactive user')

>type(exc_info.value)
<class 'fastapi.exceptions.HTTPException'>

>type(HTTPException(status_code=400, detail='Inactive user'))
<class 'fastapi.exceptions.HTTPException'>

>exc_info.value == HTTPException(status_code=400, detail='Inactive user')
False

>str(exc_info.value) == "HTTPException(status_code=400, detail='Inactive user')"
False

Any ideas why this is happening are greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

静若繁花 2025-02-18 07:52:55

==在Python中可能只是比较对象的ID,这就是为什么相等性在这里失败的原因。

也许您应该尝试类似的事情:

assert exc_info.value.status_code == 400
assert exc_info.detail == 'Inactive user'

== in python might just compare the objects's ids, which is why the equality fails here.

Maybe you should try something like:

assert exc_info.value.status_code == 400
assert exc_info.detail == 'Inactive user'
没有心的人 2025-02-18 07:52:55

正如其他指出的那样,您无法使用==在异常实例的情况下使用。 pytest提供 match risis中的关键字功能,如果要匹配异常的字符串表示形式,则可以使用该功能。

with pytest.raises(HTTPException, match=r"HTTPException(status_code=400, detail='Inactive user')") as exc_info:
    get_current_active_user(current_user=user)

As other pointed out, You can not use == sign with exception instances. Pytest provides match keyword in the raises function, you can use that if you want to match the string representation of the exception.

with pytest.raises(HTTPException, match=r"HTTPException(status_code=400, detail='Inactive user')") as exc_info:
    get_current_active_user(current_user=user)
鲜肉鲜肉永远不皱 2025-02-18 07:52:55

python异常不相等:

In [8]: from starlette.exceptions import HTTPException

In [9]: e1 = HTTPException(status_code=400, detail="Inactive user")

In [10]: e2 = HTTPException(status_code=400, detail="Inactive user")

In [11]: e1 == e2
Out[11]: False

不仅是星条:

In [12]: e3 = ValueError("wtf")

In [13]: e4 = ValueError("wtf")

In [14]: e3 == e4
Out[14]: False

如果要进行字符串比较,则必须使用repr

In [15]: str(e1)
Out[15]: ''

In [16]: repr(e1)
Out[16]: "HTTPException(status_code=400, detail='Inactive user')"

但是最好只是比较预期的异常attrs(pytest 。

assert exc_info.value.status_code == 400
assert exc_info.detail == 'Inactive user'

Python exceptions do not compare equal:

In [8]: from starlette.exceptions import HTTPException

In [9]: e1 = HTTPException(status_code=400, detail="Inactive user")

In [10]: e2 = HTTPException(status_code=400, detail="Inactive user")

In [11]: e1 == e2
Out[11]: False

it's not just Starlette:

In [12]: e3 = ValueError("wtf")

In [13]: e4 = ValueError("wtf")

In [14]: e3 == e4
Out[14]: False

If you want to do a string comparison you will have to use repr:

In [15]: str(e1)
Out[15]: ''

In [16]: repr(e1)
Out[16]: "HTTPException(status_code=400, detail='Inactive user')"

But better would be to just compare the expected exception attrs (pytest.raises has already ensured it is of the expected type)

assert exc_info.value.status_code == 400
assert exc_info.detail == 'Inactive user'
↙温凉少女 2025-02-18 07:52:55

您无法在此处直接进行比较,尝试将其与servert一起使用。这将起作用。


assert exc_info.value.status_code == 400
assert exc_info.detail == "Inactive user"

You can not directly compare here, try to use it with assert. This will work.


assert exc_info.value.status_code == 400
assert exc_info.detail == "Inactive user"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文