如何在 Behave 框架(Python)断言失败时继续执行 "for" 循环?
How to continue execution of "for" loop when assertion fails in Behave framework (Python)?
@then("----------------------------------")
def step_impl(context, vnf):
some_list=[] // list of values
is_log_exists = False
for log in context:
for key, val in log.items():
is_log_exists = all(elem in val for elem in some_list) // checking if all the element in some_list is present in val which is also a list.
assert is_log_exists, f"Failed"
参考上面一段代码。一旦断言失败而不是停止执行,有没有其他方法可以继续for循环中的下一次迭代?每当发生断言失败时,都应该在控制台中看到它,并且应该开始下一次迭代。
欢迎来到 Stack Overflow Madhuvanthi。
我认为您应该单独跟踪失败,然后创建自定义断言消息以向用户显示错误:
@then("----------------------------------")
def step_impl(context, vnf):
some_list=[] # list of values
failures = list()
for log in context:
for key, val in log.items():
for elem in some_list():
if val not in elem:
failures.append((val, elem))
# Create an assertion message detailing why the assertion failed
assert_msg = "The following values were not found in the corresponding element:\n"
for val, elem in failures:
assert_msg += "Value: {}, Element: {}\n".format(val, elem)
# This form of the assertion allows you to pass in a custom message that will be
# displayed only if the assertion is False
assert len(failures) == 0, assert_msg
@then("----------------------------------")
def step_impl(context, vnf):
some_list=[] // list of values
is_log_exists = False
for log in context:
for key, val in log.items():
is_log_exists = all(elem in val for elem in some_list) // checking if all the element in some_list is present in val which is also a list.
assert is_log_exists, f"Failed"
参考上面一段代码。一旦断言失败而不是停止执行,有没有其他方法可以继续for循环中的下一次迭代?每当发生断言失败时,都应该在控制台中看到它,并且应该开始下一次迭代。
欢迎来到 Stack Overflow Madhuvanthi。
我认为您应该单独跟踪失败,然后创建自定义断言消息以向用户显示错误:
@then("----------------------------------")
def step_impl(context, vnf):
some_list=[] # list of values
failures = list()
for log in context:
for key, val in log.items():
for elem in some_list():
if val not in elem:
failures.append((val, elem))
# Create an assertion message detailing why the assertion failed
assert_msg = "The following values were not found in the corresponding element:\n"
for val, elem in failures:
assert_msg += "Value: {}, Element: {}\n".format(val, elem)
# This form of the assertion allows you to pass in a custom message that will be
# displayed only if the assertion is False
assert len(failures) == 0, assert_msg