如何将变量参数传递给python-behave

时间:2017-10-25 19:43:08

标签: python-behave

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

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

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

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

1 个答案:

答案 0 :(得分:2)

可能会发生一些事情。我发现here如果您正在使用python 3,则需要通过在u前加'''来使其成为unicode。我也有点绊倒你必须包括when,then或作为执行步骤命令的一部分(不仅仅是步骤的名称),但你的例子似乎正在这样做。我对你传递变量的方式感到困惑,但可以告诉你如何使用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'

然后致电:

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
  ...