Skip to main content
 首页 » 编程设计

python之我可以将步骤定义放在一个不是 "steps"的文件夹中吗

2024年09月07日8think

我正在尝试在 Python 上使用 Behave。 我想知道是否有办法将我的 .py 文件放在其他地方,而不是被迫将它们全部放在“步骤”文件夹中。我当前的结构如下所示

tests/ 
    features/ 
    steps/ #all code inside here, for now 

我想完成的事情是这样的

tests/ 
    features/ #with all the .feature files 
    login/ #with all the .py files for logging in inside a service 
    models/ #with all the .py files that represents a given object 
    and so on 

我在 Behave 之前使用的唯一 BDD 框架是 Cucumber with Java,它允许在我想要的任何地方插入步骤定义(其余由 Cucumber 本身处理)。 我问这个是因为我想在我的项目中有很多类,以便以更好的方式组织我的代码。

请您参考如下方法:

这可能有点晚,但您可以执行以下操作:

结构如下:

tests/ 
    features/ 
        steps/ 
            login 
            main_menu 
            all_steps.py 

在 steps 的子文件夹中,您可以创建带有实现的 _steps.py 文件,然后在 all_steps.py(或您想要的命名方式)中,您只需导入它们:

from tests.steps.login.<feature>_step import * 
from tests.steps.main_menu.<feature>_step import * 
etc 

当您运行它时,它应该会找到步骤文件。或者,您可以将文件放在项目的任何位置,只要您有 1 个步骤文件夹并且文件中有一个文件是您导入所有步骤