行为:在步骤定义之外访问 context.config 个变量
Behave: Accessing context.config variables outside of step definitions
我找不到如何使用 behave.ini
中 context.config.userdata['url']
的值初始化我的 ApiClient
的方法
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
首先,在您的 behave.ini
中,格式很重要。也就是说,记下空格:
[behave.userdata]
url = http://some.url
其次,与其在 /features/steps/steps.py
中创建 ApiClient 对象,不如应该 在 /features/environment.py
中创建它。这是什么environment.py
?如果您不知道,它基本上是一个定义您的测试运行应该发生什么的文件 before/during/after。有关详细信息,请参阅 here。
基本上,你会得到类似的东西:
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'])
稍后,当您想使用 ApiClient 对象时,您可以这样做:
steps.py
from behave import *
@given('I am logged as "{user}" "{password}"')
def login(context, user, password):
context.api_calls.auth(user, password)
我知道这是一个非常古老的问题,但答案不是最新的。当前文档中似乎不需要空格,OP 的问题是 header 标签。必须是 [behave]
我找不到如何使用 behave.ini
中context.config.userdata['url']
的值初始化我的 ApiClient
的方法
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
首先,在您的 behave.ini
中,格式很重要。也就是说,记下空格:
[behave.userdata]
url = http://some.url
其次,与其在 /features/steps/steps.py
中创建 ApiClient 对象,不如应该 在 /features/environment.py
中创建它。这是什么environment.py
?如果您不知道,它基本上是一个定义您的测试运行应该发生什么的文件 before/during/after。有关详细信息,请参阅 here。
基本上,你会得到类似的东西:
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'])
稍后,当您想使用 ApiClient 对象时,您可以这样做:
steps.py
from behave import *
@given('I am logged as "{user}" "{password}"')
def login(context, user, password):
context.api_calls.auth(user, password)
我知道这是一个非常古老的问题,但答案不是最新的。当前文档中似乎不需要空格,OP 的问题是 header 标签。必须是 [behave]