我正在使用 PyHamcrest,但我认为这个问题与语言无关。
我想转向:
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))
但我想知道是否已经有类似的实现。
最佳答案
将实际
值的值序列(列表或元组)和相同数量的匹配器作为参数传递给 contains
:
assert_that((actual0, actual1, ...), contains(matcher0, matcher1, ...))
匹配器列表按照实际值序列一一应用。所有内容都必须匹配才能通过断言。
关于python - 如何在Hamcrest中组合多个assert_that?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10485731/