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
确保仅在两个文件之一中定义了该步骤。
添加回答
举报