mirror of
https://github.com/httprunner/httprunner.git
synced 2026-06-04 23:39:33 +08:00
refactor variable context:
1, variable context has two level, testset and testcase; 2, testset level variables can be used in whole test suite, while testcase level variables can only be used in testcase; 3, when variable binds with functions, the funtions will be called and the result will be set to the variable.
This commit is contained in:
@@ -1,10 +1,4 @@
|
||||
register_variables:
|
||||
variable_binds:
|
||||
- TOKEN: "debugtalk"
|
||||
- var: [1, 2, 3]
|
||||
- data: {'name': 'user', 'password': '123456'}
|
||||
|
||||
register_template_variables:
|
||||
bind_variables:
|
||||
variable_binds:
|
||||
- TOKEN: "debugtalk"
|
||||
- token: $TOKEN
|
||||
|
||||
@@ -4,14 +4,13 @@
|
||||
- tests.data.custom_functions
|
||||
variable_binds:
|
||||
- TOKEN: debugtalk
|
||||
- json: {}
|
||||
- random: ${gen_random_string(5)}
|
||||
- authorization: ${gen_md5($TOKEN, $json, $random)}
|
||||
|
||||
- test:
|
||||
name: create user which does not exist
|
||||
variable_binds:
|
||||
- json: {"name": "user", "password": "123456"}
|
||||
- random: ${gen_random_string(5)}
|
||||
- authorization: ${gen_md5($TOKEN, $json, $random)}
|
||||
request:
|
||||
url: http://127.0.0.1:5000/api/users/1000
|
||||
method: POST
|
||||
@@ -28,6 +27,8 @@
|
||||
name: create user which does not exist
|
||||
variable_binds:
|
||||
- json: {"name": "user", "password": "123456"}
|
||||
- random: ${gen_random_string(5)}
|
||||
- authorization: ${gen_md5($TOKEN, $json, $random)}
|
||||
request:
|
||||
url: http://127.0.0.1:5000/api/users/1000
|
||||
method: POST
|
||||
|
||||
@@ -9,9 +9,6 @@
|
||||
gen_md5: "lambda *str_args: hashlib.md5(''.join(str_args).encode('utf-8')).hexdigest()"
|
||||
variable_binds:
|
||||
- TOKEN: debugtalk
|
||||
- data: ""
|
||||
- random: ${gen_random_string(5)}
|
||||
- authorization: ${gen_md5($TOKEN, $data, $random)}
|
||||
request:
|
||||
base_url: http://127.0.0.1:5000
|
||||
|
||||
@@ -19,6 +16,8 @@
|
||||
name: create user which does not exist
|
||||
variable_binds:
|
||||
- data: '{"name": "user", "password": "123456"}'
|
||||
- random: ${gen_random_string(5)}
|
||||
- authorization: ${gen_md5($TOKEN, $data, $random)}
|
||||
request:
|
||||
url: /api/users/1000
|
||||
method: POST
|
||||
@@ -35,6 +34,8 @@
|
||||
name: create user which does exist
|
||||
variable_binds:
|
||||
- data: '{"name": "user", "password": "123456"}'
|
||||
- random: ${gen_random_string(5)}
|
||||
- authorization: ${gen_md5($TOKEN, $data, $random)}
|
||||
- expected_status_code: 500
|
||||
request:
|
||||
url: /api/users/1000
|
||||
|
||||
@@ -12,51 +12,51 @@ class VariableBindsUnittest(unittest.TestCase):
|
||||
testcase_file_path = os.path.join(os.getcwd(), 'tests/data/demo_binds.yml')
|
||||
self.testcases = utils.load_testcases(testcase_file_path)
|
||||
|
||||
def test_context_register_variables(self):
|
||||
def test_context_bind_testset_variables(self):
|
||||
# testcase in JSON format
|
||||
testcase1 = {
|
||||
"variable_binds": [
|
||||
{"TOKEN": "debugtalk"},
|
||||
{"var": [1, 2, 3]},
|
||||
{"data": {'name': 'user', 'password': '123456'}}
|
||||
]
|
||||
}
|
||||
# testcase in YAML format
|
||||
testcase2 = self.testcases["register_variables"]
|
||||
|
||||
for testcase in [testcase1, testcase2]:
|
||||
variable_binds = testcase['variable_binds']
|
||||
self.context.register_variables_config(variable_binds)
|
||||
|
||||
context_variables = self.context._get_evaluated_testcase_variables()
|
||||
self.assertIn("TOKEN", context_variables)
|
||||
self.assertEqual(context_variables["TOKEN"], "debugtalk")
|
||||
self.assertIn("var", context_variables)
|
||||
self.assertEqual(context_variables["var"], [1, 2, 3])
|
||||
self.assertIn("data", context_variables)
|
||||
self.assertEqual(
|
||||
context_variables["data"],
|
||||
{'name': 'user', 'password': '123456'}
|
||||
)
|
||||
|
||||
def test_context_register_template_variables(self):
|
||||
testcase1 = {
|
||||
"variable_binds": [
|
||||
{"GLOBAL_TOKEN": "debugtalk"},
|
||||
{"token": "$GLOBAL_TOKEN"}
|
||||
]
|
||||
}
|
||||
testcase2 = self.testcases["register_template_variables"]
|
||||
# testcase in YAML format
|
||||
testcase2 = self.testcases["bind_variables"]
|
||||
|
||||
for testcase in [testcase1, testcase2]:
|
||||
variable_binds = testcase['variable_binds']
|
||||
self.context.register_variables_config(variable_binds)
|
||||
self.context.bind_variables(variable_binds, level="testset")
|
||||
|
||||
context_variables = self.context._get_evaluated_testcase_variables()
|
||||
self.assertIn("GLOBAL_TOKEN", context_variables)
|
||||
self.assertEqual(context_variables["GLOBAL_TOKEN"], "debugtalk")
|
||||
self.assertIn("token", context_variables)
|
||||
self.assertEqual(context_variables["token"], "debugtalk")
|
||||
testset_variables = self.context.testset_shared_variables_mapping
|
||||
testcase_variables = self.context.get_testcase_variables_mapping()
|
||||
self.assertIn("GLOBAL_TOKEN", testset_variables)
|
||||
self.assertIn("GLOBAL_TOKEN", testcase_variables)
|
||||
self.assertEqual(testset_variables["GLOBAL_TOKEN"], "debugtalk")
|
||||
self.assertIn("token", testset_variables)
|
||||
self.assertIn("token", testcase_variables)
|
||||
self.assertEqual(testset_variables["token"], "debugtalk")
|
||||
|
||||
def test_context_bind_testcase_variables(self):
|
||||
testcase1 = {
|
||||
"variable_binds": [
|
||||
{"GLOBAL_TOKEN": "debugtalk"},
|
||||
{"token": "$GLOBAL_TOKEN"}
|
||||
]
|
||||
}
|
||||
testcase2 = self.testcases["bind_variables"]
|
||||
|
||||
for testcase in [testcase1, testcase2]:
|
||||
variable_binds = testcase['variable_binds']
|
||||
self.context.bind_variables(variable_binds)
|
||||
|
||||
testset_variables = self.context.testset_shared_variables_mapping
|
||||
testcase_variables = self.context.get_testcase_variables_mapping()
|
||||
self.assertNotIn("GLOBAL_TOKEN", testset_variables)
|
||||
self.assertIn("GLOBAL_TOKEN", testcase_variables)
|
||||
self.assertEqual(testcase_variables["GLOBAL_TOKEN"], "debugtalk")
|
||||
self.assertNotIn("token", testset_variables)
|
||||
self.assertIn("token", testcase_variables)
|
||||
self.assertEqual(testcase_variables["token"], "debugtalk")
|
||||
|
||||
def test_context_bind_lambda_functions(self):
|
||||
testcase1 = {
|
||||
@@ -76,9 +76,9 @@ class VariableBindsUnittest(unittest.TestCase):
|
||||
self.context.bind_functions(function_binds)
|
||||
|
||||
variable_binds = testcase['variable_binds']
|
||||
self.context.register_variables_config(variable_binds)
|
||||
self.context.bind_variables(variable_binds)
|
||||
|
||||
context_variables = self.context._get_evaluated_testcase_variables()
|
||||
context_variables = self.context.get_testcase_variables_mapping()
|
||||
self.assertIn("add1", context_variables)
|
||||
self.assertEqual(context_variables["add1"], 3)
|
||||
self.assertIn("sum2nums", context_variables)
|
||||
@@ -108,8 +108,8 @@ class VariableBindsUnittest(unittest.TestCase):
|
||||
self.context.bind_functions(function_binds)
|
||||
|
||||
variable_binds = testcase['variable_binds']
|
||||
self.context.register_variables_config(variable_binds)
|
||||
context_variables = self.context._get_evaluated_testcase_variables()
|
||||
self.context.bind_variables(variable_binds)
|
||||
context_variables = self.context.get_testcase_variables_mapping()
|
||||
|
||||
self.assertIn("TOKEN", context_variables)
|
||||
TOKEN = context_variables["TOKEN"]
|
||||
@@ -142,8 +142,8 @@ class VariableBindsUnittest(unittest.TestCase):
|
||||
self.context.import_module_functions(module_functions)
|
||||
|
||||
variable_binds = testcase['variable_binds']
|
||||
self.context.register_variables_config(variable_binds)
|
||||
context_variables = self.context._get_evaluated_testcase_variables()
|
||||
self.context.bind_variables(variable_binds)
|
||||
context_variables = self.context.get_testcase_variables_mapping()
|
||||
|
||||
self.assertIn("TOKEN", context_variables)
|
||||
TOKEN = context_variables["TOKEN"]
|
||||
|
||||
@@ -49,40 +49,44 @@ class TestResponse(ApiServerUnittest):
|
||||
}
|
||||
)
|
||||
|
||||
extract_binds = {
|
||||
"resp_status_code": "status_code",
|
||||
"resp_headers_content_type": "headers.content-type",
|
||||
"resp_content_body_success": "body.success",
|
||||
"resp_content_content_success": "content.success",
|
||||
"resp_content_text_success": "text.success",
|
||||
"resp_content_person_first_name": "content.person.name.first_name",
|
||||
"resp_content_cities_1": "content.person.cities.1"
|
||||
}
|
||||
extract_binds_list = [
|
||||
{"resp_status_code": "status_code"},
|
||||
{"resp_headers_content_type": "headers.content-type"},
|
||||
{"resp_content_body_success": "body.success"},
|
||||
{"resp_content_content_success": "content.success"},
|
||||
{"resp_content_text_success": "text.success"},
|
||||
{"resp_content_person_first_name": "content.person.name.first_name"},
|
||||
{"resp_content_cities_1": "content.person.cities.1"}
|
||||
]
|
||||
resp_obj = response.ResponseObject(resp)
|
||||
extract_binds_dict = resp_obj.extract_response(extract_binds)
|
||||
extract_binds_dict_list = resp_obj.extract_response(extract_binds_list)
|
||||
|
||||
self.assertEqual(
|
||||
extract_binds_dict["resp_status_code"],
|
||||
extract_binds_dict_list[0]["resp_status_code"],
|
||||
200
|
||||
)
|
||||
self.assertEqual(
|
||||
extract_binds_dict["resp_headers_content_type"],
|
||||
extract_binds_dict_list[1]["resp_headers_content_type"],
|
||||
"application/json"
|
||||
)
|
||||
self.assertEqual(
|
||||
extract_binds_dict["resp_content_content_success"],
|
||||
extract_binds_dict_list[2]["resp_content_body_success"],
|
||||
False
|
||||
)
|
||||
self.assertEqual(
|
||||
extract_binds_dict["resp_content_text_success"],
|
||||
extract_binds_dict_list[3]["resp_content_content_success"],
|
||||
False
|
||||
)
|
||||
self.assertEqual(
|
||||
extract_binds_dict["resp_content_person_first_name"],
|
||||
extract_binds_dict_list[4]["resp_content_text_success"],
|
||||
False
|
||||
)
|
||||
self.assertEqual(
|
||||
extract_binds_dict_list[5]["resp_content_person_first_name"],
|
||||
"Leo"
|
||||
)
|
||||
self.assertEqual(
|
||||
extract_binds_dict["resp_content_cities_1"],
|
||||
extract_binds_dict_list[6]["resp_content_cities_1"],
|
||||
"Shenzhen"
|
||||
)
|
||||
|
||||
@@ -107,21 +111,21 @@ class TestResponse(ApiServerUnittest):
|
||||
}
|
||||
)
|
||||
|
||||
extract_binds = {
|
||||
"resp_content_dict_key_error": "content.not_exist"
|
||||
}
|
||||
extract_binds_list = [
|
||||
{"resp_content_dict_key_error": "content.not_exist"}
|
||||
]
|
||||
resp_obj = response.ResponseObject(resp)
|
||||
|
||||
with self.assertRaises(exception.ParamsError):
|
||||
resp_obj.extract_response(extract_binds)
|
||||
resp_obj.extract_response(extract_binds_list)
|
||||
|
||||
extract_binds = {
|
||||
"resp_content_list_index_error": "content.person.cities.3"
|
||||
}
|
||||
extract_binds_list = [
|
||||
{"resp_content_list_index_error": "content.person.cities.3"}
|
||||
]
|
||||
resp_obj = response.ResponseObject(resp)
|
||||
|
||||
with self.assertRaises(exception.ParamsError):
|
||||
resp_obj.extract_response(extract_binds)
|
||||
resp_obj.extract_response(extract_binds_list)
|
||||
|
||||
def test_extract_response_json_string(self):
|
||||
resp = requests.post(
|
||||
@@ -134,14 +138,14 @@ class TestResponse(ApiServerUnittest):
|
||||
}
|
||||
)
|
||||
|
||||
extract_binds = {
|
||||
"resp_content_body": "content"
|
||||
}
|
||||
extract_binds_list = [
|
||||
{"resp_content_body": "content"}
|
||||
]
|
||||
resp_obj = response.ResponseObject(resp)
|
||||
|
||||
extract_binds_dict = resp_obj.extract_response(extract_binds)
|
||||
extract_binds_dict_list = resp_obj.extract_response(extract_binds_list)
|
||||
self.assertEqual(
|
||||
extract_binds_dict["resp_content_body"],
|
||||
extract_binds_dict_list[0]["resp_content_body"],
|
||||
"abc"
|
||||
)
|
||||
|
||||
|
||||
@@ -41,11 +41,11 @@ class TestRunner(ApiServerUnittest):
|
||||
"password": "123456"
|
||||
}
|
||||
},
|
||||
"extract_binds": {
|
||||
"resp_status_code": "status_code",
|
||||
"resp_body_success": "content.success",
|
||||
"resp_headers_contenttype": "headers.content-type"
|
||||
},
|
||||
"extract_binds": [
|
||||
{"resp_status_code": "status_code"},
|
||||
{"resp_body_success": "content.success"},
|
||||
{"resp_headers_contenttype": "headers.content-type"}
|
||||
],
|
||||
"validators": [
|
||||
{"check": "resp_status_code", "comparator": "eq", "expected": 200},
|
||||
{"check": "resp_body_success", "comparator": "eq", "expected": False},
|
||||
|
||||
Reference in New Issue
Block a user