为了账号安全,请及时绑定邮箱和手机立即绑定

行为步骤消歧/可选步骤参数

行为步骤消歧/可选步骤参数

茅侃侃 2024-01-15 21:17:53
我正在尝试支持以下功能场景:Feature: test  Scenario: Test optional      Given attacked by samurai        And attacked by samurai from behind我的步骤文件是from behave import step@step('attacked by {opponent}')def step_attacked_by(context, opponent):    print(opponent)@step('attacked by {opponent} from {direction}')def step_attacked_by(context, opponent, direction):    print(opponent)    print(direction)我收到错误:behave.step_registry.AmbiguousStep: @step('attacked by {opponent} from {direction}') has already been defined in  existing step @step('attacked by {opponent}') at steps/test.py:5然后我尝试使用可选参数:我的功能文件:Feature: test  Scenario: Test optional      Given attacked by a samurai我的步骤文件:import parsefrom behave import step, register_type@parse.with_pattern(r"a\s+")def parse_word_a(text):    """Type converter for "a " (followed by one/more spaces)."""    return text.strip()register_type(a_=parse_word_a)@step('attacked by {:a_?}{opponent}')def step_attacked_by(context, a_, opponent):    print(a_)    print(opponent)我收到此错误:    raise ValueError('format spec %r not recognised' % type)ValueError: format spec 'a_?' not recognised我认为我真的不需要一个可选参数,只要我可以消除第一个示例中的步骤的歧义即可。
查看完整描述

1 回答

?
湖上湖

TA贡献2003条经验 获得超2个赞

在使用 Behave 时,我注意到在步骤定义文件中定义步骤的顺序很重要。


尝试首先从最具体的步骤开始定义您的步骤:


@step('attacked by {opponent} from {direction}')

def step_attacked_by(context, opponent, direction):

    print(opponent)

    print(direction)


@step('attacked by {opponent}')

def step_attacked_by(context, opponent):

    print(opponent)

运行你的 test.feature 不会再给我带来歧义错误。


查看完整回答
反对 回复 2024-01-15
  • 1 回答
  • 0 关注
  • 29 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信