在 python 中打印断言的成功消息 [英] print success messages for asserts in python

查看:66
本文介绍了在 python 中打印断言的成功消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 python 中使用断言.每次断言失败时,我都会收到失败消息,我会把它放在那里打印.我想知道是否有办法在断言条件通过时打印自定义成功消息?

I am using assert in python. Every time an assert fails I get the failure message which I would have put there to be printed. I was wondering if there is a way to print a custom success message when the assert condition passes?

我正在使用 py.test 框架.

I am using py.test framework.

例如:

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

对于上述断言,如果断言失败,我会收到消息IO 停止失败",但如果断言通过,我希望IO 停止成功".像这样:

for the above assert I get message "IO stop failed" if assert fails but I am looking to have "IO stop succeeded" if assert passes. something like this:

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

推荐答案

是的,最简单的肯定是在 assert 下方放置一个打印:

Yes, the simplest is surely to place a print below the assert:

assert self.clnt.stop_io()==1, "IO stop failed"
print("IO stop passed at location ==1")

这篇关于在 python 中打印断言的成功消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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