我正在嘗試支持以下功能場景: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, [email protected]_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 不會再給我帶來歧義錯誤。
添加回答
舉報
0/150
提交
取消