表现,具有相同名称的多个步骤

时间:2015-04-15 22:24:44

标签: python-3.x gherkin python-behave

我有两个功能文件:

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运行该步骤。除了拥有所有唯一的步骤名称之外,还有什么方法可以区分这些文件吗?

1 个答案:

答案 0 :(得分: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")

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

相关问题