rename teststeps to tests

This commit is contained in:
debugtalk
2018-11-23 17:56:53 +08:00
parent 8f4500566d
commit 18875bc5f1
12 changed files with 271 additions and 271 deletions
+10 -10
View File
@@ -38,12 +38,12 @@ class HttpRunner(object):
unittest.TestSuite() unittest.TestSuite()
""" """
def _add_teststep(test_runner, teststep_dict): def _add_test(test_runner, test_dict):
""" add teststep to testcase. """ add test to testcase.
""" """
def test(self): def test(self):
try: try:
test_runner.run_test(teststep_dict) test_runner.run_test(test_dict)
except exceptions.MyBaseFailure as ex: except exceptions.MyBaseFailure as ex:
self.fail(str(ex)) self.fail(str(ex))
finally: finally:
@@ -53,7 +53,7 @@ class HttpRunner(object):
test_runner.http_client_session.init_meta_data() test_runner.http_client_session.init_meta_data()
# TODO: refactor # TODO: refactor
test.__doc__ = teststep_dict.get("name") or teststep_dict.get("config", {}).get("name") test.__doc__ = test_dict.get("name") or test_dict.get("config", {}).get("name")
return test return test
test_suite = unittest.TestSuite() test_suite = unittest.TestSuite()
@@ -64,18 +64,18 @@ class HttpRunner(object):
test_runner = runner.Runner(config, functions, self.http_client_session) test_runner = runner.Runner(config, functions, self.http_client_session)
TestSequense = type('TestSequense', (unittest.TestCase,), {}) TestSequense = type('TestSequense', (unittest.TestCase,), {})
teststeps = testcase.get("teststeps", []) tests = testcase.get("tests", [])
for index, teststep_dict in enumerate(teststeps): for index, test_dict in enumerate(tests):
for times_index in range(int(teststep_dict.get("times", 1))): for times_index in range(int(test_dict.get("times", 1))):
# suppose one testcase should not have more than 9999 steps, # suppose one testcase should not have more than 9999 steps,
# and one step should not run more than 999 times. # and one step should not run more than 999 times.
test_method_name = 'test_{:04}_{:03}'.format(index, times_index) test_method_name = 'test_{:04}_{:03}'.format(index, times_index)
test_method = _add_teststep(test_runner, teststep_dict) test_method = _add_test(test_runner, test_dict)
setattr(TestSequense, test_method_name, test_method) setattr(TestSequense, test_method_name, test_method)
loaded_testcase = self.test_loader.loadTestsFromTestCase(TestSequense) loaded_testcase = self.test_loader.loadTestsFromTestCase(TestSequense)
setattr(loaded_testcase, "config", config) setattr(loaded_testcase, "config", config)
setattr(loaded_testcase, "teststeps", teststeps) setattr(loaded_testcase, "tests", tests)
setattr(loaded_testcase, "runner", test_runner) setattr(loaded_testcase, "runner", test_runner)
test_suite.addTest(loaded_testcase) test_suite.addTest(loaded_testcase)
@@ -221,7 +221,7 @@ def prepare_locust_tests(path):
functions = tests_mapping.get("project_mapping", {}).get("functions", {}) functions = tests_mapping.get("project_mapping", {}).get("functions", {})
testcase = tests_mapping["testcases"][0] testcase = tests_mapping["testcases"][0]
items = testcase.get("teststeps", []) items = testcase.get("tests", [])
tests = [] tests = []
for item in items: for item in items:
+11 -11
View File
@@ -17,11 +17,11 @@ class SessionContext(object):
def __init__(self, functions, variables=None): def __init__(self, functions, variables=None):
self.session_variables_mapping = utils.ensure_mapping_format(variables or {}) self.session_variables_mapping = utils.ensure_mapping_format(variables or {})
self.FUNCTIONS_MAPPING = functions self.FUNCTIONS_MAPPING = functions
self.teststep_variables_mapping = {} self.test_variables_mapping = {}
self.init_teststep_variables() self.init_test_variables()
def init_teststep_variables(self, variables_mapping=None): def init_test_variables(self, variables_mapping=None):
""" init teststep variables, called when each teststep(api) starts. """ init test variables, called when each test(api) starts.
variables_mapping will be evaluated first. variables_mapping will be evaluated first.
Args: Args:
@@ -38,14 +38,14 @@ class SessionContext(object):
variables_mapping = utils.ensure_mapping_format(variables_mapping) variables_mapping = utils.ensure_mapping_format(variables_mapping)
for variable_name, variable_value in variables_mapping.items(): for variable_name, variable_value in variables_mapping.items():
variable_value = self.eval_content(variable_value) variable_value = self.eval_content(variable_value)
self.update_teststep_variables(variable_name, variable_value) self.update_test_variables(variable_name, variable_value)
self.teststep_variables_mapping.update(self.session_variables_mapping) self.test_variables_mapping.update(self.session_variables_mapping)
def update_teststep_variables(self, variable_name, variable_value): def update_test_variables(self, variable_name, variable_value):
""" update teststep variables, these variables are only valid in the current teststep. """ update test variables, these variables are only valid in the current test.
""" """
self.teststep_variables_mapping[variable_name] = variable_value self.test_variables_mapping[variable_name] = variable_value
def update_seesion_variables(self, variables_mapping): def update_seesion_variables(self, variables_mapping):
""" update session with extracted variables mapping. """ update session with extracted variables mapping.
@@ -53,7 +53,7 @@ class SessionContext(object):
""" """
variables_mapping = utils.ensure_mapping_format(variables_mapping) variables_mapping = utils.ensure_mapping_format(variables_mapping)
self.session_variables_mapping.update(variables_mapping) self.session_variables_mapping.update(variables_mapping)
self.teststep_variables_mapping.update(self.session_variables_mapping) self.test_variables_mapping.update(self.session_variables_mapping)
def eval_content(self, content): def eval_content(self, content):
""" evaluate content recursively, take effect on each variable and function in content. """ evaluate content recursively, take effect on each variable and function in content.
@@ -61,7 +61,7 @@ class SessionContext(object):
""" """
return parser.parse_data( return parser.parse_data(
content, content,
self.teststep_variables_mapping, self.test_variables_mapping,
self.FUNCTIONS_MAPPING self.FUNCTIONS_MAPPING
) )
+22 -22
View File
@@ -279,11 +279,11 @@ tests_def_mapping = {
"testcases": {} "testcases": {}
} }
def load_teststep(raw_stepinfo): def load_test(raw_testinfo):
""" load teststep with api/testcase/proc references """ load test with api/testcase/proc references
Args: Args:
raw_stepinfo (dict): teststep data, maybe in 3 formats. raw_testinfo (dict): test data, maybe in 3 formats.
# api reference # api reference
{ {
"name": "add product to cart", "name": "add product to cart",
@@ -308,24 +308,24 @@ def load_teststep(raw_stepinfo):
} }
Returns: Returns:
list: loaded teststeps list list: loaded tests list
Args: Args:
raw_stepinfo (dict): teststep info raw_testinfo (dict): test info
""" """
# reference api # reference api
if "api" in raw_stepinfo: if "api" in raw_testinfo:
api_name = raw_stepinfo["api"] api_name = raw_testinfo["api"]
raw_stepinfo["api_def"] = _get_api_definition(api_name) raw_testinfo["api_def"] = _get_api_definition(api_name)
# TODO: reference proc functions # TODO: reference proc functions
elif "func" in raw_stepinfo: elif "func" in raw_testinfo:
pass pass
# reference testcase # reference testcase
elif "testcase" in raw_stepinfo: elif "testcase" in raw_testinfo:
testcase_path = raw_stepinfo["testcase"] testcase_path = raw_testinfo["testcase"]
if testcase_path not in tests_def_mapping["testcases"]: if testcase_path not in tests_def_mapping["testcases"]:
testcase_path = os.path.join( testcase_path = os.path.join(
@@ -337,13 +337,13 @@ def load_teststep(raw_stepinfo):
else: else:
testcase_dict = tests_def_mapping[testcase_path] testcase_dict = tests_def_mapping[testcase_path]
raw_stepinfo["testcase_def"] = testcase_dict raw_testinfo["testcase_def"] = testcase_dict
# define directly # define directly
else: else:
pass pass
return raw_stepinfo return raw_testinfo
def load_testcase(raw_testcase): def load_testcase(raw_testcase):
@@ -360,7 +360,7 @@ def load_testcase(raw_testcase):
"request": {} "request": {}
} }
}, },
# teststeps part # tests part
{ {
"test": {...} "test": {...}
}, },
@@ -374,12 +374,12 @@ def load_testcase(raw_testcase):
{ {
"name": "XYZ", "name": "XYZ",
"config": {}, "config": {},
"teststeps": [teststep11, teststep12] "tests": [test11, test12]
} }
""" """
config = {} config = {}
teststeps = [] tests = []
for item in raw_testcase: for item in raw_testcase:
# TODO: add json schema validation # TODO: add json schema validation
@@ -394,7 +394,7 @@ def load_testcase(raw_testcase):
config.update(test_block) config.update(test_block)
elif key == "test": elif key == "test":
teststeps.append(load_teststep(test_block)) tests.append(load_test(test_block))
else: else:
logger.log_warning( logger.log_warning(
@@ -403,7 +403,7 @@ def load_testcase(raw_testcase):
return { return {
"config": config, "config": config,
"teststeps": teststeps "tests": tests
} }
@@ -601,16 +601,16 @@ def load_tests(path, dot_env_path=None):
"path": "testcase1_path", "path": "testcase1_path",
"variables": [], # optional "variables": [], # optional
}, },
"teststeps": [ "tests": [
# teststep data structure # test data structure
{ {
'name': 'test step desc1', 'name': 'test desc1',
'variables': [], # optional 'variables': [], # optional
'extract': [], # optional 'extract': [], # optional
'validate': [], 'validate': [],
'request': {} 'request': {}
}, },
teststep2 # another teststep dict test_dict_2 # another test dict
] ]
}, },
testcase_dict_2 # another testcase dict testcase_dict_2 # another testcase dict
+90 -90
View File
@@ -542,15 +542,15 @@ def parse_data(content, variables_mapping=None, functions_mapping=None):
return content return content
def _extend_with_api(teststep_dict, api_def_dict): def _extend_with_api(test_dict, api_def_dict):
""" extend teststep with api definition, teststep will merge and override api definition. """ extend test with api definition, test will merge and override api definition.
Args: Args:
teststep_dict (dict): teststep block test_dict (dict): test block
api_def_dict (dict): api definition api_def_dict (dict): api definition
Returns: Returns:
dict: extended teststep dict. dict: extended test dict.
Examples: Examples:
>>> api_def_dict = { >>> api_def_dict = {
@@ -558,12 +558,12 @@ def _extend_with_api(teststep_dict, api_def_dict):
"request": {...}, "request": {...},
"validate": [{'eq': ['status_code', 200]}] "validate": [{'eq': ['status_code', 200]}]
} }
>>> teststep_dict = { >>> test_dict = {
"name": "get token 2", "name": "get token 2",
"extract": [{"token": "content.token"}], "extract": [{"token": "content.token"}],
"validate": [{'eq': ['status_code', 201]}, {'len_eq': ['content.token', 16]}] "validate": [{'eq': ['status_code', 201]}, {'len_eq': ['content.token', 16]}]
} }
>>> _extend_with_api(teststep_dict, api_def_dict) >>> _extend_with_api(test_dict, api_def_dict)
{ {
"name": "get token 2", "name": "get token 2",
"request": {...}, "request": {...},
@@ -574,18 +574,18 @@ def _extend_with_api(teststep_dict, api_def_dict):
""" """
# override name # override name
api_def_name = api_def_dict.pop("name", "") api_def_name = api_def_dict.pop("name", "")
teststep_dict["name"] = teststep_dict.get("name") or api_def_name test_dict["name"] = test_dict.get("name") or api_def_name
# override variables # override variables
def_variables = api_def_dict.pop("variables", []) def_variables = api_def_dict.pop("variables", [])
teststep_dict["variables"] = utils.extend_variables( test_dict["variables"] = utils.extend_variables(
def_variables, def_variables,
teststep_dict.get("variables", {}) test_dict.get("variables", {})
) )
# merge & override validators TODO: relocate # merge & override validators TODO: relocate
def_raw_validators = api_def_dict.pop("validate", []) def_raw_validators = api_def_dict.pop("validate", [])
ref_raw_validators = teststep_dict.get("validate", []) ref_raw_validators = test_dict.get("validate", [])
def_validators = [ def_validators = [
parse_validator(validator) parse_validator(validator)
for validator in def_raw_validators for validator in def_raw_validators
@@ -594,86 +594,86 @@ def _extend_with_api(teststep_dict, api_def_dict):
parse_validator(validator) parse_validator(validator)
for validator in ref_raw_validators for validator in ref_raw_validators
] ]
teststep_dict["validate"] = utils.extend_validators( test_dict["validate"] = utils.extend_validators(
def_validators, def_validators,
ref_validators ref_validators
) )
# merge & override extractors # merge & override extractors
def_extrators = api_def_dict.pop("extract", []) def_extrators = api_def_dict.pop("extract", [])
teststep_dict["extract"] = utils.extend_variables( test_dict["extract"] = utils.extend_variables(
def_extrators, def_extrators,
teststep_dict.get("extract", []) test_dict.get("extract", [])
) )
# TODO: merge & override request # TODO: merge & override request
teststep_dict["request"] = api_def_dict.pop("request", {}) test_dict["request"] = api_def_dict.pop("request", {})
# base_url # base_url
if "base_url" in teststep_dict: if "base_url" in test_dict:
base_url = teststep_dict.pop("base_url") base_url = test_dict.pop("base_url")
teststep_dict["request"]["url"] = utils.build_url( test_dict["request"]["url"] = utils.build_url(
base_url, base_url,
teststep_dict["request"]["url"] test_dict["request"]["url"]
) )
# verify # verify
if "verify" in teststep_dict: if "verify" in test_dict:
verify = teststep_dict.pop("verify") verify = test_dict.pop("verify")
elif "verify" in api_def_dict: elif "verify" in api_def_dict:
verify = api_def_dict.pop("verify") verify = api_def_dict.pop("verify")
else: else:
verify = True verify = True
teststep_dict["request"]["verify"] = verify test_dict["request"]["verify"] = verify
# merge & override setup_hooks # merge & override setup_hooks
def_setup_hooks = api_def_dict.pop("setup_hooks", []) def_setup_hooks = api_def_dict.pop("setup_hooks", [])
ref_setup_hooks = teststep_dict.get("setup_hooks", []) ref_setup_hooks = test_dict.get("setup_hooks", [])
extended_setup_hooks = list(set(def_setup_hooks + ref_setup_hooks)) extended_setup_hooks = list(set(def_setup_hooks + ref_setup_hooks))
if extended_setup_hooks: if extended_setup_hooks:
teststep_dict["setup_hooks"] = extended_setup_hooks test_dict["setup_hooks"] = extended_setup_hooks
# merge & override teardown_hooks # merge & override teardown_hooks
def_teardown_hooks = api_def_dict.pop("teardown_hooks", []) def_teardown_hooks = api_def_dict.pop("teardown_hooks", [])
ref_teardown_hooks = teststep_dict.get("teardown_hooks", []) ref_teardown_hooks = test_dict.get("teardown_hooks", [])
extended_teardown_hooks = list(set(def_teardown_hooks + ref_teardown_hooks)) extended_teardown_hooks = list(set(def_teardown_hooks + ref_teardown_hooks))
if extended_teardown_hooks: if extended_teardown_hooks:
teststep_dict["teardown_hooks"] = extended_teardown_hooks test_dict["teardown_hooks"] = extended_teardown_hooks
# TODO: extend with other api definition items, e.g. times # TODO: extend with other api definition items, e.g. times
teststep_dict.update(api_def_dict) test_dict.update(api_def_dict)
return teststep_dict return test_dict
def _extend_with_testcase(teststep_dict, testcase_def_dict): def _extend_with_testcase(test_dict, testcase_def_dict):
""" extend teststep with testcase definition """ extend test with testcase definition
teststep will merge and override testcase config definition. test will merge and override testcase config definition.
Args: Args:
teststep_dict (dict): teststep block test_dict (dict): test block
testcase_def_dict (dict): testcase definition testcase_def_dict (dict): testcase definition
Returns: Returns:
dict: extended teststep dict. dict: extended test dict.
""" """
# override testcase config variables # override testcase config variables
testcase_def_dict["config"].setdefault("variables", {}) testcase_def_dict["config"].setdefault("variables", {})
testcase_def_variables = utils.ensure_mapping_format(testcase_def_dict["config"].get("variables", {})) testcase_def_variables = utils.ensure_mapping_format(testcase_def_dict["config"].get("variables", {}))
testcase_def_variables.update(teststep_dict.pop("variables", {})) testcase_def_variables.update(test_dict.pop("variables", {}))
testcase_def_dict["config"]["variables"] = testcase_def_variables testcase_def_dict["config"]["variables"] = testcase_def_variables
# override base_url, verify # override base_url, verify
# priority: testcase config > testsuite teststep # priority: testcase config > testsuite tests
teststep_base_url = teststep_dict.pop("base_url", None) test_base_url = test_dict.pop("base_url", None)
teststep_verify = teststep_dict.pop("verify", True) test_verify = test_dict.pop("verify", True)
testcase_def_dict["config"].setdefault("base_url", teststep_base_url) testcase_def_dict["config"].setdefault("base_url", test_base_url)
testcase_def_dict["config"].setdefault("verify", teststep_verify) testcase_def_dict["config"].setdefault("verify", test_verify)
# override testcase config name, output, etc. # override testcase config name, output, etc.
testcase_def_dict["config"].update(teststep_dict) testcase_def_dict["config"].update(test_dict)
teststep_dict.clear() test_dict.clear()
teststep_dict.update(testcase_def_dict) test_dict.update(testcase_def_dict)
def __parse_config(config, project_mapping): def __parse_config(config, project_mapping):
@@ -722,22 +722,22 @@ def __parse_config(config, project_mapping):
) )
def __parse_teststeps(teststeps, config, project_mapping): def __parse_tests(tests, config, project_mapping):
""" override teststeps with testcase config variables, base_url and verify. """ override tests with testcase config variables, base_url and verify.
teststep maybe nested testcase. test maybe nested testcase.
variables priority: variables priority:
testsuite config > testsuite teststep > testcase config > testcase teststep > api testsuite config > testsuite test > testcase config > testcase test > api
base_url/verify priority: base_url/verify priority:
testcase teststep > testcase config > testsuite teststep > testsuite config > api testcase test > testcase config > testsuite test > testsuite config > api
Args: Args:
teststeps (list): tests (list):
config (dict): config (dict):
Returns: Returns:
list: overrided teststeps list: overrided tests
""" """
config_variables = config.pop("variables", {}) config_variables = config.pop("variables", {})
@@ -745,80 +745,80 @@ def __parse_teststeps(teststeps, config, project_mapping):
config_verify = config.pop("verify", True) config_verify = config.pop("verify", True)
functions = project_mapping.get("functions", {}) functions = project_mapping.get("functions", {})
for teststep in teststeps: for test_dict in tests:
# base_url & verify: priority teststep > config # base_url & verify: priority test_dict > config
if config_base_url: if config_base_url:
teststep.setdefault("base_url", config_base_url) test_dict.setdefault("base_url", config_base_url)
teststep.setdefault("verify", config_verify) test_dict.setdefault("verify", config_verify)
if "testcase_def" in teststep: if "testcase_def" in test_dict:
# teststep is nested testcase # test_dict is nested testcase
# 1, testsuite config => testsuite teststeps # 1, testsuite config => testsuite tests
# override teststep variables # override test_dict variables
teststep["variables"] = utils.extend_variables( test_dict["variables"] = utils.extend_variables(
teststep.pop("variables", {}), test_dict.pop("variables", {}),
config_variables config_variables
) )
# parse teststep name # parse test_dict name
try: try:
teststep["name"] = parse_data( test_dict["name"] = parse_data(
teststep.pop("name", ""), test_dict.pop("name", ""),
teststep["variables"], test_dict["variables"],
functions functions
) )
except exceptions.VariableNotFound: except exceptions.VariableNotFound:
pass pass
# 2, testsuite teststep => testcase config # 2, testsuite test_dict => testcase config
testcase_def = teststep.pop("testcase_def") testcase_def = test_dict.pop("testcase_def")
_extend_with_testcase(teststep, testcase_def) _extend_with_testcase(test_dict, testcase_def)
# 3, testcase config => testcase teststep # 3, testcase config => testcase test_dict
_parse_testcase(teststep, project_mapping) _parse_testcase(test_dict, project_mapping)
else: else:
# teststep is API test, has two cases. # test_dict is API test, has two cases.
# (1) teststep has API reference # (1) test_dict has API reference
# (2) teststep is defined directly # (2) test_dict is defined directly
# 1, config => teststeps # 1, config => tests
# override teststep variables # override test_dict variables
teststep["variables"] = utils.extend_variables( test_dict["variables"] = utils.extend_variables(
teststep.pop("variables", {}), test_dict.pop("variables", {}),
config_variables config_variables
) )
# parse teststep name # parse test_dict name
try: try:
teststep["name"] = parse_data( test_dict["name"] = parse_data(
teststep.pop("name", ""), test_dict.pop("name", ""),
teststep["variables"], test_dict["variables"],
functions functions
) )
except exceptions.VariableNotFound: except exceptions.VariableNotFound:
pass pass
if "api_def" in teststep: if "api_def" in test_dict:
# case (1) # case (1)
# 2, teststep => api # 2, test_dict => api
api_def_dict = teststep.pop("api_def") api_def_dict = test_dict.pop("api_def")
_extend_with_api(teststep, api_def_dict) _extend_with_api(test_dict, api_def_dict)
else: else:
# case (2) # case (2)
if "base_url" in teststep: if "base_url" in test_dict:
base_url = teststep.pop("base_url") base_url = test_dict.pop("base_url")
teststep["request"]["url"] = utils.build_url( test_dict["request"]["url"] = utils.build_url(
base_url, base_url,
teststep["request"]["url"] test_dict["request"]["url"]
) )
def _parse_testcase(testcase, project_mapping): def _parse_testcase(testcase, project_mapping):
__parse_config(testcase["config"], project_mapping) __parse_config(testcase["config"], project_mapping)
__parse_teststeps(testcase["teststeps"], testcase["config"], project_mapping) __parse_tests(testcase["tests"], testcase["config"], project_mapping)
def parse_tests(tests_mapping): def parse_tests(tests_mapping):
@@ -841,8 +841,8 @@ def parse_tests(tests_mapping):
"path": "testcase1_path", "path": "testcase1_path",
"variables": [], # optional, priority 2 "variables": [], # optional, priority 2
}, },
"teststeps": [ "tests": [
# teststep data structure # test data structure
{ {
'name': 'test step desc1', 'name': 'test step desc1',
'variables': [], # optional, priority 3 'variables': [], # optional, priority 3
@@ -853,7 +853,7 @@ def parse_tests(tests_mapping):
'request': {}, 'request': {},
} }
}, },
teststep2 # another teststep dict test_dict_2 # another test dict
] ]
}, },
testcase_dict_2 # another testcase dict testcase_dict_2 # another testcase dict
+46 -46
View File
@@ -19,15 +19,15 @@ class Runner(object):
} }
>>> runner = Runner(config, functions) >>> runner = Runner(config, functions)
>>> teststep = { >>> test_dict = {
"name": "teststep description", "name": "test description",
"variables": [], # optional "variables": [], # optional
"request": { "request": {
"url": "http://127.0.0.1:5000/api/users/1000", "url": "http://127.0.0.1:5000/api/users/1000",
"method": "GET" "method": "GET"
} }
} }
>>> runner.run_test(teststep) >>> runner.run_test(test_dict)
""" """
@@ -68,32 +68,32 @@ class Runner(object):
if self.testcase_teardown_hooks: if self.testcase_teardown_hooks:
self.do_hook_actions(self.testcase_teardown_hooks) self.do_hook_actions(self.testcase_teardown_hooks)
def _handle_skip_feature(self, teststep_dict): def _handle_skip_feature(self, test_dict):
""" handle skip feature for teststep """ handle skip feature for test
- skip: skip current test unconditionally - skip: skip current test unconditionally
- skipIf: skip current test if condition is true - skipIf: skip current test if condition is true
- skipUnless: skip current test unless condition is true - skipUnless: skip current test unless condition is true
Args: Args:
teststep_dict (dict): teststep info test_dict (dict): test info
Raises: Raises:
SkipTest: skip teststep SkipTest: skip test
""" """
# TODO: move skip to initialize # TODO: move skip to initialize
skip_reason = None skip_reason = None
if "skip" in teststep_dict: if "skip" in test_dict:
skip_reason = teststep_dict["skip"] skip_reason = test_dict["skip"]
elif "skipIf" in teststep_dict: elif "skipIf" in test_dict:
skip_if_condition = teststep_dict["skipIf"] skip_if_condition = test_dict["skipIf"]
if self.session_context.eval_content(skip_if_condition): if self.session_context.eval_content(skip_if_condition):
skip_reason = "{} evaluate to True".format(skip_if_condition) skip_reason = "{} evaluate to True".format(skip_if_condition)
elif "skipUnless" in teststep_dict: elif "skipUnless" in test_dict:
skip_unless_condition = teststep_dict["skipUnless"] skip_unless_condition = test_dict["skipUnless"]
if not self.session_context.eval_content(skip_unless_condition): if not self.session_context.eval_content(skip_unless_condition):
skip_reason = "{} evaluate to False".format(skip_unless_condition) skip_reason = "{} evaluate to False".format(skip_unless_condition)
@@ -106,11 +106,11 @@ class Runner(object):
# TODO: check hook function if valid # TODO: check hook function if valid
self.session_context.eval_content(action) self.session_context.eval_content(action)
def _run_teststep(self, teststep_dict): def _run_test(self, test_dict):
""" run single teststep. """ run single teststep.
Args: Args:
teststep_dict (dict): teststep info test_dict (dict): teststep info
{ {
"name": "teststep description", "name": "teststep description",
"skip": "skip this test unconditionally", "skip": "skip this test unconditionally",
@@ -139,28 +139,28 @@ class Runner(object):
""" """
# check skip # check skip
self._handle_skip_feature(teststep_dict) self._handle_skip_feature(test_dict)
# prepare # prepare
teststep_dict = utils.lower_test_dict_keys(teststep_dict) test_dict = utils.lower_test_dict_keys(test_dict)
teststep_variables = teststep_dict.get("variables", {}) test_variables = test_dict.get("variables", {})
self.session_context.init_teststep_variables(teststep_variables) self.session_context.init_test_variables(test_variables)
# parse teststep request # parse test request
raw_request = teststep_dict.get('request', {}) raw_request = test_dict.get('request', {})
parsed_teststep_request = self.session_context.eval_content(raw_request) parsed_test_request = self.session_context.eval_content(raw_request)
self.session_context.update_teststep_variables("request", parsed_teststep_request) self.session_context.update_test_variables("request", parsed_test_request)
# setup hooks # setup hooks
setup_hooks = teststep_dict.get("setup_hooks", []) setup_hooks = test_dict.get("setup_hooks", [])
setup_hooks.insert(0, "${setup_hook_prepare_kwargs($request)}") setup_hooks.insert(0, "${setup_hook_prepare_kwargs($request)}")
self.do_hook_actions(setup_hooks) self.do_hook_actions(setup_hooks)
try: try:
url = parsed_teststep_request.pop('url') url = parsed_test_request.pop('url')
method = parsed_teststep_request.pop('method') method = parsed_test_request.pop('method')
parsed_teststep_request.setdefault("verify", self.verify) parsed_test_request.setdefault("verify", self.verify)
group_name = parsed_teststep_request.pop("group", None) group_name = parsed_test_request.pop("group", None)
except KeyError: except KeyError:
raise exceptions.ParamsError("URL or METHOD missed!") raise exceptions.ParamsError("URL or METHOD missed!")
@@ -173,38 +173,38 @@ class Runner(object):
raise exceptions.ParamsError(err_msg) raise exceptions.ParamsError(err_msg)
logger.log_info("{method} {url}".format(method=method, url=url)) logger.log_info("{method} {url}".format(method=method, url=url))
logger.log_debug("request kwargs(raw): {kwargs}".format(kwargs=parsed_teststep_request)) logger.log_debug("request kwargs(raw): {kwargs}".format(kwargs=parsed_test_request))
# request # request
resp = self.http_client_session.request( resp = self.http_client_session.request(
method, method,
url, url,
name=group_name, name=group_name,
**parsed_teststep_request **parsed_test_request
) )
resp_obj = response.ResponseObject(resp) resp_obj = response.ResponseObject(resp)
# teardown hooks # teardown hooks
teardown_hooks = teststep_dict.get("teardown_hooks", []) teardown_hooks = test_dict.get("teardown_hooks", [])
if teardown_hooks: if teardown_hooks:
logger.log_info("start to run teardown hooks") logger.log_info("start to run teardown hooks")
self.session_context.update_teststep_variables("response", resp_obj) self.session_context.update_test_variables("response", resp_obj)
self.do_hook_actions(teardown_hooks) self.do_hook_actions(teardown_hooks)
# extract # extract
extractors = teststep_dict.get("extract", []) extractors = test_dict.get("extract", [])
extracted_variables_mapping = resp_obj.extract_response(extractors) extracted_variables_mapping = resp_obj.extract_response(extractors)
self.session_context.update_seesion_variables(extracted_variables_mapping) self.session_context.update_seesion_variables(extracted_variables_mapping)
# validate # validate
validators = teststep_dict.get("validate", []) validators = test_dict.get("validate", [])
try: try:
self.evaluated_validators = self.session_context.validate(validators, resp_obj) self.evaluated_validators = self.session_context.validate(validators, resp_obj)
except (exceptions.ParamsError, exceptions.ValidationFailure, exceptions.ExtractFailure): except (exceptions.ParamsError, exceptions.ValidationFailure, exceptions.ExtractFailure):
# log request # log request
err_req_msg = "request: \n" err_req_msg = "request: \n"
err_req_msg += "headers: {}\n".format(parsed_teststep_request.pop("headers", {})) err_req_msg += "headers: {}\n".format(parsed_test_request.pop("headers", {}))
for k, v in parsed_teststep_request.items(): for k, v in parsed_test_request.items():
err_req_msg += "{}: {}\n".format(k, repr(v)) err_req_msg += "{}: {}\n".format(k, repr(v))
logger.log_error(err_req_msg) logger.log_error(err_req_msg)
@@ -223,18 +223,18 @@ class Runner(object):
config = testcase_dict.get("config", {}) config = testcase_dict.get("config", {})
test_runner = Runner(config, self.functions, self.http_client_session) test_runner = Runner(config, self.functions, self.http_client_session)
teststeps = testcase_dict.get("teststeps", []) tests = testcase_dict.get("tests", [])
for index, teststep_dict in enumerate(teststeps): for index, test_dict in enumerate(tests):
test_runner.run_test(teststep_dict) test_runner.run_test(test_dict)
self.session_context.update_seesion_variables(test_runner.extract_sessions()) self.session_context.update_seesion_variables(test_runner.extract_sessions())
def run_test(self, teststep_dict): def run_test(self, test_dict):
""" run single teststep of testcase. """ run single teststep of testcase.
teststep_dict may be in 3 types. test_dict may be in 3 types.
Args: Args:
teststep_dict (dict): test_dict (dict):
# teststep # teststep
{ {
@@ -249,7 +249,7 @@ class Runner(object):
# embeded testcase # embeded testcase
{ {
"config": {...}, "config": {...},
"teststeps": [ "tests": [
{...}, {...},
{...} {...}
] ]
@@ -262,11 +262,11 @@ class Runner(object):
} }
""" """
if "config" in teststep_dict: if "config" in test_dict:
self._run_testcase(teststep_dict) self._run_testcase(test_dict)
else: else:
# api # api
self._run_teststep(teststep_dict) self._run_test(test_dict)
def extract_sessions(self): def extract_sessions(self):
""" """
+8 -8
View File
@@ -19,9 +19,9 @@ def is_testcase(data_structure):
"variables": [], # optional "variables": [], # optional
"request": {} # optional "request": {} # optional
}, },
"teststeps": [ "tests": [
teststep1, test_dict1,
{ # teststep2 { # test_dict2
'name': 'test step desc2', 'name': 'test step desc2',
'variables': [], # optional 'variables': [], # optional
'extract': [], # optional 'extract': [], # optional
@@ -40,10 +40,10 @@ def is_testcase(data_structure):
if not isinstance(data_structure, dict): if not isinstance(data_structure, dict):
return False return False
if "teststeps" not in data_structure: if "tests" not in data_structure:
return False return False
if not isinstance(data_structure["teststeps"], list): if not isinstance(data_structure["tests"], list):
return False return False
return True return True
@@ -67,8 +67,8 @@ def is_testcases(data_structure):
"path": "testcase1_path", "path": "testcase1_path",
"variables": [], # optional "variables": [], # optional
}, },
"teststeps": [ "tests": [
# teststep data structure # test data structure
{ {
'name': 'test step desc1', 'name': 'test step desc1',
'variables': [], # optional 'variables': [], # optional
@@ -76,7 +76,7 @@ def is_testcases(data_structure):
'validate': [], 'validate': [],
'request': {} 'request': {}
}, },
teststep2 # another teststep dict test_dict_2 # another test dict
] ]
}, },
testcase_dict_2 # another testcase dict testcase_dict_2 # another testcase dict
+43 -43
View File
@@ -28,7 +28,7 @@ class TestHttpRunner(ApiServerUnittest):
}, },
'variables': [] 'variables': []
}, },
'teststeps': [ 'tests': [
{ {
'name': '/api/get-token', 'name': '/api/get-token',
'request': { 'request': {
@@ -119,7 +119,7 @@ class TestHttpRunner(ApiServerUnittest):
}, },
'variables': [] 'variables': []
}, },
"teststeps": [ "tests": [
{ {
"name": "post data", "name": "post data",
"request": { "request": {
@@ -182,7 +182,7 @@ class TestHttpRunner(ApiServerUnittest):
testcases = [ testcases = [
{ {
"config": {"name": "test teardown hooks"}, "config": {"name": "test teardown hooks"},
"teststeps": [ "tests": [
{ {
"name": "test teardown hooks", "name": "test teardown hooks",
"request": { "request": {
@@ -222,7 +222,7 @@ class TestHttpRunner(ApiServerUnittest):
"config": { "config": {
"name": "test teardown hooks" "name": "test teardown hooks"
}, },
"teststeps": [ "tests": [
{ {
"name": "test teardown hooks", "name": "test teardown hooks",
"request": { "request": {
@@ -256,7 +256,7 @@ class TestHttpRunner(ApiServerUnittest):
"config": { "config": {
"name": "test teardown hooks" "name": "test teardown hooks"
}, },
"teststeps": [ "tests": [
{ {
"name": "test teardown hooks", "name": "test teardown hooks",
"request": { "request": {
@@ -368,28 +368,28 @@ class TestHttpRunner(ApiServerUnittest):
test_suite = runner._add_tests(tests_mapping) test_suite = runner._add_tests(tests_mapping)
self.assertEqual( self.assertEqual(
test_suite._tests[0].teststeps[0]['name'], test_suite._tests[0].tests[0]['name'],
'get token with iOS/10.1 and test1' 'get token with iOS/10.1 and test1'
) )
# TODO: add parameterize # TODO: add parameterize
# self.assertEqual( # self.assertEqual(
# test_suite._tests[1].teststeps[0]['name'], # test_suite._tests[1].tests[0]['name'],
# 'get token with iOS/10.1 and test2' # 'get token with iOS/10.1 and test2'
# ) # )
# self.assertEqual( # self.assertEqual(
# test_suite._tests[2].teststeps[0]['name'], # test_suite._tests[2].tests[0]['name'],
# 'get token with iOS/10.2 and test1' # 'get token with iOS/10.2 and test1'
# ) # )
# self.assertEqual( # self.assertEqual(
# test_suite._tests[3].teststeps[0]['name'], # test_suite._tests[3].tests[0]['name'],
# 'get token with iOS/10.2 and test2' # 'get token with iOS/10.2 and test2'
# ) # )
# self.assertEqual( # self.assertEqual(
# test_suite._tests[4].teststeps[0]['name'], # test_suite._tests[4].tests[0]['name'],
# 'get token with iOS/10.3 and test1' # 'get token with iOS/10.3 and test1'
# ) # )
# self.assertEqual( # self.assertEqual(
# test_suite._tests[5].teststeps[0]['name'], # test_suite._tests[5].tests[0]['name'],
# 'get token with iOS/10.3 and test2' # 'get token with iOS/10.3 and test2'
# ) # )
@@ -419,13 +419,13 @@ class TestApi(ApiServerUnittest):
self.assertEqual(testcase_config["name"], "setup and reset all.") self.assertEqual(testcase_config["name"], "setup and reset all.")
self.assertIn("path", testcase_config) self.assertIn("path", testcase_config)
testcase_teststeps = testcases[0]["teststeps"] testcase_tests = testcases[0]["tests"]
self.assertEqual(len(testcase_teststeps), 2) self.assertEqual(len(testcase_tests), 2)
self.assertIn("api", testcase_teststeps[0]) self.assertIn("api", testcase_tests[0])
self.assertEqual(testcase_teststeps[0]["name"], "get token (setup)") self.assertEqual(testcase_tests[0]["name"], "get token (setup)")
self.assertIsInstance(testcase_teststeps[0]["variables"], list) self.assertIsInstance(testcase_tests[0]["variables"], list)
self.assertIn("api_def", testcase_teststeps[0]) self.assertIn("api_def", testcase_tests[0])
self.assertEqual(testcase_teststeps[0]["api_def"]["request"]["url"], "/api/get-token") self.assertEqual(testcase_tests[0]["api_def"]["request"]["url"], "/api/get-token")
def test_testcase_parser(self): def test_testcase_parser(self):
testcase_path = "tests/testcases/setup.yml" testcase_path = "tests/testcases/setup.yml"
@@ -437,13 +437,13 @@ class TestApi(ApiServerUnittest):
self.assertEqual(len(parsed_testcases), 1) self.assertEqual(len(parsed_testcases), 1)
self.assertNotIn("variables", parsed_testcases[0]["config"]) self.assertNotIn("variables", parsed_testcases[0]["config"])
self.assertEqual(len(parsed_testcases[0]["teststeps"]), 2) self.assertEqual(len(parsed_testcases[0]["tests"]), 2)
teststep1 = parsed_testcases[0]["teststeps"][0] test_dict1 = parsed_testcases[0]["tests"][0]
self.assertEqual(teststep1["name"], "get token (setup)") self.assertEqual(test_dict1["name"], "get token (setup)")
self.assertNotIn("api_def", teststep1) self.assertNotIn("api_def", test_dict1)
self.assertEqual(teststep1["variables"]["device_sn"], "TESTCASE_SETUP_XXX") self.assertEqual(test_dict1["variables"]["device_sn"], "TESTCASE_SETUP_XXX")
self.assertEqual(teststep1["request"]["url"], "http://127.0.0.1:5000/api/get-token") self.assertEqual(test_dict1["request"]["url"], "http://127.0.0.1:5000/api/get-token")
def test_testcase_add_tests(self): def test_testcase_add_tests(self):
testcase_path = "tests/testcases/setup.yml" testcase_path = "tests/testcases/setup.yml"
@@ -454,10 +454,10 @@ class TestApi(ApiServerUnittest):
test_suite = runner._add_tests(tests_mapping) test_suite = runner._add_tests(tests_mapping)
self.assertEqual(len(test_suite._tests), 1) self.assertEqual(len(test_suite._tests), 1)
teststeps = test_suite._tests[0].teststeps tests = test_suite._tests[0].tests
self.assertEqual(teststeps[0]["name"], "get token (setup)") self.assertEqual(tests[0]["name"], "get token (setup)")
self.assertEqual(teststeps[0]["variables"]["device_sn"], "TESTCASE_SETUP_XXX") self.assertEqual(tests[0]["variables"]["device_sn"], "TESTCASE_SETUP_XXX")
self.assertIn("api", teststeps[0]) self.assertIn("api", tests[0])
def test_testcase_simple_run_suite(self): def test_testcase_simple_run_suite(self):
testcase_path = "tests/testcases/setup.yml" testcase_path = "tests/testcases/setup.yml"
@@ -504,14 +504,14 @@ class TestApi(ApiServerUnittest):
self.assertEqual(testcase_config["name"], "create users with uid") self.assertEqual(testcase_config["name"], "create users with uid")
self.assertIn("path", testcase_config) self.assertIn("path", testcase_config)
testcase_teststeps = testcases[0]["teststeps"] testcase_tests = testcases[0]["tests"]
self.assertEqual(len(testcase_teststeps), 2) self.assertEqual(len(testcase_tests), 2)
self.assertIn("testcase_def", testcase_teststeps[0]) self.assertIn("testcase_def", testcase_tests[0])
self.assertEqual(testcase_teststeps[0]["name"], "create user 1000 and check result.") self.assertEqual(testcase_tests[0]["name"], "create user 1000 and check result.")
self.assertIsInstance(testcase_teststeps[0]["testcase_def"], dict) self.assertIsInstance(testcase_tests[0]["testcase_def"], dict)
self.assertEqual(testcase_teststeps[0]["testcase_def"]["config"]["name"], "create user and check result.") self.assertEqual(testcase_tests[0]["testcase_def"]["config"]["name"], "create user and check result.")
self.assertEqual(len(testcase_teststeps[0]["testcase_def"]["teststeps"]), 4) self.assertEqual(len(testcase_tests[0]["testcase_def"]["tests"]), 4)
self.assertEqual(testcase_teststeps[0]["testcase_def"]["teststeps"][0]["name"], "setup and reset all (override).") self.assertEqual(testcase_tests[0]["testcase_def"]["tests"][0]["name"], "setup and reset all (override).")
def test_testsuite_parser(self): def test_testsuite_parser(self):
testcase_path = "tests/testsuites/create_users.yml" testcase_path = "tests/testsuites/create_users.yml"
@@ -521,17 +521,17 @@ class TestApi(ApiServerUnittest):
parsed_testcases = tests_mapping["testcases"] parsed_testcases = tests_mapping["testcases"]
self.assertEqual(len(parsed_testcases), 1) self.assertEqual(len(parsed_testcases), 1)
self.assertEqual(len(parsed_testcases[0]["teststeps"]), 2) self.assertEqual(len(parsed_testcases[0]["tests"]), 2)
testcase1 = parsed_testcases[0]["teststeps"][0] testcase1 = parsed_testcases[0]["tests"][0]
self.assertEqual(testcase1["config"]["name"], "create user 1000 and check result.") self.assertEqual(testcase1["config"]["name"], "create user 1000 and check result.")
self.assertNotIn("testcase_def", testcase1) self.assertNotIn("testcase_def", testcase1)
self.assertEqual(len(testcase1["teststeps"]), 4) self.assertEqual(len(testcase1["tests"]), 4)
self.assertEqual( self.assertEqual(
testcase1["teststeps"][0]["teststeps"][0]["request"]["url"], testcase1["tests"][0]["tests"][0]["request"]["url"],
"http://127.0.0.1:5000/api/get-token" "http://127.0.0.1:5000/api/get-token"
) )
self.assertEqual(len(testcase1["teststeps"][0]["teststeps"][0]["variables"]["device_sn"]), 15) self.assertEqual(len(testcase1["tests"][0]["tests"][0]["variables"]["device_sn"]), 15)
def test_testsuite_add_tests(self): def test_testsuite_add_tests(self):
testcase_path = "tests/testsuites/create_users.yml" testcase_path = "tests/testsuites/create_users.yml"
@@ -542,8 +542,8 @@ class TestApi(ApiServerUnittest):
test_suite = runner._add_tests(tests_mapping) test_suite = runner._add_tests(tests_mapping)
self.assertEqual(len(test_suite._tests), 1) self.assertEqual(len(test_suite._tests), 1)
teststeps = test_suite._tests[0].teststeps tests = test_suite._tests[0].tests
self.assertEqual(teststeps[0]["config"]["name"], "create user 1000 and check result.") self.assertEqual(tests[0]["config"]["name"], "create user 1000 and check result.")
def test_testsuite_run_suite(self): def test_testsuite_run_suite(self):
testcase_path = "tests/testsuites/create_users.yml" testcase_path = "tests/testsuites/create_users.yml"
+7 -7
View File
@@ -22,9 +22,9 @@ class TestContext(ApiServerUnittest):
context_functions = self.context.FUNCTIONS_MAPPING context_functions = self.context.FUNCTIONS_MAPPING
self.assertIn("gen_md5", context_functions) self.assertIn("gen_md5", context_functions)
def test_init_teststep_variables(self): def test_init_test_variables(self):
self.assertEqual( self.assertEqual(
self.context.teststep_variables_mapping, self.context.test_variables_mapping,
{'SECRET_KEY': 'DebugTalk'} {'SECRET_KEY': 'DebugTalk'}
) )
@@ -63,7 +63,7 @@ class TestContext(ApiServerUnittest):
{"data": '{"name": "user", "password": "123456"}'}, {"data": '{"name": "user", "password": "123456"}'},
{"authorization": "${gen_md5($TOKEN, $data, $random)}"} {"authorization": "${gen_md5($TOKEN, $data, $random)}"}
] ]
self.context.init_teststep_variables(variables) self.context.init_test_variables(variables)
request = { request = {
"url": "http://127.0.0.1:5000/api/users/1000", "url": "http://127.0.0.1:5000/api/users/1000",
@@ -110,7 +110,7 @@ class TestContext(ApiServerUnittest):
{"resp_status_code": 200}, {"resp_status_code": 200},
{"resp_body_success": True} {"resp_body_success": True}
] ]
self.context.init_teststep_variables(variables) self.context.init_test_variables(variables)
with self.assertRaises(exceptions.ValidationFailure): with self.assertRaises(exceptions.ValidationFailure):
self.context.validate(validators, resp_obj) self.context.validate(validators, resp_obj)
@@ -125,7 +125,7 @@ class TestContext(ApiServerUnittest):
{"resp_status_code": 201}, {"resp_status_code": 201},
{"resp_body_success": True} {"resp_body_success": True}
] ]
self.context.init_teststep_variables(variables) self.context.init_test_variables(variables)
self.context.validate(validators, resp_obj) self.context.validate(validators, resp_obj)
def test_validate_exception(self): def test_validate_exception(self):
@@ -139,7 +139,7 @@ class TestContext(ApiServerUnittest):
{"check": "$resp_status_code", "comparator": "eq", "expect": 201} {"check": "$resp_status_code", "comparator": "eq", "expect": 201}
] ]
variables = [] variables = []
self.context.init_teststep_variables(variables) self.context.init_test_variables(variables)
with self.assertRaises(exceptions.VariableNotFound): with self.assertRaises(exceptions.VariableNotFound):
self.context.validate(validators, resp_obj) self.context.validate(validators, resp_obj)
@@ -148,7 +148,7 @@ class TestContext(ApiServerUnittest):
variables = [ variables = [
{"resp_status_code": 200} {"resp_status_code": 200}
] ]
self.context.init_teststep_variables(variables) self.context.init_test_variables(variables)
with self.assertRaises(exceptions.ValidationFailure): with self.assertRaises(exceptions.ValidationFailure):
self.context.validate(validators, resp_obj) self.context.validate(validators, resp_obj)
+25 -25
View File
@@ -264,8 +264,8 @@ class TestSuiteLoader(unittest.TestCase):
cls.project_mapping = loader.project_mapping cls.project_mapping = loader.project_mapping
cls.tests_def_mapping = loader.tests_def_mapping cls.tests_def_mapping = loader.tests_def_mapping
def test_load_teststep_testcase(self): def test_load_test_testcase(self):
raw_teststep = { raw_test = {
"name": "setup and reset all (override).", "name": "setup and reset all (override).",
"testcase": "testcases/setup.yml", "testcase": "testcases/setup.yml",
"variables": [ "variables": [
@@ -273,24 +273,24 @@ class TestSuiteLoader(unittest.TestCase):
], ],
"output": ["token", "device_sn"] "output": ["token", "device_sn"]
} }
testcase = loader.load_teststep(raw_teststep) testcase = loader.load_test(raw_test)
self.assertEqual( self.assertEqual(
"setup and reset all (override).", "setup and reset all (override).",
testcase["name"] testcase["name"]
) )
teststeps = testcase["testcase_def"]["teststeps"] tests = testcase["testcase_def"]["tests"]
self.assertEqual(len(teststeps), 2) self.assertEqual(len(tests), 2)
self.assertEqual(teststeps[0]["name"], "get token (setup)") self.assertEqual(tests[0]["name"], "get token (setup)")
self.assertEqual(teststeps[1]["name"], "reset all users") self.assertEqual(tests[1]["name"], "reset all users")
def test_load_testcase(self): def test_load_testcase(self):
raw_testcase = loader.load_file("tests/testsuites/create_users.yml") raw_testcase = loader.load_file("tests/testsuites/create_users.yml")
testcase = loader.load_testcase(raw_testcase) testcase = loader.load_testcase(raw_testcase)
self.assertEqual(testcase["config"]["name"], "create users with uid") self.assertEqual(testcase["config"]["name"], "create users with uid")
self.assertIn("device_sn", testcase["config"]["variables"][0]) self.assertIn("device_sn", testcase["config"]["variables"][0])
self.assertEqual(len(testcase["teststeps"]), 2) self.assertEqual(len(testcase["tests"]), 2)
self.assertEqual(testcase["teststeps"][0]["name"], "create user 1000 and check result.") self.assertEqual(testcase["tests"][0]["name"], "create user 1000 and check result.")
self.assertEqual(testcase["teststeps"][0]["testcase_def"]["config"]["name"], "create user and check result.") self.assertEqual(testcase["tests"][0]["testcase_def"]["config"]["name"], "create user and check result.")
def test_load_testcases_by_path_files(self): def test_load_testcases_by_path_files(self):
# absolute file path # absolute file path
@@ -300,7 +300,7 @@ class TestSuiteLoader(unittest.TestCase):
project_mapping = tests_mapping["project_mapping"] project_mapping = tests_mapping["project_mapping"]
testcases_list = tests_mapping["testcases"] testcases_list = tests_mapping["testcases"]
self.assertEqual(len(testcases_list), 1) self.assertEqual(len(testcases_list), 1)
self.assertEqual(len(testcases_list[0]["teststeps"]), 3) self.assertEqual(len(testcases_list[0]["tests"]), 3)
self.assertIn("get_sign", project_mapping["functions"]) self.assertIn("get_sign", project_mapping["functions"])
# relative file path # relative file path
@@ -309,7 +309,7 @@ class TestSuiteLoader(unittest.TestCase):
project_mapping = tests_mapping["project_mapping"] project_mapping = tests_mapping["project_mapping"]
testcases_list = tests_mapping["testcases"] testcases_list = tests_mapping["testcases"]
self.assertEqual(len(testcases_list), 1) self.assertEqual(len(testcases_list), 1)
self.assertEqual(len(testcases_list[0]["teststeps"]), 3) self.assertEqual(len(testcases_list[0]["tests"]), 3)
self.assertIn("get_sign", project_mapping["functions"]) self.assertIn("get_sign", project_mapping["functions"])
# TODO: list/set container with file(s) # TODO: list/set container with file(s)
@@ -319,17 +319,17 @@ class TestSuiteLoader(unittest.TestCase):
# ] # ]
# testcases_list = loader.load_tests(path) # testcases_list = loader.load_tests(path)
# self.assertEqual(len(testcases_list), 2) # self.assertEqual(len(testcases_list), 2)
# self.assertEqual(len(testcases_list[0]["teststeps"]), 3) # self.assertEqual(len(testcases_list[0]["tests"]), 3)
# self.assertEqual(len(testcases_list[1]["teststeps"]), 3) # self.assertEqual(len(testcases_list[1]["tests"]), 3)
# testcases_list.extend(testcases_list) # testcases_list.extend(testcases_list)
# self.assertEqual(len(testcases_list), 4) # self.assertEqual(len(testcases_list), 4)
# for testcase in testcases_list: # for testcase in testcases_list:
# for teststep in testcase["teststeps"]: # for test_dict in testcase["tests"]:
# self.assertIn('name', teststep) # self.assertIn('name', test_dict)
# self.assertIn('request', teststep) # self.assertIn('request', test_dict)
# self.assertIn('url', teststep['request']) # self.assertIn('url', test_dict['request'])
# self.assertIn('method', teststep['request']) # self.assertIn('method', test_dict['request'])
def test_load_testcases_by_path_folder(self): def test_load_testcases_by_path_folder(self):
# absolute folder path # absolute folder path
@@ -381,15 +381,15 @@ class TestSuiteLoader(unittest.TestCase):
self.assertIn({'device_sn': '${gen_random_string(15)}'}, testcases_list[0]["config"]["variables"]) self.assertIn({'device_sn': '${gen_random_string(15)}'}, testcases_list[0]["config"]["variables"])
self.assertIn("gen_md5", project_mapping["functions"]) self.assertIn("gen_md5", project_mapping["functions"])
self.assertIn("base_url", testcases_list[0]["config"]) self.assertIn("base_url", testcases_list[0]["config"])
teststep0 = testcases_list[0]["teststeps"][0] test_dict0 = testcases_list[0]["tests"][0]
self.assertEqual( self.assertEqual(
"get token with $user_agent, $app_version", "get token with $user_agent, $app_version",
teststep0["name"] test_dict0["name"]
) )
self.assertIn("/api/get-token", teststep0["api_def"]["request"]["url"]) self.assertIn("/api/get-token", test_dict0["api_def"]["request"]["url"])
self.assertIn( self.assertIn(
{'eq': ['status_code', 200]}, {'eq': ['status_code', 200]},
teststep0["validate"] test_dict0["validate"]
) )
def test_load_testcases_with_testcase_ref(self): def test_load_testcases_with_testcase_ref(self):
@@ -407,7 +407,7 @@ class TestSuiteLoader(unittest.TestCase):
{'device_sn': '${gen_random_string(15)}'}, {'device_sn': '${gen_random_string(15)}'},
testcases_list[0]["config"]["variables"][0] testcases_list[0]["config"]["variables"][0]
) )
testcase0 = testcases_list[0]["teststeps"][0] testcase0 = testcases_list[0]["tests"][0]
self.assertEqual( self.assertEqual(
"create user 1000 and check result.", "create user 1000 and check result.",
testcase0["name"] testcase0["name"]
@@ -417,7 +417,7 @@ class TestSuiteLoader(unittest.TestCase):
testcase0["testcase_def"]["config"]["name"] testcase0["testcase_def"]["config"]["name"]
) )
testcase1 = testcases_list[0]["teststeps"][1] testcase1 = testcases_list[0]["tests"][1]
self.assertEqual( self.assertEqual(
"create user 1001 and check result.", "create user 1001 and check result.",
testcase1["name"] testcase1["name"]
+5 -5
View File
@@ -445,9 +445,9 @@ class TestParser(unittest.TestCase):
parser.parse_tests(tests_mapping) parser.parse_tests(tests_mapping)
parsed_testcases = tests_mapping["testcases"] parsed_testcases = tests_mapping["testcases"]
self.assertIsInstance(parsed_testcases, list) self.assertIsInstance(parsed_testcases, list)
teststep1 = parsed_testcases[0]["teststeps"][0] test_dict1 = parsed_testcases[0]["tests"][0]
self.assertEqual(teststep1["variables"]["var_c"], 3) self.assertEqual(test_dict1["variables"]["var_c"], 3)
self.assertEqual(teststep1["variables"]["PROJECT_KEY"], "ABCDEFGH") self.assertEqual(test_dict1["variables"]["PROJECT_KEY"], "ABCDEFGH")
# TODO: parameters # TODO: parameters
# self.assertEqual(len(parsed_testcases), 2 * 2) # self.assertEqual(len(parsed_testcases), 2 * 2)
self.assertEqual(parsed_testcases[0]["config"]["name"], '1230') self.assertEqual(parsed_testcases[0]["config"]["name"], '1230')
@@ -479,11 +479,11 @@ class TestParser(unittest.TestCase):
def test_extend_with_api(self): def test_extend_with_api(self):
loader.load_project_tests(os.path.join(os.getcwd(), "tests")) loader.load_project_tests(os.path.join(os.getcwd(), "tests"))
raw_stepinfo = { raw_testinfo = {
"name": "get token", "name": "get token",
"api": "get_token", "api": "get_token",
} }
api_def_dict = loader.load_teststep(raw_stepinfo) api_def_dict = loader.load_test(raw_testinfo)
test_block = { test_block = {
"name": "override block", "name": "override block",
"times": 3, "times": 3,
+1 -1
View File
@@ -215,7 +215,7 @@ class TestRunner(ApiServerUnittest):
tests_mapping = loader.load_tests(testcase_file_path) tests_mapping = loader.load_tests(testcase_file_path)
testcase = tests_mapping["testcases"][0] testcase = tests_mapping["testcases"][0]
config_dict_headers = testcase["config"]["request"]["headers"] config_dict_headers = testcase["config"]["request"]["headers"]
test_dict_headers = testcase["teststeps"][0]["request"]["headers"] test_dict_headers = testcase["tests"][0]["request"]["headers"]
headers = deep_update_dict( headers = deep_update_dict(
config_dict_headers, config_dict_headers,
test_dict_headers test_dict_headers
+3 -3
View File
@@ -24,8 +24,8 @@ class TestValidator(unittest.TestCase):
"path": "testcase1_path", "path": "testcase1_path",
"variables": [], # optional "variables": [], # optional
}, },
"teststeps": [ "tests": [
# teststep data structure # test data structure
{ {
'name': 'test step desc1', 'name': 'test step desc1',
'variables': [], # optional 'variables': [], # optional
@@ -33,7 +33,7 @@ class TestValidator(unittest.TestCase):
'validate': [], 'validate': [],
'request': {} 'request': {}
}, },
# teststep2 # another teststep dict # test_dict2 # another test dict
] ]
}, },
# testcase_dict_2 # another testcase dict # testcase_dict_2 # another testcase dict