pytest提出了例外
我正在为此功能编写测试:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
==
在Python中可能只是比较对象的ID,这就是为什么相等性在这里失败的原因。也许您应该尝试类似的事情:
==
in python might just compare the objects's ids, which is why the equality fails here.Maybe you should try something like:
正如其他指出的那样,您无法使用
==
在异常实例的情况下使用。 pytest提供
risis中的关键字功能,如果要匹配异常的字符串表示形式,则可以使用该功能。match
As other pointed out, You can not use
==
sign with exception instances. Pytest providesmatch
keyword in the raises function, you can use that if you want to match the string representation of the exception.python异常不相等:
不仅是星条:
如果要进行字符串比较,则必须使用
repr
:但是最好只是比较预期的异常attrs(
pytest 。
Python exceptions do not compare equal:
it's not just Starlette:
If you want to do a string comparison you will have to use
repr
:But better would be to just compare the expected exception attrs (
pytest.raises
has already ensured it is of the expected type)您无法在此处直接进行比较,尝试将其与
servert
一起使用。这将起作用。You can not directly compare here, try to use it with
assert
. This will work.