python-3.x - 表现相同名称的多个步骤

标签 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运行该步骤。除了拥有所有唯一的步骤名称之外,是否有任何方法可以区分这些文件?

最佳答案

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

@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/

相关文章:

bdd - BDD方案应该包括实际的测试数据,还是仅描述它?

java - 有什么方法可以模仿 python 的端口密码加密?

python - 如何在slurm中导入cv2?

java - 如何在java代码中获取cucumber中具有多个管道的步骤的参数?

ruby - 单个特征文件中的多个特征

python - BDD 行为 Python 需要创建一个世界地图来保存值

python - 如何模拟 BDD 步骤文件

python - 如何使用 Python 获取当前行为步骤?

python - 有什么办法可以让这台发电机干涸吗?

Python多处理池apply_async错误