我有两个功能文件:
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")
上面的代码基于检查包含功能的文件的基本名称(减去扩展名)。您也可以检查实际的功能名称,但是我认为文件名称比功能名称更稳定,因此我更喜欢测试文件名称。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句