mirror of
https://github.com/httprunner/httprunner.git
synced 2026-05-13 04:50:11 +08:00
fix: pass output variables between testcases
This commit is contained in:
@@ -14,7 +14,8 @@ class SessionContext(object):
|
||||
|
||||
"""
|
||||
def __init__(self, variables=None):
|
||||
self.session_variables_mapping = utils.ensure_mapping_format(variables or {})
|
||||
variables_mapping = utils.ensure_mapping_format(variables or {})
|
||||
self.session_variables_mapping = parser.parse_variables_mapping(variables_mapping)
|
||||
self.init_test_variables()
|
||||
self.validation_results = []
|
||||
|
||||
|
||||
@@ -637,7 +637,6 @@ def parse_variables_mapping(variables_mapping, ignore=False):
|
||||
}
|
||||
|
||||
"""
|
||||
variables_mapping = variables_mapping or {}
|
||||
run_times = 0
|
||||
parsed_variables_mapping = {}
|
||||
|
||||
@@ -806,7 +805,7 @@ def _extend_with_testcase(test_dict, testcase_def_dict):
|
||||
test_dict.update(testcase_def_dict)
|
||||
|
||||
|
||||
def __prepare_config(config, project_mapping):
|
||||
def __prepare_config(config, project_mapping, session_variables_set=None):
|
||||
""" parse testcase/testsuite config.
|
||||
"""
|
||||
# get config variables
|
||||
@@ -821,12 +820,13 @@ def __prepare_config(config, project_mapping):
|
||||
if raw_config_variables_mapping:
|
||||
config["variables"] = raw_config_variables_mapping
|
||||
|
||||
check_variables_set = raw_config_variables_mapping.keys()
|
||||
check_variables_set = set(raw_config_variables_mapping.keys())
|
||||
check_variables_set |= (session_variables_set or set())
|
||||
prepared_config = prepare_lazy_data(config, functions, check_variables_set, cached=True)
|
||||
return prepared_config
|
||||
|
||||
|
||||
def __prepare_testcase_tests(tests, config, project_mapping):
|
||||
def __prepare_testcase_tests(tests, config, project_mapping, session_variables_set=None):
|
||||
""" override tests with testcase config variables, base_url and verify.
|
||||
test maybe nested testcase.
|
||||
|
||||
@@ -851,7 +851,7 @@ def __prepare_testcase_tests(tests, config, project_mapping):
|
||||
functions = project_mapping.get("functions", {})
|
||||
|
||||
prepared_testcase_tests = []
|
||||
session_variables_set = set()
|
||||
session_variables_set = set(config_variables.keys()) | (session_variables_set or set())
|
||||
for test_dict in tests:
|
||||
|
||||
teststep_variables_set = {"request", "response"}
|
||||
@@ -890,10 +890,7 @@ def __prepare_testcase_tests(tests, config, project_mapping):
|
||||
test_dict["config"].setdefault("verify", config_verify)
|
||||
|
||||
# 3, testcase_def config => testcase_def test_dict
|
||||
test_dict = _parse_testcase(test_dict, project_mapping)
|
||||
|
||||
config = test_dict.get("config", {})
|
||||
teststep_variables_set |= set(config.get("variables", []))
|
||||
test_dict = _parse_testcase(test_dict, project_mapping, session_variables_set)
|
||||
|
||||
elif "api_def" in test_dict:
|
||||
# test_dict has API reference
|
||||
@@ -948,7 +945,7 @@ def __prepare_testcase_tests(tests, config, project_mapping):
|
||||
return prepared_testcase_tests
|
||||
|
||||
|
||||
def _parse_testcase(testcase, project_mapping):
|
||||
def _parse_testcase(testcase, project_mapping, session_variables_set=None):
|
||||
""" parse testcase
|
||||
|
||||
Args:
|
||||
@@ -962,12 +959,14 @@ def _parse_testcase(testcase, project_mapping):
|
||||
testcase.setdefault("config", {})
|
||||
prepared_config = __prepare_config(
|
||||
testcase["config"],
|
||||
project_mapping
|
||||
project_mapping,
|
||||
session_variables_set
|
||||
)
|
||||
prepared_testcase_tests = __prepare_testcase_tests(
|
||||
testcase["teststeps"],
|
||||
prepared_config,
|
||||
project_mapping
|
||||
project_mapping,
|
||||
session_variables_set
|
||||
)
|
||||
return {
|
||||
"config": prepared_config,
|
||||
@@ -988,7 +987,7 @@ def __get_parsed_testsuite_testcases(testcases, testsuite_config, project_mappin
|
||||
testcases (dict):
|
||||
{
|
||||
"testcase1 name": {
|
||||
"testcase": "testcases/create_and_check.yml",
|
||||
"testcase": "testcases/create_user.yml",
|
||||
"weight": 2,
|
||||
"variables": {
|
||||
"uid": 1000
|
||||
|
||||
@@ -63,6 +63,7 @@ class Runner(object):
|
||||
self.verify = config.get("verify", True)
|
||||
self.output = config.get("output", [])
|
||||
self.validation_results = []
|
||||
config_variables = config.get("variables", {})
|
||||
|
||||
# testcase setup hooks
|
||||
testcase_setup_hooks = config.get("setup_hooks", [])
|
||||
@@ -70,7 +71,7 @@ class Runner(object):
|
||||
self.testcase_teardown_hooks = config.get("teardown_hooks", [])
|
||||
|
||||
self.http_client_session = http_client_session or HttpSession()
|
||||
self.session_context = SessionContext()
|
||||
self.session_context = SessionContext(config_variables)
|
||||
|
||||
if testcase_setup_hooks:
|
||||
self.do_hook_actions(testcase_setup_hooks, "setup")
|
||||
@@ -365,6 +366,9 @@ class Runner(object):
|
||||
self.meta_datas = None
|
||||
if "teststeps" in test_dict:
|
||||
# nested testcase
|
||||
test_dict.setdefault("config", {}).setdefault("variables", {})
|
||||
test_dict["config"]["variables"].update(
|
||||
self.session_context.session_variables_mapping)
|
||||
self._run_testcase(test_dict)
|
||||
else:
|
||||
# api
|
||||
|
||||
@@ -6,13 +6,13 @@ config:
|
||||
|
||||
testcases:
|
||||
create user 1000 and check result.:
|
||||
testcase: testcases/create_and_check.yml
|
||||
testcase: testcases/create_user.yml
|
||||
weight: 2
|
||||
variables:
|
||||
uid: 1000
|
||||
|
||||
create user 1001 and check result.:
|
||||
testcase: testcases/create_and_check.yml
|
||||
testcase: testcases/create_user.yml
|
||||
weight: 3
|
||||
variables:
|
||||
uid: 1001
|
||||
|
||||
@@ -138,11 +138,11 @@ class TestHttpRunner(ApiServerUnittest):
|
||||
self.assertEqual(len(vars_out), 6)
|
||||
self.assertEqual(vars_out[0]["in"]["uid"], 101)
|
||||
self.assertEqual(vars_out[0]["in"]["device_sn"], "TESTSUITE_X1")
|
||||
token1 = vars_out[0]["out"]["token"]
|
||||
token1 = vars_out[0]["out"]["session_token"]
|
||||
self.assertEqual(len(token1), 16)
|
||||
self.assertEqual(vars_out[5]["in"]["uid"], 103)
|
||||
self.assertEqual(vars_out[5]["in"]["device_sn"], "TESTSUITE_X2")
|
||||
token2 = vars_out[0]["out"]["token"]
|
||||
token2 = vars_out[0]["out"]["session_token"]
|
||||
self.assertEqual(len(token2), 16)
|
||||
self.assertEqual(token1, token2)
|
||||
|
||||
@@ -241,7 +241,7 @@ class TestHttpRunner(ApiServerUnittest):
|
||||
summary = self.runner.summary
|
||||
self.assertTrue(summary["success"])
|
||||
self.assertEqual(summary["stat"]["testcases"]["total"], 2)
|
||||
self.assertEqual(summary["stat"]["teststeps"]["total"], 8)
|
||||
self.assertEqual(summary["stat"]["teststeps"]["total"], 4)
|
||||
|
||||
def test_run_httprunner_with_hooks(self):
|
||||
testcase_file_path = os.path.join(
|
||||
@@ -471,7 +471,7 @@ class TestHttpRunner(ApiServerUnittest):
|
||||
self.assertEqual(len(summary["details"]), 3 * 2)
|
||||
|
||||
self.assertEqual(summary["stat"]["testcases"]["total"], 6)
|
||||
self.assertEqual(summary["stat"]["teststeps"]["total"], 3 * 2 * 4)
|
||||
self.assertEqual(summary["stat"]["teststeps"]["total"], 3 * 2 * 2)
|
||||
self.assertEqual(
|
||||
summary["details"][0]["name"],
|
||||
"create user 101 and check result for TESTSUITE_X1."
|
||||
@@ -482,10 +482,10 @@ class TestHttpRunner(ApiServerUnittest):
|
||||
)
|
||||
self.assertEqual(
|
||||
summary["details"][0]["stat"]["total"],
|
||||
4
|
||||
2
|
||||
)
|
||||
records_name_list = [
|
||||
summary["details"][i]["records"][2]["name"]
|
||||
summary["details"][i]["records"][1]["meta_datas"][1]["name"]
|
||||
for i in range(6)
|
||||
]
|
||||
self.assertEqual(
|
||||
@@ -610,20 +610,21 @@ class TestApi(ApiServerUnittest):
|
||||
self.assertIn("api", teststeps[0])
|
||||
|
||||
def test_testcase_complex_verify(self):
|
||||
testcase_path = "tests/testcases/create_and_check.yml"
|
||||
testcase_path = "tests/testcases/create_user.yml"
|
||||
tests_mapping = loader.load_tests(testcase_path)
|
||||
testcases = parser.parse_tests(tests_mapping)
|
||||
teststeps = testcases[0]["teststeps"]
|
||||
|
||||
# testcases/setup.yml
|
||||
teststep1 = teststeps[0]
|
||||
self.assertEqual(teststep1["teststeps"][0]["request"]["verify"], False)
|
||||
self.assertEqual(teststep1["teststeps"][1]["request"]["verify"], False)
|
||||
teststep0 = teststeps[0]
|
||||
self.assertEqual(teststep0["teststeps"][0]["request"]["verify"], False)
|
||||
self.assertEqual(teststep0["teststeps"][1]["request"]["verify"], False)
|
||||
|
||||
# testcases/create_and_check.yml teststep 2/3/4
|
||||
self.assertEqual(teststeps[1]["request"]["verify"], True)
|
||||
self.assertEqual(teststeps[2]["request"]["verify"], True)
|
||||
self.assertEqual(teststeps[3]["request"]["verify"], True)
|
||||
# testcases/create_user.yml
|
||||
teststep1 = teststeps[1]
|
||||
self.assertEqual(teststep1["teststeps"][0]["request"]["verify"], True)
|
||||
self.assertEqual(teststep1["teststeps"][1]["request"]["verify"], True)
|
||||
self.assertEqual(teststep1["teststeps"][2]["request"]["verify"], True)
|
||||
|
||||
def test_testcase_simple_run_suite(self):
|
||||
testcase_path = "tests/testcases/setup.yml"
|
||||
@@ -635,13 +636,13 @@ class TestApi(ApiServerUnittest):
|
||||
self.assertEqual(len(tests_results[0][1].records), 2)
|
||||
|
||||
def test_testcase_complex_run_suite(self):
|
||||
testcase_path = "tests/testcases/create_and_check.yml"
|
||||
testcase_path = "tests/testcases/create_user.yml"
|
||||
tests_mapping = loader.load_tests(testcase_path)
|
||||
testcases = parser.parse_tests(tests_mapping)
|
||||
runner = HttpRunner()
|
||||
test_suite = runner._add_tests(testcases)
|
||||
tests_results = runner._run_suite(test_suite)
|
||||
self.assertEqual(len(tests_results[0][1].records), 4)
|
||||
self.assertEqual(len(tests_results[0][1].records), 2)
|
||||
|
||||
results = tests_results[0][1]
|
||||
self.assertEqual(
|
||||
@@ -650,7 +651,7 @@ class TestApi(ApiServerUnittest):
|
||||
)
|
||||
self.assertEqual(
|
||||
results.records[1]["name"],
|
||||
"make sure user 9001 does not exist"
|
||||
"create user and check result."
|
||||
)
|
||||
|
||||
def test_testsuite_loader(self):
|
||||
@@ -679,7 +680,7 @@ class TestApi(ApiServerUnittest):
|
||||
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(len(testcase_tests["testcase_def"]["teststeps"]), 2)
|
||||
self.assertEqual(
|
||||
testcase_tests["testcase_def"]["teststeps"][0]["name"],
|
||||
"setup and reset all (override) for $device_sn."
|
||||
@@ -691,7 +692,7 @@ class TestApi(ApiServerUnittest):
|
||||
|
||||
parsed_testcases = parser.parse_tests(tests_mapping)
|
||||
self.assertEqual(len(parsed_testcases), 2)
|
||||
self.assertEqual(len(parsed_testcases[0]["teststeps"]), 4)
|
||||
self.assertEqual(len(parsed_testcases[0]["teststeps"]), 2)
|
||||
|
||||
testcase1 = parsed_testcases[0]["teststeps"][0]
|
||||
self.assertIn("setup and reset all (override)", testcase1["config"]["name"].raw_string)
|
||||
@@ -725,16 +726,16 @@ class TestApi(ApiServerUnittest):
|
||||
test_suite = runner._add_tests(testcases)
|
||||
tests_results = runner._run_suite(test_suite)
|
||||
|
||||
self.assertEqual(len(tests_results[0][1].records), 4)
|
||||
self.assertEqual(len(tests_results[0][1].records), 2)
|
||||
|
||||
results = tests_results[0][1]
|
||||
self.assertIn(
|
||||
"setup and reset all (override)",
|
||||
results.records[0]["name"]
|
||||
)
|
||||
self.assertIn(
|
||||
self.assertEqual(
|
||||
results.records[1]["name"],
|
||||
["make sure user 1000 does not exist", "make sure user 1001 does not exist"]
|
||||
"create user and check result."
|
||||
)
|
||||
|
||||
|
||||
|
||||
22
tests/testcases/create_user.yml
Normal file
22
tests/testcases/create_user.yml
Normal file
@@ -0,0 +1,22 @@
|
||||
|
||||
- config:
|
||||
name: "create user and check result."
|
||||
id: create_user
|
||||
base_url: "http://127.0.0.1:5000"
|
||||
variables:
|
||||
uid: 9001
|
||||
device_sn: "TESTCASE_CREATE_XXX"
|
||||
output:
|
||||
- session_token
|
||||
|
||||
- test:
|
||||
name: setup and reset all (override) for $device_sn.
|
||||
testcase: testcases/setup.yml
|
||||
output:
|
||||
- session_token
|
||||
|
||||
- test:
|
||||
name: create user and check result.
|
||||
variables:
|
||||
token: $session_token
|
||||
testcase: testcases/deps/check_and_create.yml
|
||||
@@ -1,4 +1,3 @@
|
||||
|
||||
- config:
|
||||
name: "create user and check result."
|
||||
id: create_and_check
|
||||
@@ -6,14 +5,6 @@
|
||||
variables:
|
||||
uid: 9001
|
||||
device_sn: "TESTCASE_CREATE_XXX"
|
||||
output:
|
||||
- token
|
||||
|
||||
- test:
|
||||
name: setup and reset all (override) for $device_sn.
|
||||
testcase: testcases/setup.yml
|
||||
output:
|
||||
- token
|
||||
|
||||
- test:
|
||||
name: make sure user $uid does not exist
|
||||
@@ -9,7 +9,7 @@
|
||||
base_url: "http://127.0.0.1:5000"
|
||||
verify: False
|
||||
output:
|
||||
- token
|
||||
- session_token
|
||||
|
||||
- test:
|
||||
name: get token (setup)
|
||||
@@ -20,7 +20,7 @@
|
||||
os_platform: 'ios'
|
||||
app_version: '2.8.6'
|
||||
extract:
|
||||
- token: content.token
|
||||
- session_token: content.token
|
||||
validate:
|
||||
- eq: ["status_code", 200]
|
||||
- len_eq: ["content.token", 16]
|
||||
@@ -29,4 +29,4 @@
|
||||
name: reset all users
|
||||
api: api/reset_all.yml
|
||||
variables:
|
||||
token: $token
|
||||
token: $session_token
|
||||
|
||||
@@ -8,14 +8,14 @@ config:
|
||||
|
||||
testcases:
|
||||
create user 1000 and check result.:
|
||||
testcase: testcases/create_and_check.yml
|
||||
testcase: testcases/create_user.yml
|
||||
variables:
|
||||
uid: 1000
|
||||
var_c: ${gen_random_string(5)}
|
||||
var_d: $var_c
|
||||
|
||||
create user 1001 and check result.:
|
||||
testcase: testcases/create_and_check.yml
|
||||
testcase: testcases/create_user.yml
|
||||
variables:
|
||||
uid: 1001
|
||||
var_c: ${gen_random_string(5)}
|
||||
|
||||
@@ -6,7 +6,7 @@ config:
|
||||
|
||||
testcases:
|
||||
create user $uid and check result for $device_sn.:
|
||||
testcase: testcases/create_and_check.yml
|
||||
testcase: testcases/create_user.yml
|
||||
variables:
|
||||
uid: 1000
|
||||
device_sn: TESTSUITE_XXX
|
||||
|
||||
Reference in New Issue
Block a user