首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在python-behave中将变量参数传递给context.execute_steps

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

Stack Overflow用户
提问于 2017-10-26 03:43:08
回答 1查看 5K关注 0票数 4

在执行我的函数之前,我想执行某些步骤。该步骤将变量作为参数。我不能在context.execute_steps中通过它。

代码语言:javascript
复制
eg.
call1 = "version"
call1_method = "get"

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

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

EN

回答 1

Stack Overflow用户

发布于 2017-11-02 06:29:15

可能会发生很多事情。我发现here如果你使用的是Python3,你需要通过在'''前面包含一个u来使它成为unicode。我还有点犯了一个错误,你必须在执行步骤命令中包括when,then或给定(不仅仅是步骤的名称),但你的例子似乎就是这样做的。我对你传递变量的方式有点困惑,但我可以告诉你你是如何使用execute_steps的,以及如何在Python2和3中使用behave 1.2.5的。

代码语言:javascript
复制
@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'

然后呼叫:

代码语言:javascript
复制
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

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

代码语言:javascript
复制
@step('execute api "{method}" "{version}"')
def step_impl(context, method, version):
    # do stuff with passed in method and version variables

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

代码语言:javascript
复制
@step('I call another step')
def step_impl(context):
    context.execute_steps(u'''When execute api "get" "version1"''')

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

代码语言:javascript
复制
@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)

然后调用一行中的步骤

代码语言:javascript
复制
When I do the first function
  And I call another step
  ...
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46940856

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档