how to pass variable argument to context.execute_steps in python-behave

非 Y 不嫁゛ 提交于 2019-12-01 12:58:08

问题


I want to execute certain step before executing my function. The step takes variable as an argument. I am not able to pass it in context.execute_steps.

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

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

however this does not work. I get error in argument parsing because variables are not in quotes. I don't see any such example in behave documentation. Any help would be really appreciated.


回答1:


There's a bunch of stuff that could be going on. I found here that if you're using python 3 you need to make it unicode by including a u in front of '''. I also was a bit tripped up that you have to include when, then or given as part of execute step command (not just the name of the step), but your example seems to be doing that. I'm kind of confused by what the way you're passing variables but can tell you this how you use execute_steps and works in python 2 & 3 with 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'

Then calling:

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

Would execute when I set the Number to "5" exactly as part of the when I call another step.

Hard to say for your exact example because I'm unfamiliar with the other step you're trying to execute but if you defined the previous step with something like:

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

you should the be able to use it like so in another step.

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

If your problem is just passing information between steps. You could just use context to pass it between them.

@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)

And then call the steps in a row

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


来源:https://stackoverflow.com/questions/46940856/how-to-pass-variable-argument-to-context-execute-steps-in-python-behave

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!