在具有参数集的步骤中调用步骤

时间:2019-11-26 17:43:47

标签: python-behave

我知道我可以使用

context.execute_steps('given I have logged in')

语法以在步骤

中运行步骤

如果步骤中包含可变参数,则可以在步骤

中对其进行硬编码
@when('the user selects {value}')

成为

context.execute_steps('when the user selects blue')

但是,有一种方法可以保留变量并将其从正在运行的步骤中传入,例如

@when(the user does a thing and then selects {value})
def step_impl(context, value)
    context.execute_steps('given the user does a thing')
    context.execute_steps('when the user selects {value}')

2 个答案:

答案 0 :(得分:0)

是的,您可以简单地使用python字符串格式。

@when(the user does a thing and then selects {value})
def step_impl(context, value)
    context.execute_steps('given the user does a thing')
    context.execute_steps('when the user selects {0}'.format(value))

或使用旧式python字符串格式

@when(the user does a thing and then selects {value})
def step_impl(context, value)
    context.execute_steps('given the user does a thing')
    context.execute_steps('when the user selects %s'%value)

答案 1 :(得分:0)

或根据文档。下面的示例带有f'strings。

@when(the user does a thing and then selects {value}) 
def step_impl(context, value):
    context.execute_steps(
        f'''
        given the user does a thing
        when the user selects "{value}"
        '''
    )