如何在python-behave中将变量参数传递给context.execute_steps

pyt*_*ser 4 python-behave

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

eg.
call1 = "version"
call1_method = "get"

context.execute_steps('''When execute api{"method":call1_method, "call":call1}''')
Run Code Online (Sandbox Code Playgroud)

但是这不起作用。我在参数解析中出错,因为变量不在引号中。我在行为文档中没有看到任何这样的例子。任何帮助将非常感激。

Cyn*_*nic 6

有很多事情可能会发生。我在这里发现,如果您使用的是 python 3,则需要通过u'''. 我也有点困惑,您必须包含何时,然后或作为执行步骤命令的一部分给出(不仅仅是步骤的名称),但您的示例似乎正在这样做。我对您传递变量的方式感到困惑,但可以告诉您如何使用 execute_steps 并在 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'
Run Code Online (Sandbox Code Playgroud)

然后调用:

Given I call another step
When some not mentioned step
Then some other not mentioned step
Run Code Online (Sandbox Code Playgroud)

I set the Number to "5" exactly作为when 的一部分执行I call another step

对于您的确切示例,很难说,因为我不熟悉您尝试执行的另一步骤,但是如果您使用以下内容定义了上一步:

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

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

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

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

@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)
Run Code Online (Sandbox Code Playgroud)

然后连续调用步骤

When I do the first function
  And I call another step
  ...
Run Code Online (Sandbox Code Playgroud)