2

我有两个功能文件:

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 运行该步骤。除了拥有所有唯一的步骤名称之外,有没有办法在这些文件之间产生差异?

4

1 回答 1

2

我解决共享步骤的方法是对步骤使用单个实现,根据使用该步骤的功能不同,该步骤的工作方式不同。适应你的描述,它会是这样的:

@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")

上面的代码基于检查包含该功能的文件的基本名称(减去扩展名)。您也可以检查实际的功能名称,但我认为文件名比功能名称更稳定,所以我更喜欢测试文件名。

于 2015-04-16T10:59:07.770 回答