行为测试-找不到模块

时间:2019-01-29 20:02:18

标签: python python-3.6 python-behave

我开始使用Behave编写一些BDD测试,但是在尝试在测试中导入util模块时遇到了问题。

这是我当前的文件夹结构:

root/
    __init__.py
    README
    features/
        __init__.py
        test.feature
        environment.py
        steps/
            __init__.py
            steps_test.py
            utils/
              __init__.py
              string_utils.py
              user_utils.py

在我的environment.py中,我希望一些设置代码在功能之前运行:

import steps.utils.user_utils
def before_feature(context, feature):
    username = user_utils.create_username()
    print(username)

在我的user_utils.py中:

import string_utils
def create_username():
    name = string_utils.gen_str(10, 3)
    return name

但是当我从项目behave文件夹运行root/时,出现错误消息:

$ behave

...

File "root/features/steps/utils/user_utils.py", line 1, in <module>
    import string_utils
ModuleNotFoundError: No module named 'string_utils'

但是,如果我在python user_utils.py文件夹中运行utils/,则不会出现任何错误,这向我表明它确实找到了该模块。

如何通过行为识别模块?

1 个答案:

答案 0 :(得分:0)

问题在于,Behave期望一个相当严格的目录结构。即,它不会递归搜索steps/文件夹内的文件夹。因此,您需要将utils/文件夹中的内容上移到steps/文件夹中,这样才能正常工作。