Luc*_*llo 4 python python-behave
我正在使用Python来做我的测试.在步骤文件中,我想获取当前步骤名称,因为如果测试失败,我会截取屏幕截图并将文件重命名为步骤名称.
像这样的东西:
给定用户登录
当用户做某事时
然后发生了一些事情
我希望我的步骤代码如下:
@given('user is logged in')
try:
# MY CODE HERE
except:
# GET THE STEP NAME HERE
Run Code Online (Sandbox Code Playgroud)
任何人都知道如何做到这一点?
我基本上完成了你要做的事情,通过after_step在我的environment.py文件中设置一个钩子来截取失败测试的屏幕截图.
def after_step(context, step):
if step.status == "failed":
take_the_shot(context.scenario.name + " " + step.name)
Run Code Online (Sandbox Code Playgroud)
如果某个步骤可以出现在多个场景中,那么您很可能也想要场景名称.
如果以上内容对您不起作用,遗憾的是,行为不会像当前场景或当前功能那样为当前步骤设置step字段.你可以有一个钩子来执行这样的赋值,然后在步骤本身,你可以访问步骤的名称.就像是:contextscenariofeaturebefore_stepcontext.step.name
def before_step(context, step):
context.step = step
Run Code Online (Sandbox Code Playgroud)
步骤实施:
def step_impl(context):
if some_condition:
take_the_shot(context.scenario.name + " " + context.step.name)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2354 次 |
| 最近记录: |