1

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
Sarunas Urbelis
  • 567
  • 5
  • 14
  • Is the problem reading the ini file? Does https://stackoverflow.com/questions/8884188/how-to-read-and-write-ini-file-with-python3 help? – doctorlove Jun 13 '18 at 12:20
  • No, I can read the ini file and access the data from it in my step defs. Though I have no clue how to access it outside of the steps so I could use values from it when initializing ApiClient. – Sarunas Urbelis Jun 13 '18 at 12:30

2 Answers2

5

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)
natn2323
  • 1,983
  • 1
  • 13
  • 30
0

I know this is a super old question but the answer is not current. No spaces seem to be required in the current documentation and OP's issue is the header tag. It must be [behave]

https://behave.readthedocs.io/en/latest/behave.html