refactor testcase layer mechanism:

1, autotest testsuite layer
2, performance test
This commit is contained in:
debugtalk
2018-12-10 16:10:06 +08:00
parent 6fd92679e3
commit acfca8c07f
13 changed files with 677 additions and 425 deletions
+10 -10
View File
@@ -69,7 +69,7 @@ class HttpRunner(object):
test_runner = runner.Runner(config, functions) test_runner = runner.Runner(config, functions)
TestSequense = type('TestSequense', (unittest.TestCase,), {}) TestSequense = type('TestSequense', (unittest.TestCase,), {})
tests = testcase.get("tests", []) tests = testcase.get("teststeps", [])
for index, test_dict in enumerate(tests): for index, test_dict in enumerate(tests):
for times_index in range(int(test_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,
@@ -80,7 +80,7 @@ class HttpRunner(object):
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, "tests", tests) setattr(loaded_testcase, "teststeps", tests)
setattr(loaded_testcase, "runner", test_runner) setattr(loaded_testcase, "runner", test_runner)
test_suite.addTest(loaded_testcase) test_suite.addTest(loaded_testcase)
@@ -145,14 +145,14 @@ class HttpRunner(object):
""" """
# parse tests # parse tests
self.exception_stage = "parse tests" self.exception_stage = "parse tests"
parser.parse_tests(tests_mapping) parsed_tests_mapping = parser.parse_tests(tests_mapping)
if self.save_tests: if self.save_tests:
utils.dump_tests(tests_mapping, "parsed") utils.dump_tests(parsed_tests_mapping, "parsed")
# add tests to test suite # add tests to test suite
self.exception_stage = "add tests to test suite" self.exception_stage = "add tests to test suite"
test_suite = self._add_tests(tests_mapping) test_suite = self._add_tests(parsed_tests_mapping)
# run test suite # run test suite
self.exception_stage = "run test suite" self.exception_stage = "run test suite"
@@ -236,16 +236,16 @@ def prepare_locust_tests(path):
{ {
"functions": {}, "functions": {},
"tests": [] "teststeps": []
} }
""" """
tests_mapping = loader.load_tests(path) tests_mapping = loader.load_tests(path)
parser.parse_tests(tests_mapping) parsed_tests_mapping = parser.parse_tests(tests_mapping)
functions = tests_mapping.get("project_mapping", {}).get("functions", {}) functions = parsed_tests_mapping.get("project_mapping", {}).get("functions", {})
testcase = tests_mapping["testcases"][0] testcase = parsed_tests_mapping["testcases"][0]
items = testcase.get("tests", []) items = testcase.get("teststeps", [])
tests = [] tests = []
for item in items: for item in items:
+188 -92
View File
@@ -286,75 +286,95 @@ tests_def_mapping = {
"testcases": {} "testcases": {}
} }
def load_test(raw_testinfo):
""" load test with api/testcase/proc references def __extend_with_api_ref(raw_testinfo):
""" extend with api reference
Raises:
exceptions.ApiNotFound: api not found
"""
api_name = raw_testinfo["api"]
# api maybe defined in two types:
# 1, individual file: each file is corresponding to one api definition
# 2, api sets file: one file contains a list of api definitions
if not os.path.isabs(api_name):
api_path = os.path.join(tests_def_mapping["PWD"], api_name)
if os.path.isfile(api_path):
# type 1: api is defined in individual file
api_name = api_path
try:
block = tests_def_mapping["api"][api_name]
# NOTICE: avoid project_mapping been changed during iteration.
raw_testinfo["api_def"] = utils.deepcopy_dict(block)
except KeyError:
raise exceptions.ApiNotFound("{} not found!".format(name))
def __extend_with_testcase_ref(raw_testinfo):
""" extend with testcase reference
"""
testcase_path = raw_testinfo["testcase"]
if testcase_path not in tests_def_mapping["testcases"]:
testcase_path = os.path.join(
project_mapping["PWD"],
testcase_path
)
testcase_dict = load_testcase(load_file(testcase_path))
tests_def_mapping[testcase_path] = testcase_dict
else:
testcase_dict = tests_def_mapping[testcase_path]
raw_testinfo["testcase_def"] = testcase_dict
def load_teststep(raw_testinfo):
""" load testcase step content.
teststep maybe defined directly, or reference api/testcase.
Args: Args:
raw_testinfo (dict): test 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",
"api": "api_add_cart", "api": "/path/to/api",
"variables": [], "variables": {},
"validate": [], "validate": [],
"extract": {} "extract": {}
} }
# testcase reference # testcase reference
{ {
"name": "add product to cart", "name": "add product to cart",
"testcase": "create_and_check", "testcase": "/path/to/testcase",
"variables": [] "variables": {}
} }
# define directly # define directly
{ {
"name": "checkout cart", "name": "checkout cart",
"request": {}, "request": {},
"variables": [], "variables": {},
"validate": [], "validate": [],
"extract": {} "extract": {}
} }
Returns: Returns:
list: loaded tests list dict: loaded teststep content
Args:
raw_testinfo (dict): test info
""" """
# reference api # reference api
if "api" in raw_testinfo: if "api" in raw_testinfo:
api_name = raw_testinfo["api"] __extend_with_api_ref(raw_testinfo)
# api maybe defined in two types:
# 1, individual file: each file is corresponding to one api definition
# 2, api sets file: one file contains a list of api definitions
if not os.path.isabs(api_name):
api_path = os.path.join(tests_def_mapping["PWD"], api_name)
if os.path.isfile(api_path):
# type 1: api is defined in individual file
api_name = api_path
raw_testinfo["api_def"] = _get_api_definition(api_name)
# TODO: reference proc functions # TODO: reference proc functions
elif "func" in raw_testinfo: # elif "func" in raw_testinfo:
pass # pass
# reference testcase # reference testcase
elif "testcase" in raw_testinfo: elif "testcase" in raw_testinfo:
testcase_path = raw_testinfo["testcase"] __extend_with_testcase_ref(raw_testinfo)
if testcase_path not in tests_def_mapping["testcases"]:
testcase_path = os.path.join(
project_mapping["PWD"],
testcase_path
)
testcase_dict = load_testcase(load_file(testcase_path))
tests_def_mapping[testcase_path] = testcase_dict
else:
testcase_dict = tests_def_mapping[testcase_path]
raw_testinfo["testcase_def"] = testcase_dict
# define directly # define directly
else: else:
@@ -364,7 +384,7 @@ def load_test(raw_testinfo):
def load_testcase(raw_testcase): def load_testcase(raw_testcase):
""" load testcase/testsuite with api/testcase references """ load testcase with api/testcase references.
Args: Args:
raw_testcase (list): raw testcase content loaded from JSON/YAML file: raw_testcase (list): raw testcase content loaded from JSON/YAML file:
@@ -372,12 +392,11 @@ def load_testcase(raw_testcase):
# config part # config part
{ {
"config": { "config": {
"name": "", "name": "XXXX",
"def": "suite_order()", "base_url": "https://debugtalk.com"
"request": {}
} }
}, },
# tests part # teststeps part
{ {
"test": {...} "test": {...}
}, },
@@ -389,9 +408,8 @@ def load_testcase(raw_testcase):
Returns: Returns:
dict: loaded testcase content dict: loaded testcase content
{ {
"name": "XYZ",
"config": {}, "config": {},
"tests": [test11, test12] "teststeps": [test11, test12]
} }
""" """
@@ -399,20 +417,11 @@ def load_testcase(raw_testcase):
tests = [] tests = []
for item in raw_testcase: for item in raw_testcase:
# TODO: add json schema validation
if not isinstance(item, dict) or len(item) != 1:
raise exceptions.FileFormatError("Testcase format error: {}".format(item))
key, test_block = item.popitem() key, test_block = item.popitem()
if not isinstance(test_block, dict):
raise exceptions.FileFormatError("Testcase format error: {}".format(item))
if key == "config": if key == "config":
config.update(test_block) config.update(test_block)
elif key == "test": elif key == "test":
tests.append(load_test(test_block)) tests.append(load_teststep(test_block))
else: else:
logger.log_warning( logger.log_warning(
"unexpected block key: {}. block key should only be 'config' or 'test'.".format(key) "unexpected block key: {}. block key should only be 'config' or 'test'.".format(key)
@@ -420,26 +429,113 @@ def load_testcase(raw_testcase):
return { return {
"config": config, "config": config,
"tests": tests "teststeps": tests
} }
def _get_api_definition(name): def load_testsuite(raw_testsuite):
""" get api definition by name. """ load testsuite with testcase references.
Args:
raw_testsuite (dict): raw testsuite content loaded from JSON/YAML file:
{
"config": {
"name": "",
"request": {}
}
"testcases": {
"testcase1": {
"testcase": "/path/to/testcase",
"variables": {...},
"parameters": {...}
},
"testcase2": {}
}
}
Returns: Returns:
dict: expected api definition if found. dict: loaded testsuite content
{
Raises: "config": {},
exceptions.ApiNotFound: api not found "testcases": [testcase1, testcase2]
}
""" """
try: testcases = raw_testsuite["testcases"]
block = tests_def_mapping["api"][name] for name, raw_testcase in testcases.items():
# NOTICE: avoid project_mapping been changed during iteration. __extend_with_testcase_ref(raw_testcase)
return utils.deepcopy_dict(block) raw_testcase.setdefault("name", name)
except KeyError:
raise exceptions.ApiNotFound("{} not found!".format(name)) return raw_testsuite
def load_test_file(path):
""" load test file, file maybe testcase/testsuite/api
Args:
path (str): test file path
Returns:
dict: loaded test content
# api
{
"path": path,
"type": "api",
"name": "",
"request": {}
}
# testcase
{
"path": path,
"type": "testcase",
"config": {},
"teststeps": []
}
# testsuite
{
"path": path,
"type": "testsuite",
"config": {},
"testcases": {}
}
"""
raw_content = load_file(path)
loaded_content = None
if isinstance(raw_content, dict):
if "testcases" in raw_content:
# file_type: testsuite
# TODO: add json schema validation for testsuite
loaded_content = load_testsuite(raw_content)
loaded_content["path"] = path
loaded_content["type"] = "testsuite"
elif "request" in raw_content:
# file_type: api
# TODO: add json schema validation for api
loaded_content = raw_content
loaded_content["path"] = path
loaded_content["type"] = "api"
else:
# invalid format
logger.log_warning("Invalid test file format: {}".format(path))
elif isinstance(raw_content, list) and len(raw_content) > 0:
# file_type: testcase
# TODO: add json schema validation for testcase
loaded_content = load_testcase(raw_content)
loaded_content["path"] = path
loaded_content["type"] = "testcase"
else:
# invalid format
logger.log_warning("Invalid test file format: {}".format(path))
return loaded_content
def load_folder_content(folder_path): def load_folder_content(folder_path):
@@ -623,7 +719,7 @@ def load_tests(path, dot_env_path=None):
"path": "testcase1_path", "path": "testcase1_path",
"variables": [], # optional "variables": [], # optional
}, },
"tests": [ "teststeps": [
# test data structure # test data structure
{ {
'name': 'test desc1', 'name': 'test desc1',
@@ -635,7 +731,17 @@ def load_tests(path, dot_env_path=None):
test_dict_2 # another test dict test_dict_2 # another test dict
] ]
}, },
testcase_dict_2 # another testcase dict testcase_2_dict # another testcase dict
],
"testsuites": [
{ # testsuite data structure
"config": {},
"testcases": {
"testcase1": {},
"testcase2": {},
}
},
testsuite_2_dict
] ]
} }
@@ -653,33 +759,23 @@ def load_tests(path, dot_env_path=None):
"project_mapping": project_mapping "project_mapping": project_mapping
} }
def load_test_file(path): def __load_file_content(path):
raw_testcase = load_file(path) loaded_content = load_test_file(path)
if not loaded_content:
try: pass
testcase = load_testcase(raw_testcase) elif loaded_content["type"] == "testsuite":
testcase["config"]["path"] = path tests_mapping.setdefault("testsuites", []).append(loaded_content)
except exceptions.FileFormatError: elif loaded_content["type"] == "testcase":
testcase = {} tests_mapping.setdefault("testcases", []).append(loaded_content)
elif loaded_content["type"] == "api":
return testcase tests_mapping.setdefault("api", []).append(loaded_content)
testcases_list = []
if os.path.isdir(path): if os.path.isdir(path):
files_list = load_folder_files(path) files_list = load_folder_files(path)
for path in files_list: for path in files_list:
testcase = load_test_file(path) __load_file_content(path)
if not testcase:
continue
testcases_list.append(testcase)
elif os.path.isfile(path): elif os.path.isfile(path):
__load_file_content(path)
testcase = load_test_file(path)
if testcase:
testcases_list.append(testcase)
tests_mapping["testcases"] = testcases_list
return tests_mapping return tests_mapping
+215 -71
View File
@@ -676,7 +676,7 @@ def _extend_with_testcase(test_dict, testcase_def_dict):
def __parse_config(config, project_mapping): def __parse_config(config, project_mapping):
""" parse testcase config, include variables and name. """ parse testcase/testsuite config, include variables and name.
""" """
# get config variables # get config variables
raw_config_variables = config.pop("variables", {}) raw_config_variables = config.pop("variables", {})
@@ -684,31 +684,29 @@ def __parse_config(config, project_mapping):
override_variables = utils.deepcopy_dict(project_mapping.get("variables", {})) override_variables = utils.deepcopy_dict(project_mapping.get("variables", {}))
functions = project_mapping.get("functions", {}) functions = project_mapping.get("functions", {})
# override testcase config variables with passed in variables # override config variables with passed in variables
raw_config_variables_mapping.update(override_variables)
# parse config variables
parsed_config_variables = {}
for key, value in raw_config_variables_mapping.items(): for key, value in raw_config_variables_mapping.items():
try:
parsed_value = parse_data(
value,
raw_config_variables_mapping,
functions
)
except exceptions.VariableNotFound:
pass
parsed_config_variables[key] = parsed_value
if key in override_variables: if parsed_config_variables:
# passed in config["variables"] = parsed_config_variables
continue
else:
# config variables
try:
parsed_value = parse_data(
value,
override_variables,
functions
)
except exceptions.VariableNotFound:
pass
override_variables[key] = parsed_value
if override_variables:
config["variables"] = override_variables
# parse config name # parse config name
config["name"] = parse_data( config["name"] = parse_data(
config.get("name", ""), config.get("name", ""),
override_variables, parsed_config_variables,
functions functions
) )
@@ -716,17 +714,17 @@ def __parse_config(config, project_mapping):
if "base_url" in config: if "base_url" in config:
config["base_url"] = parse_data( config["base_url"] = parse_data(
config["base_url"], config["base_url"],
override_variables, parsed_config_variables,
functions functions
) )
def __parse_tests(tests, config, project_mapping): def __parse_testcase_tests(tests, config, project_mapping):
""" override tests with testcase config variables, base_url and verify. """ override tests with testcase config variables, base_url and verify.
test maybe nested testcase. test maybe nested testcase.
variables priority: variables priority:
testsuite config > testsuite test > testcase config > testcase test > api testcase config > testcase test > testcase_def config > testcase_def test > api
base_url/verify priority: base_url/verify priority:
testcase test > testcase config > testsuite test > testsuite config > api testcase test > testcase config > testsuite test > testsuite config > api
@@ -734,9 +732,7 @@ def __parse_tests(tests, config, project_mapping):
Args: Args:
tests (list): tests (list):
config (dict): config (dict):
project_mapping (dict):
Returns:
list: overrided tests
""" """
config_variables = config.pop("variables", {}) config_variables = config.pop("variables", {})
@@ -752,51 +748,34 @@ def __parse_tests(tests, config, project_mapping):
test_dict.setdefault("verify", config_verify) test_dict.setdefault("verify", config_verify)
# 1, testcase config => testcase tests
# override test_dict variables
test_dict["variables"] = utils.extend_variables(
test_dict.pop("variables", {}),
config_variables
)
# parse test_dict name
try:
test_dict["name"] = parse_data(
test_dict.pop("name", ""),
test_dict["variables"],
functions
)
except exceptions.VariableNotFound:
pass
if "testcase_def" in test_dict: if "testcase_def" in test_dict:
# test_dict is nested testcase # test_dict is nested testcase
# 1, testsuite config => testsuite tests # 2, testcase test_dict => testcase_def config
# override test_dict variables
test_dict["variables"] = utils.extend_variables(
test_dict.pop("variables", {}),
config_variables
)
# parse test_dict name
try:
test_dict["name"] = parse_data(
test_dict.pop("name", ""),
test_dict["variables"],
functions
)
except exceptions.VariableNotFound:
pass
# 2, testsuite test_dict => testcase config
testcase_def = test_dict.pop("testcase_def") testcase_def = test_dict.pop("testcase_def")
_extend_with_testcase(test_dict, testcase_def) _extend_with_testcase(test_dict, testcase_def)
# 3, testcase config => testcase test_dict # 3, testcase_def config => testcase_def test_dict
_parse_testcase(test_dict, project_mapping) _parse_testcase(test_dict, project_mapping)
else: else:
# 1, config => tests
# override test_dict variables
test_dict["variables"] = utils.extend_variables(
test_dict.pop("variables", {}),
config_variables
)
# parse test_dict name
try:
test_dict["name"] = parse_data(
test_dict.pop("name", ""),
test_dict["variables"],
functions
)
except exceptions.VariableNotFound:
pass
if "api_def" in test_dict: if "api_def" in test_dict:
# test_dict has API reference # test_dict has API reference
# 2, test_dict => api # 2, test_dict => api
@@ -804,11 +783,14 @@ def __parse_tests(tests, config, project_mapping):
_extend_with_api(test_dict, api_def_dict) _extend_with_api(test_dict, api_def_dict)
if test_dict.get("base_url"): if test_dict.get("base_url"):
# parse base_url
base_url = parse_data( base_url = parse_data(
test_dict.pop("base_url"), test_dict.pop("base_url"),
test_dict["variables"], test_dict["variables"],
functions functions
) )
# build path with base_url
try: try:
request_url = parse_data( request_url = parse_data(
test_dict["request"]["url"], test_dict["request"]["url"],
@@ -817,7 +799,7 @@ def __parse_tests(tests, config, project_mapping):
) )
except exceptions.VariableNotFound: except exceptions.VariableNotFound:
""" variable in current url maybe extracted from former api """ variable in current url maybe extracted from former api
"tests": [ "teststeps": [
{ {
"request": {}, "request": {},
"extract": { "extract": {
@@ -840,12 +822,125 @@ def __parse_tests(tests, config, project_mapping):
def _parse_testcase(testcase, project_mapping): def _parse_testcase(testcase, project_mapping):
""" parse testcase
Args:
testcase (dict):
{
"config": {},
"teststeps": []
}
"""
testcase.setdefault("config", {})
__parse_config(testcase["config"], project_mapping) __parse_config(testcase["config"], project_mapping)
__parse_tests(testcase["tests"], testcase["config"], project_mapping) __parse_testcase_tests(testcase["teststeps"], testcase["config"], project_mapping)
def __get_parsed_testsuite_testcases(testcases, testsuite_config, project_mapping):
""" override testscases with testsuite config variables, base_url and verify.
variables priority:
testsuite config > testcase config > testcase_def config > testcase_def tests > api
Args:
testcases (dict):
{
"testcase1 name": {
"testcase": "testcases/create_and_check.yml",
"weight": 2,
"variables": {
"uid": 1000
},
"parameters": {
"uid": [100, 101, 102]
},
"testcase_def": {
"config": {},
"teststeps": []
}
},
"testcase2 name": {}
}
testsuite_config (dict):
{
"name": "testsuite name",
"variables": {
"device_sn": "${gen_random_string(15)}"
},
"base_url": "http://127.0.0.1:5000"
}
project_mapping (dict):
{
"env": {},
"functions": {}
}
"""
testsuite_config_variables = testsuite_config.get("variables", {})
functions = project_mapping.get("functions", {})
parsed_testcase_list = []
for testcase_name, testcase in testcases.items():
# TODO: add parameterize
# parameters = testcase.get("parameters")
parsed_testcase = testcase.pop("testcase_def")
parsed_testcase.setdefault("config", {})
parsed_testcase["path"] = testcase["testcase"]
parsed_testcase["config"]["name"] = testcase_name
# 1, testsuite config => testcase config
# override test_dict variables
testcase_config_variables = utils.extend_variables(
testcase.pop("variables", {}),
testsuite_config_variables
)
# 2, testcase config > testcase_def config
# override testcase_def config variables
parsed_testcase_config_variables = utils.extend_variables(
parsed_testcase["config"].pop("variables", {}),
testcase_config_variables
)
# parse config variables
parsed_config_variables = {}
for key, value in parsed_testcase_config_variables.items():
try:
parsed_value = parse_data(
value,
parsed_testcase_config_variables,
functions
)
except exceptions.VariableNotFound:
pass
parsed_config_variables[key] = parsed_value
if parsed_config_variables:
parsed_testcase["config"]["variables"] = parsed_config_variables
_parse_testcase(parsed_testcase, project_mapping)
parsed_testcase_list.append(parsed_testcase)
return parsed_testcase_list
def _parse_testsuite(testsuite, project_mapping):
testsuite.setdefault("config", {})
__parse_config(testsuite["config"], project_mapping)
parsed_testcase_list = __get_parsed_testsuite_testcases(
testsuite["testcases"],
testsuite["config"],
project_mapping
)
return parsed_testcase_list
def parse_tests(tests_mapping): def parse_tests(tests_mapping):
""" parse testcases configs, including variables/name/request. """ parse tests and load to parsed testcases
tests include api, testcases and testsuites.
Args: Args:
tests_mapping (dict): project info and testcases list. tests_mapping (dict): project info and testcases list.
@@ -857,14 +952,34 @@ def parse_tests(tests_mapping):
"variables": {}, # optional, priority 1 "variables": {}, # optional, priority 1
"env": {} "env": {}
}, },
"testsuites": [
{ # testsuite data structure
"config": {},
"testcases": {
"testcase1 name": {
"variables": {
"uid": 1000
},
"parameters": {
"uid": [100, 101, 102]
},
"testcase_def": {
"config": {},
"teststeps": []
}
},
"testcase2 name": {}
}
}
],
"testcases": [ "testcases": [
{ # testcase data structure { # testcase data structure
"config": { "config": {
"name": "desc1", "name": "desc1",
"path": "testcase1_path", "path": "testcase1_path",
"variables": [], # optional, priority 2 "variables": {}, # optional, priority 2
}, },
"tests": [ "teststeps": [
# test data structure # test data structure
{ {
'name': 'test step desc1', 'name': 'test step desc1',
@@ -880,12 +995,41 @@ def parse_tests(tests_mapping):
] ]
}, },
testcase_dict_2 # another testcase dict testcase_dict_2 # another testcase dict
] ],
"api": {
"variables": {},
"request": {}
}
} }
""" """
project_mapping = tests_mapping.get("project_mapping", {}) project_mapping = tests_mapping.get("project_mapping", {})
parsed_tests_mapping = {
"project_mapping": project_mapping,
"testcases": []
}
for testcase in tests_mapping["testcases"]: for test_type in tests_mapping:
testcase.setdefault("config", {})
_parse_testcase(testcase, project_mapping) if test_type == "testsuites":
# load testcases of testsuite
testsuites = tests_mapping["testsuites"]
for testsuite in testsuites:
parsed_testcases = _parse_testsuite(testsuite, project_mapping)
for parsed_testcase in parsed_testcases:
parsed_tests_mapping["testcases"].append(parsed_testcase)
elif test_type == "testcases":
for testcase in tests_mapping["testcases"]:
_parse_testcase(testcase, project_mapping)
parsed_tests_mapping["testcases"].append(testcase)
elif test_type == "api":
# encapsulate api as a testcase
testcase = {
"teststeps": tests_mapping["api"]
}
_parse_testcase(testcase, project_mapping)
parsed_tests_mapping["testcases"].append(testcase)
return parsed_tests_mapping
+2 -2
View File
@@ -282,7 +282,7 @@ class Runner(object):
http_client_session = self.http_client_session.__class__(base_url) http_client_session = self.http_client_session.__class__(base_url)
test_runner = Runner(config, self.functions, http_client_session) test_runner = Runner(config, self.functions, http_client_session)
tests = testcase_dict.get("tests", []) tests = testcase_dict.get("teststeps", [])
try: try:
for index, test_dict in enumerate(tests): for index, test_dict in enumerate(tests):
@@ -317,7 +317,7 @@ class Runner(object):
# nested testcase # nested testcase
{ {
"config": {...}, "config": {...},
"tests": [ "teststeps": [
{...}, {...},
{...} {...}
] ]
+7 -1
View File
@@ -689,11 +689,17 @@ def dump_tests(tests_mapping, tag_name):
tests_to_dump["project_mapping"][key] = project_mapping[key] tests_to_dump["project_mapping"][key] = project_mapping[key]
continue continue
# remove functions in order to dump
if project_mapping["functions"]: if project_mapping["functions"]:
debugtalk_py_path = os.path.join(pwd_dir_path, "debugtalk.py") debugtalk_py_path = os.path.join(pwd_dir_path, "debugtalk.py")
tests_to_dump["project_mapping"]["debugtalk.py"] = debugtalk_py_path tests_to_dump["project_mapping"]["debugtalk.py"] = debugtalk_py_path
tests_to_dump["testcases"] = tests_mapping["testcases"] if "api" in tests_mapping:
tests_to_dump["api"] = tests_mapping["api"]
elif "testcases" in tests_mapping:
tests_to_dump["testcases"] = tests_mapping["testcases"]
elif "testsuites" in tests_mapping:
tests_to_dump["testsuites"] = tests_mapping["testsuites"]
dump_json_file(tests_to_dump, pwd_dir_path, dump_file_name) dump_json_file(tests_to_dump, pwd_dir_path, dump_file_name)
+4 -4
View File
@@ -19,7 +19,7 @@ def is_testcase(data_structure):
"variables": [], # optional "variables": [], # optional
"request": {} # optional "request": {} # optional
}, },
"tests": [ "teststeps": [
test_dict1, test_dict1,
{ # test_dict2 { # test_dict2
'name': 'test step desc2', 'name': 'test step desc2',
@@ -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 "tests" not in data_structure: if "teststeps" not in data_structure:
return False return False
if not isinstance(data_structure["tests"], list): if not isinstance(data_structure["teststeps"], list):
return False return False
return True return True
@@ -67,7 +67,7 @@ def is_testcases(data_structure):
"path": "testcase1_path", "path": "testcase1_path",
"variables": [], # optional "variables": [], # optional
}, },
"tests": [ "teststeps": [
# test data structure # test data structure
{ {
'name': 'test step desc1', 'name': 'test step desc1',
+1
View File
@@ -1,3 +1,4 @@
name: create user
variables: variables:
user_name: user0 user_name: user0
user_password: "000000" user_password: "000000"
+62 -59
View File
@@ -29,7 +29,7 @@ class TestHttpRunner(ApiServerUnittest):
}, },
'variables': [] 'variables': []
}, },
'tests': [ "teststeps": [
{ {
'name': '/api/get-token', 'name': '/api/get-token',
'request': { 'request': {
@@ -119,7 +119,7 @@ class TestHttpRunner(ApiServerUnittest):
}, },
'variables': [] 'variables': []
}, },
"tests": [ "teststeps": [
{ {
"name": "post data", "name": "post data",
"request": { "request": {
@@ -164,7 +164,7 @@ class TestHttpRunner(ApiServerUnittest):
self.runner.run("tests/testsuites/create_users.yml") self.runner.run("tests/testsuites/create_users.yml")
summary = self.runner.summary summary = self.runner.summary
self.assertTrue(summary["success"]) self.assertTrue(summary["success"])
self.assertEqual(summary["stat"]["testsRun"], 2) self.assertEqual(summary["stat"]["testsRun"], 8)
def test_run_httprunner_with_hooks(self): def test_run_httprunner_with_hooks(self):
testcase_file_path = os.path.join( testcase_file_path = os.path.join(
@@ -180,7 +180,7 @@ class TestHttpRunner(ApiServerUnittest):
testcases = [ testcases = [
{ {
"config": {"name": "test teardown hooks"}, "config": {"name": "test teardown hooks"},
"tests": [ "teststeps": [
{ {
"name": "test teardown hooks", "name": "test teardown hooks",
"request": { "request": {
@@ -220,7 +220,7 @@ class TestHttpRunner(ApiServerUnittest):
"config": { "config": {
"name": "test teardown hooks" "name": "test teardown hooks"
}, },
"tests": [ "teststeps": [
{ {
"name": "test teardown hooks", "name": "test teardown hooks",
"request": { "request": {
@@ -254,7 +254,7 @@ class TestHttpRunner(ApiServerUnittest):
"config": { "config": {
"name": "test teardown hooks" "name": "test teardown hooks"
}, },
"tests": [ "teststeps": [
{ {
"name": "test teardown hooks", "name": "test teardown hooks",
"request": { "request": {
@@ -361,11 +361,11 @@ class TestHttpRunner(ApiServerUnittest):
testcase_file_path = os.path.join( testcase_file_path = os.path.join(
os.getcwd(), 'tests/data/demo_parameters.yml') os.getcwd(), 'tests/data/demo_parameters.yml')
tests_mapping = loader.load_tests(testcase_file_path) tests_mapping = loader.load_tests(testcase_file_path)
parser.parse_tests(tests_mapping) parsed_tests_mapping = parser.parse_tests(tests_mapping)
test_suite = self.runner._add_tests(tests_mapping) test_suite = self.runner._add_tests(parsed_tests_mapping)
self.assertEqual( self.assertEqual(
test_suite._tests[0].tests[0]['name'], test_suite._tests[0].teststeps[0]['name'],
'get token with iOS/10.1 and test1' 'get token with iOS/10.1 and test1'
) )
# TODO: add parameterize # TODO: add parameterize
@@ -415,9 +415,9 @@ class TestApi(ApiServerUnittest):
self.assertEqual(len(testcases), 1) self.assertEqual(len(testcases), 1)
testcase_config = testcases[0]["config"] testcase_config = testcases[0]["config"]
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", testcases[0])
testcase_tests = testcases[0]["tests"] testcase_tests = testcases[0]["teststeps"]
self.assertEqual(len(testcase_tests), 2) self.assertEqual(len(testcase_tests), 2)
self.assertIn("api", testcase_tests[0]) self.assertIn("api", testcase_tests[0])
self.assertEqual(testcase_tests[0]["name"], "get token (setup)") self.assertEqual(testcase_tests[0]["name"], "get token (setup)")
@@ -429,15 +429,15 @@ class TestApi(ApiServerUnittest):
testcase_path = "tests/testcases/setup.yml" testcase_path = "tests/testcases/setup.yml"
tests_mapping = loader.load_tests(testcase_path) tests_mapping = loader.load_tests(testcase_path)
parser.parse_tests(tests_mapping) parsed_tests_mapping = parser.parse_tests(tests_mapping)
parsed_testcases = tests_mapping["testcases"] parsed_testcases = parsed_tests_mapping["testcases"]
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]["tests"]), 2) self.assertEqual(len(parsed_testcases[0]["teststeps"]), 2)
test_dict1 = parsed_testcases[0]["tests"][0] test_dict1 = parsed_testcases[0]["teststeps"][0]
self.assertEqual(test_dict1["name"], "get token (setup)") self.assertEqual(test_dict1["name"], "get token (setup)")
self.assertNotIn("api_def", test_dict1) self.assertNotIn("api_def", test_dict1)
self.assertEqual(test_dict1["variables"]["device_sn"], "TESTCASE_SETUP_XXX") self.assertEqual(test_dict1["variables"]["device_sn"], "TESTCASE_SETUP_XXX")
@@ -447,31 +447,31 @@ class TestApi(ApiServerUnittest):
testcase_path = "tests/testcases/setup.yml" testcase_path = "tests/testcases/setup.yml"
tests_mapping = loader.load_tests(testcase_path) tests_mapping = loader.load_tests(testcase_path)
parser.parse_tests(tests_mapping) parsed_tests_mapping = parser.parse_tests(tests_mapping)
runner = HttpRunner() runner = HttpRunner()
test_suite = runner._add_tests(tests_mapping) test_suite = runner._add_tests(parsed_tests_mapping)
self.assertEqual(len(test_suite._tests), 1) self.assertEqual(len(test_suite._tests), 1)
tests = test_suite._tests[0].tests teststeps = test_suite._tests[0].teststeps
self.assertEqual(tests[0]["name"], "get token (setup)") self.assertEqual(teststeps[0]["name"], "get token (setup)")
self.assertEqual(tests[0]["variables"]["device_sn"], "TESTCASE_SETUP_XXX") self.assertEqual(teststeps[0]["variables"]["device_sn"], "TESTCASE_SETUP_XXX")
self.assertIn("api", tests[0]) self.assertIn("api", teststeps[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"
tests_mapping = loader.load_tests(testcase_path) tests_mapping = loader.load_tests(testcase_path)
parser.parse_tests(tests_mapping) parsed_tests_mapping = parser.parse_tests(tests_mapping)
runner = HttpRunner() runner = HttpRunner()
test_suite = runner._add_tests(tests_mapping) test_suite = runner._add_tests(parsed_tests_mapping)
tests_results = runner._run_suite(test_suite) tests_results = runner._run_suite(test_suite)
self.assertEqual(len(tests_results[0][1].records), 2) self.assertEqual(len(tests_results[0][1].records), 2)
def test_testcase_complex_run_suite(self): def test_testcase_complex_run_suite(self):
testcase_path = "tests/testcases/create_and_check.yml" testcase_path = "tests/testcases/create_and_check.yml"
tests_mapping = loader.load_tests(testcase_path) tests_mapping = loader.load_tests(testcase_path)
parser.parse_tests(tests_mapping) parsed_tests_mapping = parser.parse_tests(tests_mapping)
runner = HttpRunner() runner = HttpRunner()
test_suite = runner._add_tests(tests_mapping) test_suite = runner._add_tests(parsed_tests_mapping)
tests_results = runner._run_suite(test_suite) tests_results = runner._run_suite(test_suite)
self.assertEqual(len(tests_results[0][1].records), 4) self.assertEqual(len(tests_results[0][1].records), 4)
@@ -495,74 +495,77 @@ class TestApi(ApiServerUnittest):
self.assertIn("functions", project_mapping) self.assertIn("functions", project_mapping)
self.assertIn("env", project_mapping) self.assertIn("env", project_mapping)
testcases = tests_mapping["testcases"] testsuites = tests_mapping["testsuites"]
self.assertIsInstance(testcases, list) self.assertIsInstance(testsuites, list)
self.assertEqual(len(testcases), 1) self.assertEqual(len(testsuites), 1)
testcase_config = testcases[0]["config"]
self.assertEqual(testcase_config["name"], "create users with uid")
self.assertIn("path", testcase_config)
testcase_tests = testcases[0]["tests"] self.assertIn("path", testsuites[0])
self.assertEqual(len(testcase_tests), 2) testsuite_config = testsuites[0]["config"]
self.assertIn("testcase_def", testcase_tests[0]) self.assertEqual(testsuite_config["name"], "create users with uid")
self.assertEqual(testcase_tests[0]["name"], "create user 1000 and check result.")
self.assertIsInstance(testcase_tests[0]["testcase_def"], dict) testcases = testsuites[0]["testcases"]
self.assertEqual(testcase_tests[0]["testcase_def"]["config"]["name"], "create user and check result.") self.assertEqual(len(testcases), 2)
self.assertEqual(len(testcase_tests[0]["testcase_def"]["tests"]), 4) self.assertIn("create user 1000 and check result.", testcases)
self.assertEqual(testcase_tests[0]["testcase_def"]["tests"][0]["name"], "setup and reset all (override).") testcase_tests = testcases["create user 1000 and check result."]
self.assertIn("testcase_def", testcase_tests)
self.assertEqual(testcase_tests["name"], "create user 1000 and check result.")
self.assertIsInstance(testcase_tests["testcase_def"], dict)
self.assertEqual(testcase_tests["testcase_def"]["config"]["name"], "create user and check result.")
self.assertEqual(len(testcase_tests["testcase_def"]["teststeps"]), 4)
self.assertEqual(testcase_tests["testcase_def"]["teststeps"][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"
tests_mapping = loader.load_tests(testcase_path) tests_mapping = loader.load_tests(testcase_path)
parser.parse_tests(tests_mapping) parsed_tests_mapping = parser.parse_tests(tests_mapping)
parsed_testcases = tests_mapping["testcases"] parsed_testcases = parsed_tests_mapping["testcases"]
self.assertEqual(len(parsed_testcases), 1) self.assertEqual(len(parsed_testcases), 2)
self.assertEqual(len(parsed_testcases[0]["tests"]), 2) self.assertEqual(len(parsed_testcases[0]["teststeps"]), 4)
testcase1 = parsed_testcases[0]["tests"][0] testcase1 = parsed_testcases[0]["teststeps"][0]
self.assertEqual(testcase1["config"]["name"], "create user 1000 and check result.") self.assertEqual(testcase1["config"]["name"], "setup and reset all (override).")
self.assertNotIn("testcase_def", testcase1) self.assertNotIn("testcase_def", testcase1)
self.assertEqual(len(testcase1["tests"]), 4) self.assertEqual(len(testcase1["teststeps"]), 2)
self.assertEqual( self.assertEqual(
testcase1["tests"][0]["tests"][0]["request"]["url"], testcase1["teststeps"][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["tests"][0]["tests"][0]["variables"]["device_sn"]), 15) self.assertEqual(len(testcase1["teststeps"][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"
tests_mapping = loader.load_tests(testcase_path) tests_mapping = loader.load_tests(testcase_path)
parser.parse_tests(tests_mapping) parsed_tests_mapping = parser.parse_tests(tests_mapping)
runner = HttpRunner() runner = HttpRunner()
test_suite = runner._add_tests(tests_mapping) test_suite = runner._add_tests(parsed_tests_mapping)
self.assertEqual(len(test_suite._tests), 1) self.assertEqual(len(test_suite._tests), 2)
tests = test_suite._tests[0].tests tests = test_suite._tests[0].teststeps
self.assertEqual(tests[0]["config"]["name"], "create user 1000 and check result.") self.assertEqual(tests[0]["config"]["name"], "setup and reset all (override).")
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"
tests_mapping = loader.load_tests(testcase_path) tests_mapping = loader.load_tests(testcase_path)
parser.parse_tests(tests_mapping) parsed_tests_mapping = parser.parse_tests(tests_mapping)
runner = HttpRunner() runner = HttpRunner()
test_suite = runner._add_tests(tests_mapping) test_suite = runner._add_tests(parsed_tests_mapping)
tests_results = runner._run_suite(test_suite) tests_results = runner._run_suite(test_suite)
self.assertEqual(len(tests_results[0][1].records), 2) self.assertEqual(len(tests_results[0][1].records), 4)
results = tests_results[0][1] results = tests_results[0][1]
self.assertEqual( self.assertEqual(
results.records[0]["name"], results.records[0]["name"],
"create user 1000 and check result." "setup and reset all (override)."
) )
self.assertEqual( self.assertIn(
results.records[1]["name"], results.records[1]["name"],
"create user 1001 and check result." ["make sure user 1000 does not exist", "make sure user 1001 does not exist"]
) )
+149 -148
View File
@@ -186,6 +186,13 @@ class TestFileLoader(unittest.TestCase):
os.path.join(os.getcwd(), "tests", "debugtalk.py") os.path.join(os.getcwd(), "tests", "debugtalk.py")
) )
def test_load_folder_content(self):
path = os.path.join(os.getcwd(), "tests", "api")
items_mapping = loader.load_folder_content(path)
file_path = os.path.join(os.getcwd(), "tests", "api", "basic.yml")
self.assertIn(file_path, items_mapping)
self.assertIsInstance(items_mapping[file_path], list)
class TestModuleLoader(unittest.TestCase): class TestModuleLoader(unittest.TestCase):
@@ -235,7 +242,111 @@ class TestModuleLoader(unittest.TestCase):
) )
self.assertEqual(debugtalk_functions, {}) self.assertEqual(debugtalk_functions, {})
def test_load_tests(self):
class TestSuiteLoader(unittest.TestCase):
@classmethod
def setUpClass(cls):
loader.load_project_tests(os.path.join(os.getcwd(), "tests"))
cls.project_mapping = loader.project_mapping
cls.tests_def_mapping = loader.tests_def_mapping
def test_load_teststep_api(self):
raw_test = {
"name": "create user (override).",
"api": "api/create_user.yml",
"variables": [
{"uid": "999"}
]
}
teststep = loader.load_teststep(raw_test)
self.assertEqual(
"create user (override).",
teststep["name"]
)
self.assertIn("api_def", teststep)
api_def = teststep["api_def"]
self.assertEqual(api_def["name"], "create user")
self.assertEqual(api_def["request"]["url"], "/api/users/$uid")
def test_load_teststep_testcase(self):
raw_test = {
"name": "setup and reset all (override).",
"testcase": "testcases/setup.yml",
"variables": [
{"device_sn": "$device_sn"}
],
"output": ["token", "device_sn"]
}
testcase = loader.load_teststep(raw_test)
self.assertEqual(
"setup and reset all (override).",
testcase["name"]
)
tests = testcase["testcase_def"]["teststeps"]
self.assertEqual(len(tests), 2)
self.assertEqual(tests[0]["name"], "get token (setup)")
self.assertEqual(tests[1]["name"], "reset all users")
def test_load_test_file_api(self):
loaded_content = loader.load_test_file("tests/api/create_user.yml")
self.assertEqual(loaded_content["type"], "api")
self.assertIn("path", loaded_content)
self.assertIn("request", loaded_content)
self.assertEqual(loaded_content["request"]["url"], "/api/users/$uid")
def test_load_test_file_testcase(self):
loaded_content = loader.load_test_file("tests/testcases/setup.yml")
self.assertEqual(loaded_content["type"], "testcase")
self.assertIn("path", loaded_content)
self.assertIn("config", loaded_content)
self.assertEqual(loaded_content["config"]["name"], "setup and reset all.")
self.assertIn("teststeps", loaded_content)
self.assertEqual(len(loaded_content["teststeps"]), 2)
def test_load_test_file_testsuite(self):
loaded_content = loader.load_test_file("tests/testsuites/create_users.yml")
self.assertEqual(loaded_content["type"], "testsuite")
testcases = loaded_content["testcases"]
self.assertEqual(len(testcases), 2)
self.assertIn('create user 1000 and check result.', testcases)
self.assertIn('testcase_def', testcases["create user 1000 and check result."])
self.assertEqual(
testcases["create user 1000 and check result."]["testcase_def"]["config"]["name"],
"create user and check result."
)
def test_load_tests_api_file(self):
path = os.path.join(
os.getcwd(), 'tests/api/create_user.yml')
tests_mapping = loader.load_tests(path)
project_mapping = tests_mapping["project_mapping"]
api_list = tests_mapping["api"]
self.assertEqual(len(api_list), 1)
self.assertEqual(api_list[0]["request"]["url"], "/api/users/$uid")
def test_load_tests_testcase_file(self):
# absolute file path
path = os.path.join(
os.getcwd(), 'tests/data/demo_testcase_hardcode.json')
tests_mapping = loader.load_tests(path)
project_mapping = tests_mapping["project_mapping"]
testcases_list = tests_mapping["testcases"]
self.assertEqual(len(testcases_list), 1)
self.assertEqual(len(testcases_list[0]["teststeps"]), 3)
self.assertIn("get_sign", project_mapping["functions"])
# relative file path
path = 'tests/data/demo_testcase_hardcode.yml'
tests_mapping = loader.load_tests(path)
project_mapping = tests_mapping["project_mapping"]
testcases_list = tests_mapping["testcases"]
self.assertEqual(len(testcases_list), 1)
self.assertEqual(len(testcases_list[0]["teststeps"]), 3)
self.assertIn("get_sign", project_mapping["functions"])
def test_load_tests_testcase_file_2(self):
testcase_file_path = os.path.join( testcase_file_path = os.path.join(
os.getcwd(), 'tests/data/demo_testcase.yml') os.getcwd(), 'tests/data/demo_testcase.yml')
tests_mapping = loader.load_tests(testcase_file_path) tests_mapping = loader.load_tests(testcase_file_path)
@@ -255,83 +366,52 @@ class TestModuleLoader(unittest.TestCase):
"${ENV(PROJECT_KEY)}" "${ENV(PROJECT_KEY)}"
) )
def test_load_tests_testcase_file_with_api_ref(self):
class TestSuiteLoader(unittest.TestCase):
@classmethod
def setUpClass(cls):
loader.load_project_tests(os.path.join(os.getcwd(), "tests"))
cls.project_mapping = loader.project_mapping
cls.tests_def_mapping = loader.tests_def_mapping
def test_load_test_testcase(self):
raw_test = {
"name": "setup and reset all (override).",
"testcase": "testcases/setup.yml",
"variables": [
{"device_sn": "$device_sn"}
],
"output": ["token", "device_sn"]
}
testcase = loader.load_test(raw_test)
self.assertEqual(
"setup and reset all (override).",
testcase["name"]
)
tests = testcase["testcase_def"]["tests"]
self.assertEqual(len(tests), 2)
self.assertEqual(tests[0]["name"], "get token (setup)")
self.assertEqual(tests[1]["name"], "reset all users")
def test_load_testcase(self):
raw_testcase = loader.load_file("tests/testsuites/create_users.yml")
testcase = loader.load_testcase(raw_testcase)
self.assertEqual(testcase["config"]["name"], "create users with uid")
self.assertIn("device_sn", testcase["config"]["variables"])
self.assertEqual(len(testcase["tests"]), 2)
self.assertEqual(testcase["tests"][0]["name"], "create user 1000 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):
# absolute file path
path = os.path.join( path = os.path.join(
os.getcwd(), 'tests/data/demo_testcase_hardcode.json') os.getcwd(), 'tests/data/demo_testcase_layer.yml')
tests_mapping = loader.load_tests(path) tests_mapping = loader.load_tests(path)
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.assertIn('device_sn', testcases_list[0]["config"]["variables"])
self.assertEqual(len(testcases_list[0]["tests"]), 3) self.assertIn("gen_md5", project_mapping["functions"])
self.assertIn("get_sign", project_mapping["functions"]) self.assertIn("base_url", testcases_list[0]["config"])
test_dict0 = testcases_list[0]["teststeps"][0]
self.assertEqual(
"get token with $user_agent, $app_version",
test_dict0["name"]
)
self.assertIn("/api/get-token", test_dict0["api_def"]["request"]["url"])
self.assertIn(
{'eq': ['status_code', 200]},
test_dict0["validate"]
)
# relative file path def test_load_tests_testsuite_file_with_testcase_ref(self):
path = 'tests/data/demo_testcase_hardcode.yml' path = os.path.join(
os.getcwd(), 'tests/testsuites/create_users.yml')
tests_mapping = loader.load_tests(path) tests_mapping = loader.load_tests(path)
project_mapping = tests_mapping["project_mapping"] project_mapping = tests_mapping["project_mapping"]
testcases_list = tests_mapping["testcases"] testsuites_list = tests_mapping["testsuites"]
self.assertEqual(len(testcases_list), 1)
self.assertEqual(len(testcases_list[0]["tests"]), 3)
self.assertIn("get_sign", project_mapping["functions"])
# TODO: list/set container with file(s) self.assertEqual(
# path = [ "create users with uid",
# os.path.join(os.getcwd(), 'tests/data/demo_testcase_hardcode.json'), testsuites_list[0]["config"]["name"]
# 'tests/data/demo_testcase_hardcode.yml' )
# ] self.assertEqual(
# testcases_list = loader.load_tests(path) {'device_sn': '${gen_random_string(15)}'},
# self.assertEqual(len(testcases_list), 2) testsuites_list[0]["config"]["variables"]
# self.assertEqual(len(testcases_list[0]["tests"]), 3) )
# self.assertEqual(len(testcases_list[1]["tests"]), 3) self.assertIn(
# testcases_list.extend(testcases_list) "create user 1000 and check result.",
# self.assertEqual(len(testcases_list), 4) testsuites_list[0]["testcases"]
)
# for testcase in testcases_list: self.assertEqual(
# for test_dict in testcase["tests"]: testsuites_list[0]["testcases"]["create user 1000 and check result."]["testcase_def"]["config"]["name"],
# self.assertIn('name', test_dict) "create user and check result."
# self.assertIn('request', test_dict) )
# self.assertIn('url', test_dict['request'])
# self.assertIn('method', test_dict['request'])
def test_load_testcases_by_path_folder(self): def test_load_tests_folder_path(self):
# absolute folder path # absolute folder path
path = os.path.join(os.getcwd(), 'tests/data') path = os.path.join(os.getcwd(), 'tests/data')
tests_mapping = loader.load_tests(path) tests_mapping = loader.load_tests(path)
@@ -344,16 +424,7 @@ class TestSuiteLoader(unittest.TestCase):
testcase_list_2 = tests_mapping["testcases"] testcase_list_2 = tests_mapping["testcases"]
self.assertEqual(len(testcase_list_1), len(testcase_list_2)) self.assertEqual(len(testcase_list_1), len(testcase_list_2))
# TODO: list/set container with file(s) def test_load_tests_path_not_exist(self):
# path = [
# os.path.join(os.getcwd(), 'tests/data'),
# 'tests/data/'
# ]
# tests_mapping = loader.load_tests(path)
# testcase_list_3 = tests_mapping["testcases"]
# self.assertEqual(len(testcase_list_3), 2 * len(testcase_list_1))
def test_load_testcases_by_path_not_exist(self):
# absolute folder path # absolute folder path
path = os.path.join(os.getcwd(), 'tests/data_not_exist') path = os.path.join(os.getcwd(), 'tests/data_not_exist')
with self.assertRaises(exceptions.FileNotFound): with self.assertRaises(exceptions.FileNotFound):
@@ -364,76 +435,6 @@ class TestSuiteLoader(unittest.TestCase):
with self.assertRaises(exceptions.FileNotFound): with self.assertRaises(exceptions.FileNotFound):
loader.load_tests(path) loader.load_tests(path)
# TODO: list/set container with file(s)
# path = [
# os.path.join(os.getcwd(), 'tests/data_not_exist'),
# 'tests/data_not_exist/'
# ]
# with self.assertRaises(exceptions.FileNotFound):
# loader.load_tests(path)
def test_load_testcases_with_api_ref(self):
path = os.path.join(
os.getcwd(), 'tests/data/demo_testcase_layer.yml')
tests_mapping = loader.load_tests(path)
project_mapping = tests_mapping["project_mapping"]
testcases_list = tests_mapping["testcases"]
self.assertIn('device_sn', testcases_list[0]["config"]["variables"])
self.assertIn("gen_md5", project_mapping["functions"])
self.assertIn("base_url", testcases_list[0]["config"])
test_dict0 = testcases_list[0]["tests"][0]
self.assertEqual(
"get token with $user_agent, $app_version",
test_dict0["name"]
)
self.assertIn("/api/get-token", test_dict0["api_def"]["request"]["url"])
self.assertIn(
{'eq': ['status_code', 200]},
test_dict0["validate"]
)
def test_load_testcases_with_testcase_ref(self):
path = os.path.join(
os.getcwd(), 'tests/testsuites/create_users.yml')
tests_mapping = loader.load_tests(path)
project_mapping = tests_mapping["project_mapping"]
testcases_list = tests_mapping["testcases"]
self.assertEqual(
"create users with uid",
testcases_list[0]["config"]["name"]
)
self.assertEqual(
{'device_sn': '${gen_random_string(15)}'},
testcases_list[0]["config"]["variables"]
)
testcase0 = testcases_list[0]["tests"][0]
self.assertEqual(
"create user 1000 and check result.",
testcase0["name"]
)
self.assertEqual(
"create user and check result.",
testcase0["testcase_def"]["config"]["name"]
)
testcase1 = testcases_list[0]["tests"][1]
self.assertEqual(
"create user 1001 and check result.",
testcase1["name"]
)
self.assertEqual(
{'uid': 1001},
testcase1["variables"]
)
def test_load_folder_content(self):
path = os.path.join(os.getcwd(), "tests", "api")
items_mapping = loader.load_folder_content(path)
file_path = os.path.join(os.getcwd(), "tests", "api", "basic.yml")
self.assertIn(file_path, items_mapping)
self.assertIsInstance(items_mapping[file_path], list)
def test_load_api_folder(self): def test_load_api_folder(self):
path = os.path.join(os.getcwd(), "tests", "api") path = os.path.join(os.getcwd(), "tests", "api")
api_definition_mapping = loader.load_api_folder(path) api_definition_mapping = loader.load_api_folder(path)
+25 -25
View File
@@ -429,7 +429,7 @@ class TestParser(unittest.TestCase):
3 * 2 * 3 3 * 2 * 3
) )
def test_parse_tests(self): def test_parse_tests_testcase(self):
testcase_file_path = os.path.join( testcase_file_path = os.path.join(
os.getcwd(), 'tests/data/demo_testcase.yml') os.getcwd(), 'tests/data/demo_testcase.yml')
tests_mapping = loader.load_tests(testcase_file_path) tests_mapping = loader.load_tests(testcase_file_path)
@@ -442,10 +442,10 @@ class TestParser(unittest.TestCase):
testcases[0]["config"]["variables"]["PROJECT_KEY"], testcases[0]["config"]["variables"]["PROJECT_KEY"],
"${ENV(PROJECT_KEY)}" "${ENV(PROJECT_KEY)}"
) )
parser.parse_tests(tests_mapping) parsed_tests_mapping = parser.parse_tests(tests_mapping)
parsed_testcases = tests_mapping["testcases"] parsed_testcases = parsed_tests_mapping["testcases"]
self.assertIsInstance(parsed_testcases, list) self.assertIsInstance(parsed_testcases, list)
test_dict1 = parsed_testcases[0]["tests"][0] test_dict1 = parsed_testcases[0]["teststeps"][0]
self.assertEqual(test_dict1["variables"]["var_c"], 3) self.assertEqual(test_dict1["variables"]["var_c"], 3)
self.assertEqual(test_dict1["variables"]["PROJECT_KEY"], "ABCDEFGH") self.assertEqual(test_dict1["variables"]["PROJECT_KEY"], "ABCDEFGH")
# TODO: parameters # TODO: parameters
@@ -456,14 +456,14 @@ class TestParser(unittest.TestCase):
tests_mapping = { tests_mapping = {
'testcases': [ 'testcases': [
{ {
'config': { "config": {
'name': '', 'name': '',
'variables': [ 'variables': [
{"password": "123456"}, {"password": "123456"},
{"creator": "user_test_001"} {"creator": "user_test_001"}
] ]
}, },
'tests': [ "teststeps": [
{ {
'name': 'testcase1', 'name': 'testcase1',
"variables": [ "variables": [
@@ -476,8 +476,8 @@ class TestParser(unittest.TestCase):
} }
] ]
} }
parser.parse_tests(tests_mapping) parsed_tests_mapping = parser.parse_tests(tests_mapping)
test_dict1_variables = tests_mapping["testcases"][0]["tests"][0]["variables"] test_dict1_variables = parsed_tests_mapping["testcases"][0]["teststeps"][0]["variables"]
self.assertEqual(test_dict1_variables["creator"], "user_test_001") self.assertEqual(test_dict1_variables["creator"], "user_test_001")
self.assertEqual(test_dict1_variables["username"], "$creator") self.assertEqual(test_dict1_variables["username"], "$creator")
@@ -487,7 +487,7 @@ class TestParser(unittest.TestCase):
tests_mapping = { tests_mapping = {
'testcases': [ 'testcases': [
{ {
'config': { "config": {
'name': '', 'name': '',
"base_url": "$host", "base_url": "$host",
'variables': { 'variables': {
@@ -495,7 +495,7 @@ class TestParser(unittest.TestCase):
}, },
"verify": False "verify": False
}, },
'tests': [ "teststeps": [
{ {
'name': 'testcase1', 'name': 'testcase1',
"base_url": "https://httprunner.org", "base_url": "https://httprunner.org",
@@ -505,8 +505,8 @@ class TestParser(unittest.TestCase):
} }
] ]
} }
parser.parse_tests(tests_mapping) parsed_tests_mapping = parser.parse_tests(tests_mapping)
test_dict = tests_mapping["testcases"][0]["tests"][0] test_dict = parsed_tests_mapping["testcases"][0]["teststeps"][0]
self.assertEqual(test_dict["request"]["url"], "https://httprunner.org/api1") self.assertEqual(test_dict["request"]["url"], "https://httprunner.org/api1")
self.assertEqual(test_dict["request"]["verify"], True) self.assertEqual(test_dict["request"]["verify"], True)
@@ -514,14 +514,14 @@ class TestParser(unittest.TestCase):
tests_mapping = { tests_mapping = {
'testcases': [ 'testcases': [
{ {
'config': { "config": {
'name': '', 'name': '',
"base_url": "$host1", "base_url": "$host1",
'variables': { 'variables': {
"host1": "https://debugtalk" "host1": "https://debugtalk"
} }
}, },
'tests': [ "teststeps": [
{ {
'name': 'testcase1', 'name': 'testcase1',
"variables": { "variables": {
@@ -533,22 +533,22 @@ class TestParser(unittest.TestCase):
} }
] ]
} }
parser.parse_tests(tests_mapping) parsed_tests_mapping = parser.parse_tests(tests_mapping)
test_dict = tests_mapping["testcases"][0]["tests"][0] test_dict = parsed_tests_mapping["testcases"][0]["teststeps"][0]
self.assertEqual(test_dict["request"]["url"], "https://httprunner.org/api1") self.assertEqual(test_dict["request"]["url"], "https://httprunner.org/api1")
def test_parse_tests_base_url_test_dict(self): def test_parse_tests_base_url_test_dict(self):
tests_mapping = { tests_mapping = {
'testcases': [ 'testcases': [
{ {
'config': { "config": {
'name': '', 'name': '',
"base_url": "$host1", "base_url": "$host1",
'variables': { 'variables': {
"host1": "https://debugtalk" "host1": "https://debugtalk"
} }
}, },
'tests': [ "teststeps": [
{ {
'name': 'testcase1', 'name': 'testcase1',
"base_url": "$host2", "base_url": "$host2",
@@ -561,8 +561,8 @@ class TestParser(unittest.TestCase):
} }
] ]
} }
parser.parse_tests(tests_mapping) parsed_tests_mapping = parser.parse_tests(tests_mapping)
test_dict = tests_mapping["testcases"][0]["tests"][0] test_dict = parsed_tests_mapping["testcases"][0]["teststeps"][0]
self.assertEqual(test_dict["request"]["url"], "https://httprunner.org/api1") self.assertEqual(test_dict["request"]["url"], "https://httprunner.org/api1")
def test_parse_tests_base_url_teststep_empty(self): def test_parse_tests_base_url_teststep_empty(self):
@@ -571,7 +571,7 @@ class TestParser(unittest.TestCase):
tests_mapping = { tests_mapping = {
'testcases': [ 'testcases': [
{ {
'config': { "config": {
'name': '', 'name': '',
"base_url": "$host", "base_url": "$host",
'variables': { 'variables': {
@@ -579,7 +579,7 @@ class TestParser(unittest.TestCase):
}, },
"verify": False "verify": False
}, },
'tests': [ "teststeps": [
{ {
'name': 'testcase1', 'name': 'testcase1',
"base_url": "", "base_url": "",
@@ -589,8 +589,8 @@ class TestParser(unittest.TestCase):
} }
] ]
} }
parser.parse_tests(tests_mapping) parsed_tests_mapping = parser.parse_tests(tests_mapping)
test_dict = tests_mapping["testcases"][0]["tests"][0] test_dict = parsed_tests_mapping["testcases"][0]["teststeps"][0]
self.assertEqual(test_dict["request"]["url"], "https://debugtalk/api1") self.assertEqual(test_dict["request"]["url"], "https://debugtalk/api1")
self.assertEqual(test_dict["request"]["verify"], True) self.assertEqual(test_dict["request"]["verify"], True)
@@ -626,7 +626,7 @@ class TestParser(unittest.TestCase):
"base_url": "https://debugtalk.com", "base_url": "https://debugtalk.com",
"api": "get_token", "api": "get_token",
} }
api_def_dict = loader.load_test(raw_testinfo) api_def_dict = loader.load_teststep(raw_testinfo)
test_block = { test_block = {
"name": "override block", "name": "override block",
"times": 3, "times": 3,
+1 -1
View File
@@ -244,7 +244,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["tests"][0]["request"]["headers"] test_dict_headers = testcase["teststeps"][0]["request"]["headers"]
headers = deep_update_dict( headers = deep_update_dict(
config_dict_headers, config_dict_headers,
test_dict_headers test_dict_headers
+1 -1
View File
@@ -24,7 +24,7 @@ class TestValidator(unittest.TestCase):
"path": "testcase1_path", "path": "testcase1_path",
"variables": [], # optional "variables": [], # optional
}, },
"tests": [ "teststeps": [
# test data structure # test data structure
{ {
'name': 'test step desc1', 'name': 'test step desc1',
+12 -11
View File
@@ -1,17 +1,18 @@
- config: config:
name: create users with uid name: create users with uid
variables: variables:
device_sn: ${gen_random_string(15)} device_sn: ${gen_random_string(15)}
base_url: "http://127.0.0.1:5000" base_url: "http://127.0.0.1:5000"
- test: testcases:
name: create user 1000 and check result. create user 1000 and check result.:
testcase: testcases/create_and_check.yml testcase: testcases/create_and_check.yml
variables: weight: 2
uid: 1000 variables:
uid: 1000
- test: create user 1001 and check result.:
name: create user 1001 and check result. testcase: testcases/create_and_check.yml
testcase: testcases/create_and_check.yml weight: 23423
variables: variables:
uid: 1001 uid: 1001