Python 我是否可以将步骤定义放在一个不是";的文件夹中;步骤;你有什么行为?
我正在尝试在Python上使用Behave。 我想知道是否有办法将我的.py文件放在其他地方,而不是被迫将它们全部放在“步骤”文件夹中。我目前的结构是这样的Python 我是否可以将步骤定义放在一个不是";的文件夹中;步骤;你有什么行为?,python,bdd,python-behave,Python,Bdd,Python Behave,我正在尝试在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
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自己处理)。
我这样问是因为我希望在我的项目中有很多类,以便更好地组织我的代码。首先,从behave文档(1.2.7.dev0版)中: behave可处理三种类型的文件:
steps/
目录
为了完成一个类似于您所想的解决方法,我尝试在/steps
目录中创建一个子目录:/steps/deep/
,并在那里插入我的Python文件:/steps/deep/testing.py
。在运行behave之后,我收到了“NotImplementedError”,这意味着没有找到/deepher/testing.py
中的步骤定义
behave似乎不会在steps/
目录的子目录中递归搜索任何其他Python文件
至于您试图做什么,我认为这是一个不错的组织想法,但由于它不可行,您可以这样做:与其在tests/
目录中为Python文件设置目录,不如为Python文件设置一个好的命名约定,并将相关函数分离到它们自己的Python文件中?即:
tests/
features/
steps/
login_prompt.py # contains all the functions for logging in inside a service
login_ssh.py # contains all the functions for SSH login
models_default.py # contains all the functions for the default object
models_custom.py # contains all the functions for a custom object
and so on...
当然,在这一点上,如果您将它们分为不同的Python文件,这其实并不重要,因为behave在调用时会在
步骤/
中搜索所有Python文件,但为了组织起见,它会实现相同的效果。这可能有点晚,但您可以执行以下操作:
有这样的结构:
tests/
features/
steps/
login
main_menu
all_steps.py
在步骤中的子文件夹中,您可以使用实现创建_steps.py文件,然后在all_steps.py(或您希望如何命名)中,您只需导入它们:
from tests.steps.login.<feature>_step import *
from tests.steps.main_menu.<feature>_step import *
etc
来自tests.steps.login.\u步骤导入*
从tests.steps.main_菜单。\u步骤导入*
等
当你运行这个程序时,它会找到步骤文件。或者,您可以在项目中的任何位置拥有这些文件,只要您在导入所有步骤时在文件中有1个步骤文件夹和一个文件如果这有帮助,请选择它作为此问题的答案!不幸的是,它做到了。我想相信这可能有一个解决办法,但看起来没有。。。非常感谢@musikreck的详细回答!在运行intellij runner时,它会成功执行,而在运行behave命令时,它会出错