gpt4 book ai didi

python - Django 断言响应包含可能的字符串列表之一

转载 作者:太空宇宙 更新时间:2023-11-03 13:40:53 25 4
gpt4 key购买 nike

我正在使用内置测试工具为我的 Django 应用程序编写测试。现在我正在尝试为显示用户关注者列表的页面编写测试。当用户没有关注者时,页面会显示一条从字符串列表中随机挑选的消息。例如:

NO_FOLLOWERS_MESSAGES = [
"You don't have any followers.",
"Sargent Dan, you ain't got no followers!"
]

所以现在我想编写一个测试来断言响应包含其中一个字符串。如果我只使用一个字符串,我可以只使用 self.assertContains(request, "You don't have any followers.") 但我一直在研究如何编写具有多个可能的测试结果。任何帮助将不胜感激。

最佳答案

试试这个:

if not any([x in response.content for x in NO_FOLLOWERS_MESSAGES]):
raise AssertionError("Did not match any of the messages in the request")

关于 any():https://docs.python.org/2/library/functions.html#any

关于python - Django 断言响应包含可能的字符串列表之一,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31505779/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com