使用Python的unittest
模块,断言
self.assertTrue(a > b - 0.5 && a < b + 0.5, "The two values did not agree")
在失败时输出以下内容:
AssertionError: False is not true : The two values did not agree
我不希望打印False is not true
。理想情况下,也不应打印AssertionError
。只应打印The two values did not agree
。
我可以这样做吗?
答案 0 :(得分:1)
你可以压制False is true
部分,但是,有一件事要记住,你是提出一个异常,而你所看到的是一个断言的标准输出用Python引发的。你想看到这个。此外,这可以直接在断言方法中调用,如下面的assertTrue
所示:
def assertTrue(self, expr, msg=None):
"""Check that the expression is true."""
if not expr:
msg = self._formatMessage(msg, "%s is not true" % safe_repr(expr))
raise self.failureException(msg)
压制“假”是真的&#39;部分,将longMessage类属性更改为False:
class TestCompare(unittest.TestCase):
longMessage = False
def test_thing(self):
self.assertTrue(5 == 6, "The two values did not agree")
输出:
Failure
Traceback (most recent call last):
File "/Users/XXX/dev/rough/test_this.py", line 21, in test_things
self.assertTrue(5 == 6, "The two values did not agree")
AssertionError: The two values did not agree