亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

python 行為中不同步驟名稱顯示的不明確步驟異常

python 行為中不同步驟名稱顯示的不明確步驟異常

呼喚遠方 2023-04-25 16:47:58
我在 steps 目錄下有兩個不同的 python 文件: driverlogon.py和 triplogon.pydriverlogon.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.enter_block(context.block)當我運行這個程序時,我收到以下錯誤消息,在這種情況下,它們有不同的文本,甚至函數的內容也不同。為什么會這樣,誰能幫我理解這個?提前致謝Exception AmbiguousStep: @when('enter the configured block number') has already been defined in  existing step @when('enter the {driver_id}') at steps/driverlogon.py:26Traceback (most recent call last):  File "C:\Program Files (x86)\Python\Lib\runpy.py", line 193, in _run_module_as_main    "__main__", mod_spec)  File "C:\Program Files (x86)\Python\Lib\runpy.py", line 85, in _run_code    exec(code, run_globals)  File "C:\Program Files (x86)\Python\Scripts\behave.exe\__main__.py", line 7, in <module>  File "c:\program files (x86)\python\lib\site-packages\behave\__main__.py", line 183, in main    return run_behave(config)  File "c:\program files (x86)\python\lib\site-packages\behave\__main__.py", line 127, in run_behave    failed = runner.run()  File "c:\program files (x86)\python\lib\site-packages\behave\runner.py", line 804, in run    return self.run_with_paths()  File "c:\program files (x86)\python\lib\site-packages\behave\runner.py", line 809, in run_with_paths    self.load_step_definitions()  File "c:\program files (x86)\python\lib\site-packages\behave\runner.py", line 796, in load_step_definitions    load_step_modules(step_paths)  File "c:\program files (x86)\python\lib\site-packages\behave\runner_util.py", line 412, in load_step_modules    exec_file(os.path.join(path, name), step_module_globals)  File "c:\program files (x86)\python\lib\site-packages\behave\runner_util.py", line 386, in exec_file    exec(code, globals_, locals_)  File "steps\triplogon.py", line 23, in <module>
查看完整描述

4 回答

?
蠱毒傳說

TA貢獻1895條經驗 獲得超3個贊

歧義問題來自另一個方向,并且不能總是通過使用雙引號來解決。歧義可能仍然存在。

正確的解決方案是對參數使用類型模式,否則將使用所有格解析器并盡可能多地消耗——為假定的重復項留出空間。


查看完整回答
反對 回復 2023-04-25
?
波斯汪

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


查看完整回答
反對 回復 2023-04-25
?
滄海一幻覺

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)

或者檢查重命名文件是否有效,如果具有更復雜定義的文件按字母順序排列在另一個文件之前。


查看完整回答
反對 回復 2023-04-25
?
千萬里不及你

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


確保僅在兩個文件之一中定義了該步驟。


查看完整回答
反對 回復 2023-04-25
  • 4 回答
  • 0 關注
  • 170 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號