行为,具有相同名称的多个步骤
Behave, multiple steps with same name
我有两个特征文件:
delete.feature
new_directory.feature
还有两步文件:
delete.py
new_directory.py
每个功能文件都是这样开头的:
Background:
Given 'Workspace has the following structure'
按照不同的表格。
当我在步骤文件装饰器中写入时:
@given('Workspace has the following structure')
它如何知道背景属于哪个特征文件?当我 运行 表现得
new_directory.feature
我可以看到 运行 是 delete.feature 的那一步。除了具有所有唯一的步骤名称之外,有什么方法可以区分这些文件吗?
我解决共享步骤的方法是对步骤使用单一实现,根据使用该步骤的功能,该步骤的工作方式不同。适应你的描述,它会是这样的:
@given('Workspace has the following structure')
def step_impl(context):
feature = context.feature
name = os.path.splitext(os.path.basename(feature.filename))[0]
if name == "delete":
# do something
...
elif name == "new_directory":
# do something else
...
else:
raise Exception("can't determine how to run this step")
以上代码基于检查包含该功能的文件的基本名称(减去扩展名)。您也可以检查实际的功能名称,但我认为文件名比功能名称更稳定,所以我更喜欢测试文件名。
我有两个特征文件:
delete.feature
new_directory.feature
还有两步文件:
delete.py
new_directory.py
每个功能文件都是这样开头的:
Background:
Given 'Workspace has the following structure'
按照不同的表格。
当我在步骤文件装饰器中写入时:
@given('Workspace has the following structure')
它如何知道背景属于哪个特征文件?当我 运行 表现得
new_directory.feature
我可以看到 运行 是 delete.feature 的那一步。除了具有所有唯一的步骤名称之外,有什么方法可以区分这些文件吗?
我解决共享步骤的方法是对步骤使用单一实现,根据使用该步骤的功能,该步骤的工作方式不同。适应你的描述,它会是这样的:
@given('Workspace has the following structure')
def step_impl(context):
feature = context.feature
name = os.path.splitext(os.path.basename(feature.filename))[0]
if name == "delete":
# do something
...
elif name == "new_directory":
# do something else
...
else:
raise Exception("can't determine how to run this step")
以上代码基于检查包含该功能的文件的基本名称(减去扩展名)。您也可以检查实际的功能名称,但我认为文件名比功能名称更稳定,所以我更喜欢测试文件名。