我正在嘗試支持以下功能場(chǎng)景: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)我收到錯(cuò)誤: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然后我嘗試使用可選參數(shù):我的功能文件: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)我收到此錯(cuò)誤: raise ValueError('format spec %r not recognised' % type)ValueError: format spec 'a_?' not recognised我認(rèn)為我真的不需要一個(gè)可選參數(shù),只要我可以消除第一個(gè)示例中的步驟的歧義即可。
1 回答

湖上湖
TA貢獻(xiàn)2003條經(jīng)驗(yàn) 獲得超2個(gè)贊
在使用 Behave 時(shí),我注意到在步驟定義文件中定義步驟的順序很重要。
嘗試首先從最具體的步驟開(kāi)始定義您的步驟:
@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)
運(yùn)行你的 test.feature 不會(huì)再給我?guī)?lái)歧義錯(cuò)誤。
添加回答
舉報(bào)
0/150
提交
取消