remove debugtalk.py variables mechanism

This commit is contained in:
debugtalk
2018-11-15 14:48:44 +08:00
parent badb473520
commit 626612061e
21 changed files with 135 additions and 237 deletions

View File

@@ -13,10 +13,12 @@ class Context(object):
def __init__(self, variables=None, functions=None):
""" init Context with testcase variables and functions.
"""
variables = variables or {}
functions = functions or {}
# testcase level context
## TESTCASE_SHARED_VARIABLES_MAPPING and TESTCASE_SHARED_FUNCTIONS_MAPPING are unchangeable.
self.TESTCASE_SHARED_VARIABLES_MAPPING = utils.ensure_mapping_format(variables)
self.TESTCASE_SHARED_FUNCTIONS_MAPPING = functions or OrderedDict()
self.TESTCASE_SHARED_FUNCTIONS_MAPPING = functions
# testcase level request, will not change
self.TESTCASE_SHARED_REQUEST_MAPPING = {}

View File

@@ -8,7 +8,7 @@ import os
import sys
import yaml
from httprunner import built_in, exceptions, logger, parser, utils, validator
from httprunner import exceptions, logger, parser, utils, validator
from httprunner.compat import OrderedDict
###############################################################################
@@ -179,6 +179,7 @@ def load_dot_env_file(dot_env_path):
env_variables_mapping[variable.strip()] = value.strip()
utils.set_os_environ(env_variables_mapping)
return env_variables_mapping
@@ -219,96 +220,58 @@ def locate_file(start_path, file_name):
## debugtalk.py module loader
###############################################################################
def load_python_module(module):
""" load python module.
def load_module_functions(module):
""" load python module functions.
Args:
module: python module
Returns:
dict: variables and functions mapping for specified python module
dict: functions mapping for specified python module
{
"variables": {},
"functions": {}
"func1_name": func1,
"func2_name": func2
}
"""
# TODO (2.0): remove variables from debugtalk.py
debugtalk_module = {
"variables": {},
"functions": {}
}
module_functions = {}
for name, item in vars(module).items():
if validator.is_function((name, item)):
debugtalk_module["functions"][name] = item
elif validator.is_variable((name, item)):
if isinstance(item, tuple):
continue
debugtalk_module["variables"][name] = item
else:
pass
module_functions[name] = item
return debugtalk_module
return module_functions
def load_builtin_module():
""" load built_in module
def load_builtin_functions():
""" load built_in module functions
"""
built_in_module = load_python_module(built_in)
return built_in_module
from httprunner import built_in
return load_module_functions(built_in)
def load_debugtalk_module():
""" load project debugtalk.py module
def load_debugtalk_functions(debugtalk_path):
""" load project debugtalk.py module functions
debugtalk.py should be located in project working directory.
Args:
debugtalk_path(str): debugtalk.py path
Returns:
dict: debugtalk module mapping
dict: debugtalk module functions mapping
{
"variables": {},
"functions": {}
"func1_name": func1,
"func2_name": func2
}
"""
if not debugtalk_path:
return {}
# load debugtalk.py module
imported_module = importlib.import_module("debugtalk")
debugtalk_module = load_python_module(imported_module)
return debugtalk_module
def get_module_item(module_mapping, item_type, item_name):
""" get expected function or variable from module mapping.
Args:
module_mapping(dict): module mapping with variables and functions.
{
"variables": {},
"functions": {}
}
item_type(str): "functions" or "variables"
item_name(str): function name or variable name
Returns:
object: specified variable or function object.
Raises:
exceptions.FunctionNotFound: If specified function not found in module mapping
exceptions.VariableNotFound: If specified variable not found in module mapping
"""
try:
return module_mapping[item_type][item_name]
except KeyError:
err_msg = "{} not found in debugtalk.py module!\n".format(item_name)
err_msg += "module mapping: {}".format(module_mapping)
if item_type == "functions":
raise exceptions.FunctionNotFound(err_msg)
else:
raise exceptions.VariableNotFound(err_msg)
return load_module_functions(imported_module)
###############################################################################
@@ -899,7 +862,7 @@ def load_project_tests(test_path, dot_env_path=None):
dot_env_path (str): specified .env file path
Returns:
dict: project loaded api/testcases definitions, environments and debugtalk.py module.
dict: project loaded api/testcases definitions, environments and debugtalk.py functions.
"""
project_mapping = {}
@@ -924,13 +887,7 @@ def load_project_tests(test_path, dot_env_path=None):
project_mapping["env"] = {}
# load debugtalk.py
if debugtalk_path:
project_mapping["debugtalk"] = load_debugtalk_module()
else:
project_mapping["debugtalk"] = {
"variables": {},
"functions": {}
}
project_mapping["functions"] = load_debugtalk_functions(debugtalk_path)
project_mapping["def-api"] = load_api_folder(os.path.join(project_working_directory, "api"))
# TODO: replace suite with testcases
@@ -960,10 +917,7 @@ def load_tests(path, dot_env_path=None):
"variables": [], # optional
"request": {} # optional
"refs": {
"debugtalk": {
"variables": {},
"functions": {}
},
"functions": {},
"env": {},
"def-api": {},
"def-testcase": {}
@@ -1049,10 +1003,7 @@ def load_locust_tests(path, dot_env_path=None):
raw_testcase = load_file(path)
project_mapping = load_project_tests(path, dot_env_path)
config = {
"variables": project_mapping["debugtalk"]["variables"],
"functions": project_mapping["debugtalk"]["functions"]
}
config = {}
tests = []
for item in raw_testcase:
key, test_block = item.popitem()
@@ -1067,31 +1018,25 @@ def load_locust_tests(path, dot_env_path=None):
# parse config variables
raw_config_variables = config.get("variables", [])
parsed_config_variables = parser.parse_data(
raw_config_variables,
project_mapping["debugtalk"]["variables"],
project_mapping["debugtalk"]["functions"]
)
# priority: passed in > debugtalk.py > parameters > variables
# override variables mapping with parameters mapping
config_variables = utils.override_mapping_list(
parsed_config_variables, {})
# merge debugtalk.py module variables
config_variables.update(project_mapping["debugtalk"]["variables"])
config_variables = parser.parse_data(
raw_config_variables,
{},
project_mapping["functions"]
)
# parse config name
config["name"] = parser.parse_data(
config.get("name", ""),
config_variables,
project_mapping["debugtalk"]["functions"]
project_mapping["functions"]
)
# parse config request
config["request"] = parser.parse_data(
config.get("request", {}),
config_variables,
project_mapping["debugtalk"]["functions"]
project_mapping["functions"]
)
return {

View File

@@ -325,34 +325,6 @@ def parse_parameters(parameters, variables_mapping, functions_mapping):
## parse content with variables and functions mapping
###############################################################################
def get_builtin_item(item_type, item_name):
"""
Args:
item_type (enum): "variables" or "functions"
item_name (str): variable name or function name
Returns:
variable or function with the name of item_name
"""
# override built_in module with debugtalk.py module
from httprunner import loader
built_in_module = loader.load_builtin_module()
if item_type == "variables":
try:
return built_in_module["variables"][item_name]
except KeyError:
raise exceptions.VariableNotFound("{} is not found.".format(item_name))
else:
# item_type == "functions":
try:
return built_in_module["functions"][item_name]
except KeyError:
raise exceptions.FunctionNotFound("{} is not found.".format(item_name))
def get_mapping_variable(variable_name, variables_mapping):
""" get variable from variables_mapping.
@@ -367,10 +339,10 @@ def get_mapping_variable(variable_name, variables_mapping):
exceptions.VariableNotFound: variable is not found.
"""
if variable_name in variables_mapping:
try:
return variables_mapping[variable_name]
else:
return get_builtin_item("variables", variable_name)
except KeyError:
raise exceptions.VariableNotFound("{} is not found.".format(variable_name))
def get_mapping_function(function_name, functions_mapping):
@@ -392,12 +364,15 @@ def get_mapping_function(function_name, functions_mapping):
return functions_mapping[function_name]
try:
return get_builtin_item("functions", function_name)
except exceptions.FunctionNotFound:
# check if HttpRunner builtin functions
from httprunner import loader
built_in_functions = loader.load_builtin_functions()
return built_in_functions[function_name]
except KeyError:
pass
try:
# check if builtin functions
# check if Python builtin functions
item_func = eval(function_name)
if callable(item_func):
# is builtin function
@@ -608,10 +583,7 @@ def parse_tests(testcases, variables_mapping=None):
project_mapping = testcase_config.pop(
"refs",
{
"debugtalk": {
"variables": {},
"functions": {}
},
"functions": {},
"env": {},
"def-api": {},
"def-testcase": {}
@@ -626,8 +598,8 @@ def parse_tests(testcases, variables_mapping=None):
config_parameters = testcase_config.pop("parameters", [])
cartesian_product_parameters_list = parse_parameters(
config_parameters,
project_mapping["debugtalk"]["variables"],
project_mapping["debugtalk"]["functions"]
{},
project_mapping["functions"]
) or [{}]
for parameter_mapping in cartesian_product_parameters_list:
@@ -637,10 +609,9 @@ def parse_tests(testcases, variables_mapping=None):
raw_config_variables = config.get("variables", [])
raw_config_variables_mapping = utils.ensure_mapping_format(raw_config_variables)
# priority: passed in > .env > debugtalk.py > parameters > variables
# priority: passed in > parameters > variables
config_variables = utils.deepcopy_dict(parameter_mapping)
config_variables.update(project_mapping["debugtalk"]["variables"])
config_variables.update(variables_mapping)
for key, value in raw_config_variables_mapping.items():
@@ -653,7 +624,7 @@ def parse_tests(testcases, variables_mapping=None):
parsed_value = parse_data(
value,
config_variables,
project_mapping["debugtalk"]["functions"]
project_mapping["functions"]
)
config_variables[key] = parsed_value
@@ -663,18 +634,18 @@ def parse_tests(testcases, variables_mapping=None):
testcase_dict["config"]["name"] = parse_data(
testcase_dict["config"].get("name", ""),
config_variables,
project_mapping["debugtalk"]["functions"]
project_mapping["functions"]
)
# parse config request
testcase_dict["config"]["request"] = parse_data(
testcase_dict["config"].get("request", {}),
config_variables,
project_mapping["debugtalk"]["functions"]
project_mapping["functions"]
)
# put loaded project functions to config
testcase_dict["config"]["functions"] = project_mapping["debugtalk"]["functions"]
testcase_dict["config"]["functions"] = project_mapping["functions"]
parsed_testcases_list.append(testcase_dict)
# unset OS environment variables

View File

@@ -94,4 +94,4 @@
- ${teardown_hook_sleep_N_secs($response, $n_secs)}
validate:
- eq: ["status_code", 200]
- contained_by: [content.headers.Host, $HTTPBIN_SERVER]
- contained_by: [content.headers.Host, "${get_httpbin_server()}"]

View File

@@ -0,0 +1,27 @@
# fix #258
- config:
name: "user management testcase."
variables:
- user_agent: 'iOS/10.3'
- device_sn: ${gen_random_string(15)}
- os_platform: 'ios'
- app_version: '2.8.6'
parameters:
- user_agent: ['iOS', 'android']
request:
base_url: ${get_base_url()}
headers:
Content-Type: application/json
device_sn: $device_sn
output:
- token
- test:
name: get token with $user_agent, $app_version
api: get_token($user_agent, $device_sn, $os_platform, $app_version)
extract:
- token: content.token
validate:
- "eq": ["status_code", 200]
- "len_eq": ["content.token", 16]
- "contains": [{"a": 1, "b": 2}, "b"]

View File

@@ -10,7 +10,7 @@
- os_platform: 'ios'
- app_version: 2.8.5
request:
base_url: $BASE_URL
base_url: ${get_base_url()}
headers:
Content-Type: application/json
device_sn: $device_sn

View File

@@ -8,7 +8,7 @@
- [11, 21]
- [12, 22]
- "app_version": "${gen_app_version()}"
request: $demo_default_request
request: ${get_default_request()}
- test:
name: testcase1-$var_a

View File

@@ -6,7 +6,7 @@
- os_platform: 'ios'
- app_version: '2.8.6'
request:
base_url: $BASE_URL
base_url: ${get_base_url()}
headers:
Content-Type: application/json
device_sn: $device_sn

View File

@@ -6,7 +6,7 @@
- os_platform: 'ios'
- app_version: '2.8.6'
request:
base_url: $BASE_URL
base_url: ${get_base_url()}
headers:
Content-Type: application/json
device_sn: $device_sn

View File

@@ -3,7 +3,7 @@
variables:
- device_sn: 'HZfFBh6tU59EdXJ'
request:
base_url: $BASE_URL
base_url: ${get_base_url()}
headers:
Content-Type: application/json
device_sn: $device_sn

View File

@@ -4,17 +4,23 @@ import random
import string
import time
from tests.api_server import HTTPBIN_SERVER, SECRET_KEY, gen_md5, get_sign
from tests.api_server import HTTPBIN_SERVER, gen_md5, get_sign
BASE_URL = "http://127.0.0.1:5000"
def get_httpbin_server():
return HTTPBIN_SERVER
demo_default_request = {
"base_url": "$BASE_URL",
"headers": {
"content-type": "application/json"
def get_base_url():
return BASE_URL
def get_default_request():
return {
"base_url": BASE_URL,
"headers": {
"content-type": "application/json"
}
}
}
def sum_two(m, n):
return m + n

View File

@@ -1,7 +1,7 @@
- config:
name: basic test with httpbin
request:
base_url: $HTTPBIN_SERVER
base_url: ${get_httpbin_server()}
setup_hooks:
- ${hook_print(setup)}
teardown_hooks:
@@ -19,7 +19,7 @@
- ${teardown_hook_sleep_N_secs($response, 1)}
validate:
- eq: ["status_code", 200]
- contained_by: [content.headers.Host, $HTTPBIN_SERVER]
- contained_by: [content.headers.Host, "${get_httpbin_server()}"]
- test:
name: alter response

View File

@@ -1,7 +1,7 @@
- config:
name: load images
request:
base_url: $HTTPBIN_SERVER
base_url: ${get_httpbin_server()}
- test:
name: get png image

View File

@@ -1,7 +1,7 @@
- config:
name: test upload file with httpbin
request:
base_url: $HTTPBIN_SERVER
base_url: ${get_httpbin_server()}
- test:
name: upload file

View File

@@ -312,7 +312,7 @@ class TestHttpRunner(ApiServerUnittest):
summary = runner.summary
self.assertTrue(summary["success"])
self.assertIn("token", summary["details"][0]["in_out"]["out"])
self.assertGreater(len(summary["details"][0]["in_out"]["in"]), 7)
self.assertGreater(len(summary["details"][0]["in_out"]["in"]), 3)
def test_run_testcase_with_parameters(self):
testcase_file_path = os.path.join(

View File

@@ -10,11 +10,9 @@ class TestContext(ApiServerUnittest):
def setUp(self):
project_mapping = loader.load_project_tests(os.path.join(os.getcwd(), "tests"))
self.debugtalk_module = project_mapping["debugtalk"]
self.context = context.Context(
self.debugtalk_module["variables"],
self.debugtalk_module["functions"]
variables={"SECRET_KEY": "DebugTalk"},
functions=project_mapping["functions"]
)
testcase_file_path = os.path.join(os.getcwd(), 'tests/data/demo_binds.yml')
self.testcases = loader.load_file(testcase_file_path)

View File

@@ -191,57 +191,21 @@ class TestFileLoader(unittest.TestCase):
class TestModuleLoader(unittest.TestCase):
def test_filter_module_functions(self):
module_mapping = loader.load_python_module(loader)
functions_dict = module_mapping["functions"]
self.assertIn("load_python_module", functions_dict)
self.assertNotIn("is_py3", functions_dict)
module_functions = loader.load_module_functions(loader)
self.assertIn("load_module_functions", module_functions)
self.assertNotIn("is_py3", module_functions)
def test_load_debugtalk_module(self):
project_mapping = loader.load_project_tests(os.path.join(os.getcwd(), "httprunner"))
imported_module_items = project_mapping["debugtalk"]
self.assertNotIn("SECRET_KEY", imported_module_items["variables"])
self.assertNotIn("alter_response", imported_module_items["functions"])
self.assertNotIn("alter_response", project_mapping["functions"])
project_mapping = loader.load_project_tests(os.path.join(os.getcwd(), "tests"))
imported_module_items = project_mapping["debugtalk"]
self.assertEqual(
imported_module_items["variables"]["SECRET_KEY"],
"DebugTalk"
)
self.assertIn("alter_response", imported_module_items["functions"])
self.assertIn("alter_response", project_mapping["functions"])
is_status_code_200 = imported_module_items["functions"]["is_status_code_200"]
is_status_code_200 = project_mapping["functions"]["is_status_code_200"]
self.assertTrue(is_status_code_200(200))
self.assertFalse(is_status_code_200(500))
def test_get_module_item_functions(self):
from httprunner import utils
module_mapping = loader.load_python_module(utils)
get_uniform_comparator = loader.get_module_item(
module_mapping, "functions", "get_uniform_comparator")
self.assertTrue(validator.is_function(("get_uniform_comparator", get_uniform_comparator)))
self.assertEqual(get_uniform_comparator("=="), "equals")
with self.assertRaises(exceptions.FunctionNotFound):
loader.get_module_item(module_mapping, "functions", "gen_md4")
def test_get_module_item_variables(self):
dot_env_path = os.path.join(
os.getcwd(), "tests", ".env"
)
loader.load_dot_env_file(dot_env_path)
from tests import debugtalk
module_mapping = loader.load_python_module(debugtalk)
SECRET_KEY = loader.get_module_item(module_mapping, "variables", "SECRET_KEY")
self.assertTrue(validator.is_variable(("SECRET_KEY", SECRET_KEY)))
self.assertEqual(SECRET_KEY, "DebugTalk")
with self.assertRaises(exceptions.VariableNotFound):
loader.get_module_item(module_mapping, "variables", "SECRET_KEY2")
def test_locate_debugtalk_py(self):
debugtalk_path = loader.locate_debugtalk_py("tests/data/demo_testcase.yml")
self.assertEqual(
@@ -268,12 +232,12 @@ class TestModuleLoader(unittest.TestCase):
self.assertIsInstance(testcases, list)
self.assertEqual(
testcases[0]["config"]["request"],
'$demo_default_request'
'${get_default_request()}'
)
self.assertEqual(testcases[0]["config"]["name"], '123$var_a')
self.assertIn(
"sum_two",
testcases[0]["config"]["refs"]["debugtalk"]["functions"]
testcases[0]["config"]["refs"]["functions"]
)
@@ -427,22 +391,14 @@ class TestSuiteLoader(unittest.TestCase):
testcases_list = loader.load_tests(path)
self.assertEqual(len(testcases_list), 1)
self.assertEqual(len(testcases_list[0]["teststeps"]), 3)
self.assertEqual(
testcases_list[0]["config"]["refs"]["debugtalk"]["variables"]["SECRET_KEY"],
"DebugTalk"
)
self.assertIn("get_sign", testcases_list[0]["config"]["refs"]["debugtalk"]["functions"])
self.assertIn("get_sign", testcases_list[0]["config"]["refs"]["functions"])
# relative file path
path = 'tests/data/demo_testcase_hardcode.yml'
testcases_list = loader.load_tests(path)
self.assertEqual(len(testcases_list), 1)
self.assertEqual(len(testcases_list[0]["teststeps"]), 3)
self.assertEqual(
testcases_list[0]["config"]["refs"]["debugtalk"]["variables"]["SECRET_KEY"],
"DebugTalk"
)
self.assertIn("get_sign", testcases_list[0]["config"]["refs"]["debugtalk"]["functions"])
self.assertIn("get_sign", testcases_list[0]["config"]["refs"]["functions"])
# list/set container with file(s)
path = [
@@ -556,7 +512,6 @@ class TestSuiteLoader(unittest.TestCase):
def test_load_project_tests(self):
project_mapping = loader.load_project_tests(os.path.join(os.getcwd(), "tests"))
self.assertEqual(project_mapping["debugtalk"]["variables"]["SECRET_KEY"], "DebugTalk")
self.assertIn("get_token", project_mapping["def-api"])
self.assertIn("setup_and_reset", project_mapping["def-testcase"])
self.assertEqual(project_mapping["env"]["PROJECT_KEY"], "ABCDEFGH")

View File

@@ -389,11 +389,11 @@ class TestParser(unittest.TestCase):
)
loader.load_dot_env_file(dot_env_path)
from tests import debugtalk
debugtalk_module = loader.load_python_module(debugtalk)
functions = loader.load_module_functions(debugtalk)
cartesian_product_parameters = parser.parse_parameters(
parameters,
debugtalk_module["variables"],
debugtalk_module["functions"]
{},
functions
)
self.assertEqual(
len(cartesian_product_parameters),
@@ -424,7 +424,7 @@ class TestParser(unittest.TestCase):
{"username-password": "${parameterize(tests/data/account.csv)}"}
]
variables_mapping = {}
functions_mapping = project_mapping["debugtalk"]["functions"]
functions_mapping = project_mapping["functions"]
testcase_path = os.path.join(
os.getcwd(),
"tests/data/demo_parameters.yml"
@@ -445,11 +445,7 @@ class TestParser(unittest.TestCase):
self.assertEqual(len(parsed_testcases), 2 * 2)
self.assertEqual(
parsed_testcases[0]["config"]["request"]["base_url"],
'$BASE_URL'
)
self.assertEqual(
parsed_testcases[0]["config"]["variables"]["BASE_URL"],
'http://127.0.0.1:5000'
"http://127.0.0.1:5000"
)
self.assertIsInstance(parsed_testcases, list)
self.assertEqual(parsed_testcases[0]["config"]["name"], '12311')

View File

@@ -8,8 +8,7 @@ from tests.base import ApiServerUnittest
class TestResponse(ApiServerUnittest):
def setUp(self):
module_mapping = loader.load_python_module(built_in)
self.functions_mapping = module_mapping["functions"]
self.functions_mapping = loader.load_module_functions(built_in)
def test_parse_response_object_json(self):
url = "http://127.0.0.1:5000/api/users"

View File

@@ -11,10 +11,10 @@ class TestRunner(ApiServerUnittest):
def setUp(self):
project_mapping = loader.load_project_tests(os.path.join(os.getcwd(), "tests"))
self.debugtalk_module = project_mapping["debugtalk"]
self.debugtalk_functions = project_mapping["functions"]
config_dict = {
"variables": self.debugtalk_module["variables"],
"functions": self.debugtalk_module["functions"]
"variables": {},
"functions": self.debugtalk_functions
}
self.test_runner = runner.Runner(config_dict)
self.reset_all()
@@ -36,8 +36,8 @@ class TestRunner(ApiServerUnittest):
testcases = loader.load_file(testcase_file_path)
config_dict = {
"variables": self.debugtalk_module["variables"],
"functions": self.debugtalk_module["functions"]
"variables": {},
"functions": self.debugtalk_functions
}
test_runner = runner.Runner(config_dict)
@@ -81,8 +81,8 @@ class TestRunner(ApiServerUnittest):
config_dict = {
"name": "basic test with httpbin",
"variables": self.debugtalk_module["variables"],
"functions": self.debugtalk_module["functions"],
"variables": {},
"functions": self.debugtalk_functions,
"request": {
"base_url": HTTPBIN_SERVER
},
@@ -130,8 +130,8 @@ class TestRunner(ApiServerUnittest):
def test_run_testcase_with_hooks_modify_request(self):
config_dict = {
"name": "basic test with httpbin",
"variables": self.debugtalk_module["variables"],
"functions": self.debugtalk_module["functions"],
"variables": {},
"functions": self.debugtalk_functions,
"request": {
"base_url": HTTPBIN_SERVER
}

View File

@@ -101,8 +101,7 @@ class TestUtils(ApiServerUnittest):
def current_validators(self):
from httprunner import built_in
module_mapping = loader.load_python_module(built_in)
functions_mapping = module_mapping["functions"]
functions_mapping = loader.load_module_functions(built_in)
functions_mapping["equals"](None, None)
functions_mapping["equals"](1, 1)