4 回答

TA貢獻(xiàn)1895條經(jīng)驗(yàn) 獲得超3個(gè)贊
歧義問(wèn)題來(lái)自另一個(gè)方向,并且不能總是通過(guò)使用雙引號(hào)來(lái)解決。歧義可能仍然存在。
正確的解決方案是對(duì)參數(shù)使用類型模式,否則將使用所有格解析器并盡可能多地消耗——為假定的重復(fù)項(xiàng)留出空間。

TA貢獻(xiàn)1811條經(jīng)驗(yàn) 獲得超4個(gè)贊
作為一種解決方案,如果您想保留這些名稱,您可以這樣寫(xiě):
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)
在這種情況下不會(huì)引發(fā)異常,但 driver_id 將作為字符串傳遞,步驟將如下所示:
When enter the "10"
但是,如果您希望它被解析為 int 而不是您可以d在這種情況下使用預(yù)定義的數(shù)據(jù)類型,如下所示:
@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貢獻(xiàn)1824條經(jīng)驗(yàn) 獲得超5個(gè)贊
我將在此處提取該behave 問(wèn)題結(jié)果以總結(jié)解決方案。
將類型說(shuō)明符添加到參數(shù)(例如: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)
先放更復(fù)雜(更長(zhǎng)?)的定義。所以如果你把它放在一個(gè)文件中,它將是:
@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)
或者檢查重命名文件是否有效,如果具有更復(fù)雜定義的文件按字母順序排列在另一個(gè)文件之前。

TA貢獻(xiàn)1784條經(jīng)驗(yàn) 獲得超9個(gè)贊
步驟定義的工作方式如下:
你新建一個(gè)py文件
您向該文件添加一個(gè)新的函數(shù)定義
在這種情況下,您的兩個(gè)文件都具有相同的函數(shù)名稱,
def step_impl
你用你想要與之關(guān)聯(lián)的功能文件文本裝飾你的新功能
enter the {driver_id}
enter the configured block number
當(dāng)您運(yùn)行 behave 時(shí),該程序會(huì)收集其所有特征文件及其所有步驟定義,然后嘗試將兩者關(guān)聯(lián)起來(lái)
對(duì)于上面的示例,Behave 找到文本
enter the {driver_id}
,并將其與函數(shù)相關(guān)聯(lián)step_impl
然后 Behave 找到文本
enter the configured block number
,并嘗試將其與其函數(shù)定義相關(guān)聯(lián),但發(fā)現(xiàn)函數(shù)step_impl
已經(jīng)被定義并與特征文本相關(guān)聯(lián)。不知道該怎么做,Behave 拋出AmbiguousStep
異常讓你知道一個(gè)函數(shù)名被使用了兩次。
要解決此問(wèn)題,您需要確保您的函數(shù)名稱在所有步驟定義文件中都是唯一的。因此,在您的情況下,您有兩個(gè)文件,每個(gè)文件都定義了一個(gè)名為step_impl
. 您應(yīng)該做的是用唯一名稱重命名這些函數(shù),以便 Behave 可以在運(yùn)行時(shí)正確關(guān)聯(lián)這些名稱。為確保名稱是唯一的,我建議選擇與裝飾文本緊密匹配的名稱。如果是我寫(xiě)這些定義,我會(huì)重寫(xiě)如下:
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:
感謝您包含堆棧跟蹤。由此看來(lái),您在兩個(gè)不同的文件中兩次定義了相同的步驟:
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
你會(huì)注意到在第一行它說(shuō):
File "steps\triplogon.py", line 23, in <module>
@when(u'enter the configured block number')
表示該步驟enter the configured block number定義在triplogon.py
然后跟蹤的最后一行說(shuō):
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
確保僅在兩個(gè)文件之一中定義了該步驟。
添加回答
舉報(bào)