在执行我的函数之前,我想执行某些步骤。该步骤将变量作为参数。我不能在context.execute_steps中通过它。
eg.
call1 = "version"
call1_method = "get"
context.execute_steps('''When execute api{"method":call1_method, "call":call1}''')然而,这并不起作用。我在参数解析中遇到错误,因为变量没有用引号括起来。我在behave文档中没有看到任何这样的例子。任何帮助都将不胜感激。
发布于 2017-11-02 06:29:15
可能会发生很多事情。我发现here如果你使用的是Python3,你需要通过在'''前面包含一个u来使它成为unicode。我还有点犯了一个错误,你必须在执行步骤命令中包括when,then或给定(不仅仅是步骤的名称),但你的例子似乎就是这样做的。我对你传递变量的方式有点困惑,但我可以告诉你你是如何使用execute_steps的,以及如何在Python2和3中使用behave 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将作为when I call another step的一部分执行when I set the Number to "5" exactly。
很难说您的确切示例,因为我不熟悉您正在尝试执行的另一个步骤,但如果您使用以下内容定义了上一步:
@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
...https://stackoverflow.com/questions/46940856
复制相似问题