【问题标题】:How to get the current behave step with Python?如何使用 Python 获取当前的行为步骤?
【发布时间】:2015-07-21 14:21:30
【问题描述】:

我正在使用 Python 的行为来进行测试。在步骤文件中,我想获取当前步骤名称,因为如果测试失败,我会截图并将文件重命名为步骤名称。

类似这样的:

假设用户已登录
用户做了某事
然后某事发生

我希望我的步骤代码是这样的:

@given('user is logged in')  
try:  
   # MY CODE HERE  
except:  
   # GET THE STEP NAME HERE

有人知道怎么做吗?

【问题讨论】:

    标签: python python-behave


    【解决方案1】:

    通过在我的environment.py 文件中设置一个after_step 挂钩,我基本上已经完成了您想要做的事情,截取失败测试的屏幕截图。

    def after_step(context, step):
        if step.status == "failed":
            take_the_shot(context.scenario.name + " " + step.name)
    

    如果一个步骤可以出现在多个场景中,您很可能也希望在其中包含场景名称。

    如果上述方法对您不起作用,很遗憾,behaviour 不会在当前步骤中为 context 设置 step 字段,就像在当前场景中为 scenario 或在当前功能中为 feature 所做的那样。您可以有一个执行此类分配的before_step 钩子,然后在步骤本身中,您将访问步骤的名称为context.step.name。比如:

    def before_step(context, step):
        context.step = step
    

    以及步骤实现:

    def step_impl(context):
        if some_condition:
            take_the_shot(context.scenario.name + " " + context.step.name)
    

    【讨论】:

    • take_the_shot 是包含场景名称作为参数的方法吗?
    猜你喜欢
    • 2015-04-09
    • 1970-01-01
    • 1970-01-01
    • 2014-12-30
    • 1970-01-01
    • 2016-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多