如何编写支持多个单词的步骤实现 [英] Howto to write a step implementation that supports multiple words

查看:65
本文介绍了如何编写支持多个单词的步骤实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

小黄瓜示例

# Gherkin snip
When waiting for 30 seconds
# or
When waiting for 5 s

我想在一个步骤的定义中实现以上步骤.我尝试了以下步骤的实现.

I want to implement above steps in one step definition. I tried the following step implementation.

from behave import *
use_step_matcher("re")

@when(u"waiting for (?P<time>\d+) (s|seconds)")
def step_impl(context, time):    
  pass

运行此命令会导致此错误:

When running this results in this error:

TypeError:step_impl()为关键字参数时间"获得了多个值

TypeError: step_impl() got multiple values for keyword argument 'time'

行为将其提取为参数.

这样做有什么好习惯?

推荐答案

我遇到了类似的问题,经过多次尝试,我发现添加虚拟参数 _unused 可以解决此问题:

I got a similar issue and after many attempts I have found that adding a dummy parameter _unused solves the issue:

@when(u"waiting for (?P<time>\d+) (s|seconds)")
def step_impl(context, _unused, time):    
  pass

我类似的问题是关于 负超前断言 正则表达式:

My similar issue was about negative lookahead assertion regex:

@when(r'buy')
@when(r'buy (?P<home_owner>((?!.* car).)+) home')
@when(r'buy (?P<car_owner>((?!.* home).)+) car')
@when(r'buy (?P<home_owner>.+) home and (?P<car_owner>.+) car')
def buy(context, home_owner="default", car_owner="default"):
    pass

我通过拆分单个函数并添加参数 _unused :

I have fixed it by splitting my single function and adding parameter _unused:

@when(r'buy')
@when(r'buy (?P<home_owner>.+) home and (?P<car_owner>.+) car')
def buy1(context, home_owner="default", car_owner="default"):
    pass

@when(r'buy (?P<home_owner>((?!.* car).)+) home')
@when(r'buy (?P<car_owner>((?!.* home).)+) car')
def buy2(context, _unused, home_owner="default", car_owner="default"):
    buy1(context, home_owner, car_owner)

这篇关于如何编写支持多个单词的步骤实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆