如何将变量参数传递到python行为中的context.executesteps



我想在执行函数之前执行某个步骤。步骤将变量作为参数。我无法在context.execute_steps.中传递它

eg.
call1 = "version"
call1_method = "get"
context.execute_steps('''When execute api{"method":call1_method, "call":call1}''')

然而,这并不奏效。我在参数解析中遇到错误,因为变量不在引号中。我在行为文档中没有看到任何这样的例子。如有任何帮助,我们将不胜感激。

有很多事情可能会发生。我在这里发现,如果你使用python 3,你需要在'''前面包含一个u,使其成为unicode。我也有点绊倒了,你必须包括when,then或作为执行步骤命令的一部分给出(不仅仅是步骤的名称),但你的例子似乎就是这样做的。我有点困惑于你传递变量的方式,但可以告诉你如何使用execute_step并在python 2&3与行为1.2.5。

@step('I set the Number to "{number:d}" exactly')
def step_impl(context, number):
context.number = 10
context.number = number
@step('I call another step')
def step_impl(context):
context.execute_steps(u'''when I set the Number to "5" exactly''')
print(context.number)
assert context.number == 10, 'This will fail'

然后呼叫:

Given I call another step
When some not mentioned step
Then some other not mentioned step

将在I set the Number to "5" exactly时执行,作为在I call another step时执行的一部分。

很难说你的确切例子,因为我不熟悉你试图执行的另一个步骤,但如果你用类似的东西定义了前一个步骤:

@step('execute api "{method}" "{version}"')
def step_impl(context, method, version):
# do stuff with passed in method and version variables

你应该能够在另一个步骤中这样使用它。

@step('I call another step')
def step_impl(context):
context.execute_steps(u'''When execute api "get" "version1"''')

如果您的问题只是在步骤之间传递信息。您可以使用上下文在它们之间传递它。

@step('I do the first function')
def step_impl(context):
context.call1 = "version"
context.call1_method = "get"
@step('I call another step')
def step_impl(context):
print(%s and %s are available in this function % (context.call1, context.call1_method)

然后调用一行中的步骤

When I do the first function
And I call another step
...

最新更新