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

2024-06-01 05:44:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图支持以下功能场景:

Feature: test
  Scenario: Test optional
      Given attacked by samurai
        And attacked by samurai from behind

我的steps文件是

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

然后我尝试使用optinal arguments

我的功能文件:

Feature: test
  Scenario: Test optional
      Given attacked by a samurai

我的步骤文件:

import parse

from 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

我不认为我真的需要一个可选的参数,只要我能消除第一个例子中步骤的歧义


Tags: 文件fromtestimportbyparsedefstep
1条回答
网友
1楼 · 发布于 2024-06-01 05:44:25

在使用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不会再给我模糊性错误

相关问题 更多 >