在python中为断言打印成功消息 [英] print success messages for asserts in python
本文介绍了在python中为断言打印成功消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在python中使用assert.每次断言失败时,我都会收到要打印的失败消息.我想知道断言条件通过时是否可以打印自定义成功消息?
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屋!
查看全文