我想明确地使该步骤失败behave
当我遇到异常时
例如。我正在根据行为文档编写代码 -
from behave import *
@when('verify test fails.*?(?P<param_dict>.*)')
def test_logger(context, param_dict):
try:
logger.info("testing the logger. this is info message")
logger.info(1/0)
except Exception as e:
logger.error("arrived at exception: "+str(e))
fail("failed with exception: "+str(e))
但它会抛出这个错误:
NameError:名称“失败”未定义
我也尝试过其他方法,但没有效果
例如。context.failed = True
(也没有用)
如果我不尝试显式失败,即使最终测试结果进入异常块,最终测试结果也会变为“通过”……这很奇怪。
context.failed
只是 Behave 设置的一个属性,并不按原样执行任何操作。它是一个信息属性,虽然您可以使用它来确定失败情况并引发断言错误,但它本身不会执行任何操作。看上下文失败
至于fail
你提到的方法,它可能来自unittest
模块,如图所示here。该模块也用于 Behave 的开发测试(请参阅他们的 Github)。不过我同意,这应该在他们的文档中得到澄清。
要修复您的错误,您需要导入unittest
模块。要明确使该步骤失败,您只需在记录消息后引发异常,如下所示:
except Exception as e:
logger.error("arrived at exception: "+str(e))
fail("failed with exception: "+str(e))
raise
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)