我正在使用PyHamcrest,但我認爲這個問題是語言不可知的。如何在Hamcrest中合併多個assert_that?
我想談談:
assert_that(actual0, matcher0)
assert_that(actual1, matcher1)
成一個單一的斷言,這樣如果兩個斷言失敗,錯誤消息是這麼說的。這可以在Hamcrest(或其他包裝/模塊)中完成嗎?
在Python中,我實現了:
def assert_all(*assertions):
assertion_failed = False
assertion_message = []
for (actual, matcher) in assertions:
try:
hamcrest.assert_that(actual, matcher)
except AssertionError as e:
assertion_failed = True
assertion_message.append(e.message)
if assertion_failed:
raise AssertionError(''.join(assertion_message))
所以我的測試會叫:
assert_all(
(actual0, matcher0),
(actual1, matcher1))
但我想知道是否已存在的這樣的一個實現。
如果兩個斷言均失敗,則僅報告第一個斷言。 –
正常情況下,實際值將通過一個方法作爲序列返回。我不明白爲什麼你想要在兩個單獨的值上結合斷言,比如你的例子,但是一個具體的例子可能會更好地說明。請記住,Hamcrest匹配器用於比嘲諷方法更多的斷言。 –
您可以請您打開該項目的門票嗎? –