pythonpython-2.7python-behave

Behave: Accessing context.config variables outside of step definitions


I am can't find out a way how to init my ApiClient with the value of context.config.userdata['url'] from behave.ini

behave.ini

[behave.userdata]
url=http://some.url

steps.py

from behave import *
from client.api_client import ApiClient

# This is where i want to pass the config.userdata['url'] value
api_calls = ApiClient('???') 


@given('I am logged as "{user}" "{password}"')
def login(context, user, password):
    context.auth_header = api_calls.auth(user, password)

api_client.py

class ApiClient(object):

    def __init__(self, url):
        self.url = url

    def auth(self, email, password):
        auth_payload = {'email': email, 'password': password}
        auth_response = requests.post(self.url + '/api/auth/session', auth_payload)

        return auth_response.text

Solution

  • First things first, in your behave.ini, formatting matters. That is, make note of the spaces:

    [behave.userdata]
    url = http://some.url
    

    Second, instead of creating your ApiClient object in your /features/steps/steps.py, you should be creating it within your /features/environment.py. What's this environment.py? If you don't know, it's basically a file that defines what should happen before/during/after your test runs. See here for more details.

    Essentially, you would have something like:

    environment.py

    from client.api_client import ApiClient
    
    """ 
    The code within the following block is checked before all/any of test steps are run.
    This would be a great place to instantiate any of your class objects and store them as
    attributes in behave's context object for later use.
    """
    def before_all(context):         
        # The following creates an api_calls attribute for behave's context object
        context.api_calls = ApiClient(context.config.userdata['url'])
    

    Later, when you want to use your ApiClient object, you could do it like so:

    steps.py

    from behave import *
    
    @given('I am logged as "{user}" "{password}"')
    def login(context, user, password):
        context.api_calls.auth(user, password)