python中不同步骤名称的不明确步骤异常

python中不同步骤名称的不明确步骤异常,python,python-behave,Python,Python Behave,我在steps目录下有两个不同的python文件: driverlogon.py和 triplogon.py 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 n

我在steps目录下有两个不同的python文件: driverlogon.pytriplogon.py

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.enter_block(context.block)
运行此程序时,我收到以下错误消息
在这种情况下,它们有不同的文本,甚至函数的内容也不同。
为什么会这样,有人能帮我理解吗?提前谢谢

异常模糊步骤:@when('enter the configured block number')已在中定义
steps/driverlogon.py中的现有步骤@when('输入{driver_id}'):26
回溯(最近一次呼叫最后一次):
文件“C:\ProgramFiles(x86)\Python\Lib\runpy.py”,第193行,位于\u run\u模块\u as\u main中
“\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
文件“C:\ProgramFiles(x86)\Python\Lib\runpy.py”,第85行,在运行代码中
exec(代码、运行\全局)
文件“C:\Program Files(x86)\Python\Scripts\behave.exe\\uuuuuu main\uuuuuuuu.py”,第7行,在
文件“c:\program files(x86)\python\lib\site packages\behave\\uuuuu main\uuuuu.py”,第183行,在main中
返回运行(配置)
文件“c:\program files(x86)\python\lib\site packages\behave\\uuuuu main\uuuuuu.py”,第127行,在run\u behave中
failed=runner.run()
文件“c:\program files(x86)\python\lib\site packages\behave\runner.py”,第804行,正在运行
返回self.run_与_路径()
文件“c:\program files(x86)\python\lib\site packages\behave\runner.py”,第809行,位于带有路径的run\u中
self.load_步骤_定义()
文件“c:\program files(x86)\python\lib\site packages\behave\runner.py”,第796行,位于加载步骤定义中
加载步骤模块(步骤路径)
文件“c:\program files(x86)\python\lib\site packages\behave\runner\u util.py”,第412行,位于load\u step\u模块中
exec_文件(os.path.join(路径、名称)、步骤_模块_全局)
exec\U文件中的文件“c:\program files(x86)\python\lib\site packages\behave\runner\u util.py”,第386行
执行官(代码、全局变量、局部变量)
文件“steps\triplogon.py”,第23行,在
@当(u'输入配置的块编号')
文件“c:\program files(x86)\python\lib\site packages\behave\step\u registry.py”,第92行,在包装器中
添加步骤定义(步骤类型、步骤文本、函数)
文件“c:\program files(x86)\python\lib\site packages\behave\step\u registry.py”,第58行,在add\u step\u定义中
提出含糊步骤(消息%(新步骤、现有步骤))
behave.step_registry.AmbiguousStep:@when('enter the configured block number')已在中定义
steps/driverlogon.py中的现有步骤@when('输入{driver_id}'):26

步骤定义的工作方式如下:

  • 创建一个新的py文件
  • 将新函数定义添加到该文件中
    • 在这种情况下,两个文件具有相同的函数名,
      def step\u impl
  • 您可以使用想要关联的要素文件文本来装饰新函数
    • 输入{driver\u id}
    • 输入配置的区块编号
  • 运行behave时,程序将收集其所有功能文件和所有步骤定义,然后尝试将两者关联起来
  • 对于上面的示例,Behave查找文本
    输入{driver\u id}
    ,并将其与函数
    步骤执行关联
  • 然后Behave查找文本
    输入配置的块号
    ,并尝试将其与其功能定义关联,但发现功能
    步骤已定义并与功能文本关联。在不知道该做什么的情况下,Behave抛出
    模糊步骤
    异常,让您知道函数名已经使用了两次
要解决此问题,需要确保函数名在所有步骤定义文件中都是唯一的。因此,在您的例子中,有两个文件,每个文件定义一个名为
step\u impl
的函数。您应该使用唯一的名称重命名这些函数,以便Behave可以在运行时正确关联这些名称。为了确保名称是唯一的,我建议选择与修饰文本紧密匹配的名称。如果是我写这些定义,我会重写如下:

driverlogon.py

@when(u'enter the {driver_id}')
def step_enter_the_driver_id(context,driver_id):
     SelectDriver.input_driver(driver_id)
@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)
@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
这表明
输入配置的块编号
也已在
driverlogon.py


确保仅在两个文件中的一个文件中定义了步骤。

作为解决方案,如果要保留这些名称,可以编写:

driverlogon.py

@when(u'enter the {driver_id}')
def step_enter_the_driver_id(context,driver_id):
     SelectDriver.input_driver(driver_id)
@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)
@when(u'enter the configured block number') 
def step_enter_the_configured_block_number(context):
   ByBlock.block_data(context.block)
在这种情况下不会引发异常,但驱动程序id将作为字符串传递,步骤如下所示:

当输入“10”时

但是,如果您希望将其解析为int,您可以在这种情况下使用预定义的数据类型
d
,如下所示:

@when(u'enter the "{driver_id:d}"')
def step_enter_the_driver_id(context,driver_id):
     SelectDriver.input_driver(driver_id)

不是“行为”方面的专家,因此无法回答,但我认为正确的代码格式将有助于解决这个问题。在某些文本周围使用回退标记``来格式化为代码“inline”
,就像这样
,或者在前一行和后一行中使用带有三个回退标记``的文本块来定义代码块。谢谢你的提示,它现在看起来更可读了…对不起,我是新来的behave没有完全理解它…你是说def后面的文本“enter_the”吗。另外,我不理解“使用反映步骤文本的函数名:”更新了说明,如果有帮助,请告诉我。@Levi Noecker,谢谢你回答我的问题如此详细,我尝试了你的建议。它不起作用,然后,我将所有其他已实现函数的函数名从“step\u impl”更改为“step\u xx\u yyy”``,它仍然不起作用,我已经更新了