behave 无法检测到environment.py 文件

问题描述 投票:0回答:1

大家好,我正在尝试开发行为框架来自动化一组 API。文件夹结构如下:

environment.py文件有以下代码:

from features.configuration.api_resources import LibraryAPIResources
from features.configuration.config import get_config
from features.resources.api_payload import *
from features.resources.test_data_generator import *


def before_all(context,scenario):
    print('This is before scenario')
    if scenario.startswith("TC01"):
        context.my_config = get_config()
        context.header = {'Content-Type': 'application/json'}
        context.book_name = generate_random_book_name()
        context.isbn = generate_random_isbn()
        context.aisle = generate_random_aisle()
        context.full_name = generate_random_full_name()
        context.add_book_url = f'{context.my_config["QA"]["baseURL"]}{LibraryAPIResources.add_book}'
        context.add_book_payload = get_add_book_payload(context.book_name, context.isbn, context.aisle,
                                                        context.full_name)

每当我尝试干运行我的脚本时,我都可以看到它可以检测步骤实现,但它没有运行我在environment.py中提供的before_all钩子并抛出错误。

任何人都可以帮我解决为什么我会遇到这个问题吗?我使用的是 Python 版本 3.8.5,行为版本 1.2.6

python cucumber bdd python-behave
1个回答
0
投票

您的environment.py文件的位置是错误的。 来自官方文档

“environment.py”文件(如果存在)必须位于包含“steps”目录的同一目录中

所以基本上你必须将文件向上移动一级(从“hooks”文件夹到“features”文件夹)。

© www.soinside.com 2019 - 2024. All rights reserved.