在 python 中打印断言的成功消息

2023-11-22

我在 python 中使用断言。每次断言失败时,我都会收到失败消息,我会将其打印出来。我想知道是否有办法在断言条件通过时打印自定义成功消息?

我正在使用 py.test 框架。

例如:

assert self.clnt.stop_io()==1, "IO stop failed"

对于上述断言,如果断言失败,我会收到消息“IO 停止失败”,但如果断言通过,我希望得到“IO 停止成功”。像这样的东西:

 assert self.clnt.stop_io()==1, "IO stop failed", "IO stop succeeded"

是的,最简单的肯定是在断言下面放置一个打印:

assert self.clnt.stop_io()==1, "IO stop failed"
print("IO stop passed at location ==1")
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 python 中打印断言的成功消息 的相关文章

随机推荐