4 回答

TA貢獻1895條經驗 獲得超3個贊
歧義問題來自另一個方向,并且不能總是通過使用雙引號來解決。歧義可能仍然存在。
正確的解決方案是對參數使用類型模式,否則將使用所有格解析器并盡可能多地消耗——為假定的重復項留出空間。

TA貢獻1811條經驗 獲得超4個贊
作為一種解決方案,如果您想保留這些名稱,您可以這樣寫:
driverlogon.py
@when(u'enter the "{driver_id}"')
def step_enter_the_driver_id(context,driver_id):
SelectDriver.input_driver(driver_id)
triplogon.py
@when(u'enter the configured block number')
def step_enter_the_configured_block_number(context):
ByBlock.block_data(context.block)
在這種情況下不會引發異常,但 driver_id 將作為字符串傳遞,步驟將如下所示:
When enter the "10"
但是,如果您希望它被解析為 int 而不是您可以d在這種情況下使用預定義的數據類型,如下所示:
@when(u'enter the "{driver_id:d}"')
def step_enter_the_driver_id(context,driver_id):
SelectDriver.input_driver(driver_id)
https://behave.readthedocs.io/en/latest/parse_builtin_types.html

TA貢獻1824條經驗 獲得超5個贊
我將在此處提取該behave 問題結果以總結解決方案。
將類型說明符添加到參數(例如:S 或:d)
@when(u'enter the {driver_id:S}')
def step_enter_the_driver_id(context,driver_id):
SelectDriver.input_driver(driver_id)
@when(u'enter the configured block number')
def step_enter_the_configured_block_number(context):
ByBlock.enter_block(context.block)
先放更復雜(更長?)的定義。所以如果你把它放在一個文件中,它將是:
@when(u'enter the configured block number')
def step_enter_the_configured_block_number(context):
ByBlock.enter_block(context.block)
@when(u'enter the {driver_id}')
def step_enter_the_driver_id(context,driver_id):
SelectDriver.input_driver(driver_id)
或者檢查重命名文件是否有效,如果具有更復雜定義的文件按字母順序排列在另一個文件之前。

TA貢獻1784條經驗 獲得超9個贊
步驟定義的工作方式如下:
你新建一個py文件
您向該文件添加一個新的函數定義
在這種情況下,您的兩個文件都具有相同的函數名稱,
def step_impl
你用你想要與之關聯的功能文件文本裝飾你的新功能
enter the {driver_id}
enter the configured block number
當您運行 behave 時,該程序會收集其所有特征文件及其所有步驟定義,然后嘗試將兩者關聯起來
對于上面的示例,Behave 找到文本
enter the {driver_id}
,并將其與函數相關聯step_impl
然后 Behave 找到文本
enter the configured block number
,并嘗試將其與其函數定義相關聯,但發現函數step_impl
已經被定義并與特征文本相關聯。不知道該怎么做,Behave 拋出AmbiguousStep
異常讓你知道一個函數名被使用了兩次。
要解決此問題,您需要確保您的函數名稱在所有步驟定義文件中都是唯一的。因此,在您的情況下,您有兩個文件,每個文件都定義了一個名為step_impl
. 您應該做的是用唯一名稱重命名這些函數,以便 Behave 可以在運行時正確關聯這些名稱。為確保名稱是唯一的,我建議選擇與裝飾文本緊密匹配的名稱。如果是我寫這些定義,我會重寫如下:
driverlogon.py
@when(u'enter the {driver_id}')
def step_enter_the_driver_id(context,driver_id):
SelectDriver.input_driver(driver_id)
triplogon.py
@when(u'enter the configured block number')
def step_enter_the_configured_block_number(context):
ByBlock.block_data(context.block)
編輯#1:
感謝您包含堆棧跟蹤。由此看來,您在兩個不同的文件中兩次定義了相同的步驟:
File "steps\triplogon.py", line 23, in <module>
@when(u'enter the configured block number')
File "c:\program files (x86)\python\lib\site-packages\behave\step_registry.py", line 92, in wrapper
self.add_step_definition(step_type, step_text, func)
File "c:\program files (x86)\python\lib\site-packages\behave\step_registry.py", line 58, in add_step_definition
raise AmbiguousStep(message % (new_step, existing_step))
behave.step_registry.AmbiguousStep: @when('enter the configured block number') has already been defined in
existing step @when('enter the {driver_id}') at steps/driverlogon.py:26
你會注意到在第一行它說:
File "steps\triplogon.py", line 23, in <module>
@when(u'enter the configured block number')
表示該步驟enter the configured block number定義在triplogon.py
然后跟蹤的最后一行說:
behave.step_registry.AmbiguousStep: @when('enter the configured block number') has already been defined in
existing step @when('enter the {driver_id}') at steps/driverlogon.py:26
這表明enter the configured block number也已在中定義driverlogon.py
確保僅在兩個文件之一中定義了該步驟。
添加回答
舉報