在Behave框架(Python)中断言失败时,如何继续执行“ for”循环?

时间:2020-07-14 12:29:23

标签: python-3.x python-behave

@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循环中的下一个迭代?每当断言失败发生时,都应该在控制台中看到它,并且下一次迭代应该开始。

1 个答案:

答案 0 :(得分:0)

欢迎堆叠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