为了账号安全,请及时绑定邮箱和手机立即绑定

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 关注
  • 136 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信