mirror of
https://github.com/httprunner/httprunner.git
synced 2026-06-11 02:39:46 +08:00
refactor: adjust code location
This commit is contained in:
@@ -41,6 +41,24 @@ class Context(object):
|
|||||||
if level == "testset":
|
if level == "testset":
|
||||||
self.import_module_items(["ate.built_in"], "testset")
|
self.import_module_items(["ate.built_in"], "testset")
|
||||||
|
|
||||||
|
def config_context(self, config_dict, level):
|
||||||
|
if level == "testset":
|
||||||
|
self.testcase_parser.file_path = config_dict.get("path", None)
|
||||||
|
|
||||||
|
requires = config_dict.get('requires', [])
|
||||||
|
self.import_requires(requires)
|
||||||
|
|
||||||
|
function_binds = config_dict.get('function_binds', {})
|
||||||
|
self.bind_functions(function_binds, level)
|
||||||
|
|
||||||
|
# import_module_functions will be deprecated soon
|
||||||
|
module_items = config_dict.get('import_module_items', []) \
|
||||||
|
or config_dict.get('import_module_functions', [])
|
||||||
|
self.import_module_items(module_items, level)
|
||||||
|
|
||||||
|
variable_binds = config_dict.get('variable_binds', [])
|
||||||
|
self.bind_variables(variable_binds, level)
|
||||||
|
|
||||||
def import_requires(self, modules):
|
def import_requires(self, modules):
|
||||||
""" import required modules dynamically
|
""" import required modules dynamically
|
||||||
"""
|
"""
|
||||||
|
|||||||
@@ -1,5 +1,3 @@
|
|||||||
import requests
|
|
||||||
|
|
||||||
from ate import exception, response
|
from ate import exception, response
|
||||||
from ate.client import HttpSession
|
from ate.client import HttpSession
|
||||||
from ate.context import Context
|
from ate.context import Context
|
||||||
@@ -14,49 +12,49 @@ class Runner(object):
|
|||||||
def init_config(self, config_dict, level):
|
def init_config(self, config_dict, level):
|
||||||
""" create/update context variables binds
|
""" create/update context variables binds
|
||||||
@param (dict) config_dict
|
@param (dict) config_dict
|
||||||
|
@param (str) level, "testset" or "testcase"
|
||||||
|
testset:
|
||||||
{
|
{
|
||||||
"name": "description content",
|
"name": "smoke testset",
|
||||||
"requires": ["random", "hashlib"],
|
"path": "tests/data/demo_testset_variables.yml",
|
||||||
"function_binds": {
|
"requires": [], # optional
|
||||||
"gen_random_string": \
|
"function_binds": {}, # optional
|
||||||
"lambda str_len: ''.join(random.choice(string.ascii_letters + \
|
"import_module_items": [], # optional
|
||||||
string.digits) for _ in range(str_len))",
|
"variable_binds": [], # optional
|
||||||
"gen_md5": \
|
"request": {
|
||||||
"lambda *str_args: hashlib.md5(''.join(str_args).\
|
"base_url": "http://127.0.0.1:5000",
|
||||||
encode('utf-8')).hexdigest()"
|
"headers": {
|
||||||
},
|
"User-Agent": "iOS/2.8.3"
|
||||||
"import_module_items": ["test.data.debugtalk"],
|
}
|
||||||
"variable_binds": [
|
}
|
||||||
{"TOKEN": "debugtalk"},
|
}
|
||||||
{"random": "${gen_random_string(5)}"},
|
testcase:
|
||||||
]
|
{
|
||||||
|
"name": "testcase description",
|
||||||
|
"requires": [], # optional
|
||||||
|
"function_binds": {}, # optional
|
||||||
|
"import_module_items": [], # optional
|
||||||
|
"variable_binds": [], # optional
|
||||||
|
"request": {
|
||||||
|
"url": "/api/get-token",
|
||||||
|
"method": "POST",
|
||||||
|
"headers": {
|
||||||
|
"Content-Type": "application/json"
|
||||||
|
}
|
||||||
|
|
||||||
|
"json": {
|
||||||
|
"sign": "f1219719911caae89ccc301679857ebfda115ca2"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@param (str) context level, testcase or testset
|
@param (str) context level, testcase or testset
|
||||||
"""
|
"""
|
||||||
self.context.init_context(level)
|
self.context.init_context(level)
|
||||||
|
self.context.config_context(config_dict, level)
|
||||||
requires = config_dict.get('requires', [])
|
|
||||||
self.context.import_requires(requires)
|
|
||||||
|
|
||||||
function_binds = config_dict.get('function_binds', {})
|
|
||||||
self.context.bind_functions(function_binds, level)
|
|
||||||
|
|
||||||
# import_module_functions will be deprecated soon
|
|
||||||
module_items = config_dict.get('import_module_items', []) \
|
|
||||||
or config_dict.get('import_module_functions', [])
|
|
||||||
self.context.import_module_items(module_items, level)
|
|
||||||
|
|
||||||
variable_binds = config_dict.get('variable_binds', [])
|
|
||||||
self.context.bind_variables(variable_binds, level)
|
|
||||||
|
|
||||||
request_config = config_dict.get('request', {})
|
request_config = config_dict.get('request', {})
|
||||||
if level == "testset":
|
base_url = request_config.pop("base_url", None)
|
||||||
base_url = request_config.pop("base_url", None)
|
self.http_client_session = self.http_client_session or HttpSession(base_url)
|
||||||
self.http_client_session = self.http_client_session or HttpSession(base_url)
|
|
||||||
self.context.testcase_parser.file_path = config_dict.get("path", None)
|
|
||||||
else:
|
|
||||||
# testcase
|
|
||||||
self.http_client_session = self.http_client_session or requests.Session()
|
|
||||||
self.context.register_request(request_config, level)
|
self.context.register_request(request_config, level)
|
||||||
|
|
||||||
def run_test(self, testcase):
|
def run_test(self, testcase):
|
||||||
|
|||||||
Reference in New Issue
Block a user