mirror of
https://github.com/httprunner/httprunner.git
synced 2026-05-20 15:50:47 +08:00
add support for import module custom functions
This commit is contained in:
@@ -1,7 +1,17 @@
|
||||
import re
|
||||
import importlib
|
||||
import re
|
||||
import types
|
||||
|
||||
from ate import exception, utils
|
||||
|
||||
|
||||
def is_function(tup):
|
||||
"""
|
||||
Takes (name, object) tuple, returns True if it is a function.
|
||||
"""
|
||||
name, item = tup
|
||||
return isinstance(item, types.FunctionType)
|
||||
|
||||
class Context(object):
|
||||
""" Manages binding of variables
|
||||
"""
|
||||
@@ -29,6 +39,14 @@ class Context(object):
|
||||
function = eval(function)
|
||||
self.functions[func_name] = function
|
||||
|
||||
def import_module_functions(self, modules):
|
||||
""" import modules and bind all functions within the context
|
||||
"""
|
||||
for module_name in modules:
|
||||
imported = importlib.import_module(module_name)
|
||||
imported_functions_dict = dict(filter(is_function, vars(imported).items()))
|
||||
self.functions.update(imported_functions_dict)
|
||||
|
||||
def bind_variables(self, variable_binds):
|
||||
""" Bind named variables to value within the context.
|
||||
This allows for passing in variables or functions.
|
||||
|
||||
@@ -41,6 +41,9 @@ class TestRunner(object):
|
||||
function_binds = config_dict.get('function_binds', {})
|
||||
self.context.bind_functions(function_binds)
|
||||
|
||||
module_functions = config_dict.get('import_module_functions', [])
|
||||
self.context.import_module_functions(module_functions)
|
||||
|
||||
variable_binds = config_dict.get('variable_binds', [])
|
||||
self.context.bind_variables(variable_binds)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user