behave is not able to detect environment.py file

63 Views Asked by At

Hi all I am trying to work on behave framework to automate a set of APIs. The folder structure is as below:

enter image description here

The environment.py file has the below code:

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)

Whenever I try to dry run my script, I can see that it can detect step implementations, but it is not running the before_all hook I provided in the environment.py and throwing an error.

enter image description here

Can anyone help me out to as why I am facing this issue? I am using Python version 3.8.5 and behave version 1.2.6

1

There are 1 best solutions below

0
sashkins On

The location of your environment.py file is wrong. From official doccumentaion:

The “environment.py” file, if present, must be in the same directory that contains the “steps” directory

So basically you have to move your file one level up (from the 'hooks' folder to the 'features' folder).