我有两个功能文件:
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")
上面的代码基于检查包含该功能的文件的基本名称(减去扩展名)。您还可以检查实际的功能名称,但我认为文件名比功能名称更稳定,因此我更喜欢测试文件名。
关于python-3.x - 表现相同名称的多个步骤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29661997/