mirror of
https://github.com/httprunner/httprunner.git
synced 2026-05-30 12:59:39 +08:00
feat: implement lazy parser for validators
This commit is contained in:
@@ -6,5 +6,6 @@ request:
|
||||
url: https://debugtalk.com
|
||||
status_code: 302
|
||||
method: GET
|
||||
verify: False
|
||||
validate:
|
||||
- eq: ["status_code", 200]
|
||||
|
||||
@@ -76,7 +76,7 @@ class TestHttpClient(ApiServerUnittest):
|
||||
"a": "1",
|
||||
"b": "2"
|
||||
}
|
||||
resp = self.api_client.get(url, cookies=cookies, headers=self.headers)
|
||||
resp = self.api_client.get(url, cookies=cookies, headers=self.headers, verify=False)
|
||||
raw_request = resp.history[0].request
|
||||
self.assertEqual(raw_request._cookies["a"], "1")
|
||||
self.assertEqual(raw_request._cookies["b"], "2")
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
import os
|
||||
import time
|
||||
|
||||
import requests
|
||||
from httprunner import context, exceptions, loader, parser, response, utils
|
||||
from httprunner import context, exceptions, loader, parser, runner
|
||||
from tests.base import ApiServerUnittest, gen_md5, gen_random_string
|
||||
|
||||
|
||||
@@ -11,16 +10,10 @@ class TestContext(ApiServerUnittest):
|
||||
def setUp(self):
|
||||
loader.load_project_tests(os.path.join(os.getcwd(), "tests"))
|
||||
project_mapping = loader.project_mapping
|
||||
self.functions = project_mapping["functions"]
|
||||
self.context = context.SessionContext(
|
||||
functions=self.functions,
|
||||
variables={"SECRET_KEY": "DebugTalk"}
|
||||
)
|
||||
|
||||
def test_init_context_functions(self):
|
||||
context_functions = self.context.FUNCTIONS_MAPPING
|
||||
self.assertIn("gen_md5", context_functions)
|
||||
|
||||
def test_init_test_variables_initialize(self):
|
||||
self.assertEqual(
|
||||
self.context.test_variables_mapping,
|
||||
@@ -57,13 +50,6 @@ class TestContext(ApiServerUnittest):
|
||||
"debugtalk"
|
||||
)
|
||||
|
||||
def test_eval_content_functions(self):
|
||||
content = parser.prepare_lazy_data("${sleep_N_secs(1)}", self.functions)
|
||||
start_time = time.time()
|
||||
self.context.eval_content(content)
|
||||
elapsed_time = time.time() - start_time
|
||||
self.assertGreater(elapsed_time, 1)
|
||||
|
||||
def test_eval_content_variables(self):
|
||||
variables = {
|
||||
"SECRET_KEY": "DebugTalk"
|
||||
@@ -124,82 +110,80 @@ class TestContext(ApiServerUnittest):
|
||||
)
|
||||
self.assertEqual(parsed_request["headers"]["secret_key"], "DebugTalk")
|
||||
|
||||
def test_do_validation(self):
|
||||
self.context._do_validation(
|
||||
{"check": "check", "check_value": 1, "expect": 1, "comparator": "eq"}
|
||||
)
|
||||
self.context._do_validation(
|
||||
{"check": "check", "check_value": "abc", "expect": "abc", "comparator": "=="}
|
||||
)
|
||||
self.context._do_validation(
|
||||
{"check": "status_code", "check_value": "201", "expect": 3, "comparator": "sum_status_code"}
|
||||
)
|
||||
|
||||
def test_validate(self):
|
||||
url = "http://127.0.0.1:5000/"
|
||||
resp = requests.get(url)
|
||||
resp_obj = response.ResponseObject(resp)
|
||||
|
||||
validators = [
|
||||
{"eq": ["$resp_status_code", 201]},
|
||||
{"check": "$resp_status_code", "comparator": "eq", "expect": 201},
|
||||
{"check": "$resp_body_success", "comparator": "eq", "expect": True}
|
||||
]
|
||||
validators = parser.prepare_lazy_data(validators, {}, {"resp_status_code", "resp_body_success"})
|
||||
variables = {
|
||||
"resp_status_code": 200,
|
||||
"resp_body_success": True
|
||||
}
|
||||
|
||||
self.context.init_test_variables(variables)
|
||||
|
||||
with self.assertRaises(exceptions.ValidationFailure):
|
||||
self.context.validate(validators, resp_obj)
|
||||
|
||||
validators = [
|
||||
{"eq": ["$resp_status_code", 201]},
|
||||
{"check": "$resp_status_code", "comparator": "eq", "expect": 201},
|
||||
{"check": "$resp_body_success", "comparator": "eq", "expect": True},
|
||||
{"check": "${is_status_code_200($resp_status_code)}", "comparator": "eq", "expect": False}
|
||||
testcases = [
|
||||
{
|
||||
"config": {
|
||||
'name': "test validation"
|
||||
},
|
||||
"teststeps": [
|
||||
{
|
||||
"name": "test validation",
|
||||
"request": {
|
||||
"url": "http://127.0.0.1:5000/",
|
||||
"method": "GET",
|
||||
},
|
||||
"variables": {
|
||||
"resp_status_code": 200,
|
||||
"resp_body_success": True
|
||||
},
|
||||
"validate": [
|
||||
{"eq": ["$resp_status_code", 200]},
|
||||
{"check": "$resp_status_code", "comparator": "eq", "expect": 200},
|
||||
{"check": "$resp_body_success", "expect": True},
|
||||
{"check": "${is_status_code_200($resp_status_code)}", "expect": True}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
from tests.debugtalk import is_status_code_200
|
||||
functions = {
|
||||
"is_status_code_200": is_status_code_200
|
||||
tests_mapping = {
|
||||
"project_mapping": {
|
||||
"functions": {
|
||||
"is_status_code_200": is_status_code_200
|
||||
}
|
||||
},
|
||||
"testcases": testcases
|
||||
}
|
||||
validators = parser.prepare_lazy_data(
|
||||
validators, functions, {"resp_status_code", "resp_body_success"})
|
||||
variables = [
|
||||
{"resp_status_code": 201},
|
||||
{"resp_body_success": True}
|
||||
]
|
||||
self.context.init_test_variables(variables)
|
||||
self.context.validate(validators, resp_obj)
|
||||
|
||||
self.context.validate([], resp_obj)
|
||||
self.assertEqual(self.context.validation_results, [])
|
||||
parsed_tests_mapping = parser.parse_tests(tests_mapping)
|
||||
parsed_testcase = parsed_tests_mapping["testcases"][0]
|
||||
test_runner = runner.Runner(parsed_testcase["config"])
|
||||
teststep = parsed_testcase["teststeps"][0]
|
||||
test_runner.run_test(teststep)
|
||||
|
||||
def test_validate_exception(self):
|
||||
url = "http://127.0.0.1:5000/"
|
||||
resp = requests.get(url)
|
||||
resp_obj = response.ResponseObject(resp)
|
||||
|
||||
# expected value missed in validators
|
||||
validators = [
|
||||
{"eq": ["$resp_status_code", 201]},
|
||||
{"check": "$resp_status_code", "comparator": "eq", "expect": 201}
|
||||
testcases = [
|
||||
{
|
||||
"config": {
|
||||
'name': "test validation"
|
||||
},
|
||||
"teststeps": [
|
||||
{
|
||||
"name": "test validation",
|
||||
"request": {
|
||||
"url": "http://127.0.0.1:5000/",
|
||||
"method": "GET",
|
||||
},
|
||||
"variables": {
|
||||
"resp_status_code": 200,
|
||||
"resp_body_success": True
|
||||
},
|
||||
"validate": [
|
||||
{"eq": ["$resp_status_code", 201]},
|
||||
{"check": "$resp_status_code", "expect": 201},
|
||||
{"check": "$resp_body_success", "comparator": "eq", "expect": True}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
validators = parser.prepare_lazy_data(validators, {}, {"resp_status_code"})
|
||||
variables = []
|
||||
self.context.init_test_variables(variables)
|
||||
|
||||
with self.assertRaises(exceptions.VariableNotFound):
|
||||
self.context.validate(validators, resp_obj)
|
||||
|
||||
# expected value missed in variables mapping
|
||||
variables = [
|
||||
{"resp_status_code": 200}
|
||||
]
|
||||
self.context.init_test_variables(variables)
|
||||
|
||||
tests_mapping = {
|
||||
"testcases": testcases
|
||||
}
|
||||
parsed_tests_mapping = parser.parse_tests(tests_mapping)
|
||||
parsed_testcase = parsed_tests_mapping["testcases"][0]
|
||||
test_runner = runner.Runner(parsed_testcase["config"])
|
||||
teststep = parsed_testcase["teststeps"][0]
|
||||
with self.assertRaises(exceptions.ValidationFailure):
|
||||
self.context.validate(validators, resp_obj)
|
||||
test_runner.run_test(teststep)
|
||||
|
||||
@@ -104,19 +104,6 @@ class TestParserBasic(unittest.TestCase):
|
||||
{'args': ["$request", '12 3'], 'kwargs': {}}
|
||||
)
|
||||
|
||||
def test_parse_validator(self):
|
||||
validator = {"check": "status_code", "comparator": "eq", "expect": 201}
|
||||
self.assertEqual(
|
||||
parser.parse_validator(validator),
|
||||
{"check": "status_code", "comparator": "eq", "expect": 201}
|
||||
)
|
||||
|
||||
validator = {'eq': ['status_code', 201]}
|
||||
self.assertEqual(
|
||||
parser.parse_validator(validator),
|
||||
{"check": "status_code", "comparator": "eq", "expect": 201}
|
||||
)
|
||||
|
||||
def test_extract_variables(self):
|
||||
prepared_content = parser.prepare_lazy_data("123$a", {}, {"a"})
|
||||
self.assertEqual(
|
||||
@@ -1076,15 +1063,15 @@ class TestParser(unittest.TestCase):
|
||||
'json': {'sign': '${get_sign($device_sn, $os_platform, $app_version)}'}
|
||||
},
|
||||
'validate': [
|
||||
{'eq': ['status_code', 201]},
|
||||
{'len_eq': ['content.token', 32]}
|
||||
{"check": "status_code", "comparator": "equals", "expect": 201},
|
||||
{"check": "content.token", "comparator": "length_equals", "expect": 32}
|
||||
]
|
||||
}
|
||||
|
||||
extended_block = parser._extend_with_api(test_block, api_def_dict)
|
||||
self.assertEqual(extended_block["base_url"], "https://debugtalk.com")
|
||||
self.assertEqual(extended_block["name"], "override block")
|
||||
self.assertEqual({'var': 123}, extended_block["variables"])
|
||||
self.assertIn({'check': 'status_code', 'expect': 201, 'comparator': 'eq'}, extended_block["validate"])
|
||||
self.assertIn({'check': 'content.token', 'comparator': 'len_eq', 'expect': 32}, extended_block["validate"])
|
||||
self.assertEqual(extended_block["times"], 3)
|
||||
parser._extend_with_api(test_block, api_def_dict)
|
||||
self.assertEqual(test_block["base_url"], "https://debugtalk.com")
|
||||
self.assertEqual(test_block["name"], "override block")
|
||||
self.assertEqual({'var': 123}, test_block["variables"])
|
||||
self.assertIn({'check': 'status_code', 'expect': 201, 'comparator': 'equals'}, test_block["validate"])
|
||||
self.assertIn({'check': 'content.token', 'comparator': 'length_equals', 'expect': 32}, test_block["validate"])
|
||||
self.assertEqual(test_block["times"], 3)
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
import os
|
||||
import time
|
||||
|
||||
from httprunner import exceptions, loader, parser, runner
|
||||
from httprunner.utils import deep_update_dict
|
||||
from httprunner import loader, parser, runner
|
||||
from tests.api_server import HTTPBIN_SERVER
|
||||
from tests.base import ApiServerUnittest
|
||||
|
||||
@@ -19,7 +18,7 @@ class TestRunner(ApiServerUnittest):
|
||||
"base_url": "http://127.0.0.1",
|
||||
"verify": False
|
||||
}
|
||||
self.test_runner = runner.Runner(config, self.debugtalk_functions)
|
||||
self.test_runner = runner.Runner(config)
|
||||
self.reset_all()
|
||||
|
||||
def reset_all(self):
|
||||
@@ -36,214 +35,253 @@ class TestRunner(ApiServerUnittest):
|
||||
]
|
||||
|
||||
for testcase_file_path in testcase_file_path_list:
|
||||
testcases = loader.load_file(testcase_file_path)
|
||||
testcases = parser.prepare_lazy_data(
|
||||
testcases,
|
||||
self.debugtalk_functions,
|
||||
{"expect_status_code", "token_len", "token", "success"}
|
||||
)
|
||||
config_dict = {}
|
||||
test_runner = runner.Runner(config_dict, self.debugtalk_functions)
|
||||
|
||||
test = testcases[0]["test"]
|
||||
test_runner.run_test(test)
|
||||
|
||||
test = testcases[1]["test"]
|
||||
test_runner.run_test(test)
|
||||
|
||||
test = testcases[2]["test"]
|
||||
test_runner.run_test(test)
|
||||
|
||||
def test_run_single_testcase_fail(self):
|
||||
test = {
|
||||
"name": "get token",
|
||||
"request": {
|
||||
"url": "http://127.0.0.1:5000/api/get-token",
|
||||
"method": "POST",
|
||||
"headers": {
|
||||
"content-type": "application/json",
|
||||
"user_agent": "iOS/10.3",
|
||||
"device_sn": "HZfFBh6tU59EdXJ",
|
||||
"os_platform": "ios",
|
||||
"app_version": "2.8.6"
|
||||
},
|
||||
"json": {
|
||||
"sign": "5188962c489d1a35effa99e9346dd5efd4fdabad"
|
||||
}
|
||||
},
|
||||
"validate": [
|
||||
{"check": "status_code", "expect": 205},
|
||||
{"check": "content.token", "comparator": "len_eq", "expect": 19}
|
||||
]
|
||||
}
|
||||
|
||||
with self.assertRaises(exceptions.ValidationFailure):
|
||||
self.test_runner.run_test(test)
|
||||
tests_mapping = loader.load_tests(testcase_file_path)
|
||||
parsed_tests_mapping = parser.parse_tests(tests_mapping)
|
||||
parsed_testcase = parsed_tests_mapping["testcases"][0]
|
||||
test_runner = runner.Runner(parsed_testcase["config"])
|
||||
test_runner.run_test(parsed_testcase["teststeps"][0])
|
||||
test_runner.run_test(parsed_testcase["teststeps"][1])
|
||||
test_runner.run_test(parsed_testcase["teststeps"][2])
|
||||
|
||||
def test_run_testcase_with_hooks(self):
|
||||
start_time = time.time()
|
||||
|
||||
config_dict = {
|
||||
"name": "basic test with httpbin",
|
||||
"base_url": HTTPBIN_SERVER,
|
||||
"setup_hooks": [
|
||||
"${sleep_N_secs(0.5)}",
|
||||
"${hook_print(setup)}"
|
||||
],
|
||||
"teardown_hooks": [
|
||||
"${sleep_N_secs(1)}",
|
||||
"${hook_print(teardown)}"
|
||||
]
|
||||
testcases = [
|
||||
{
|
||||
"config": {
|
||||
"name": "basic test with httpbin",
|
||||
"base_url": HTTPBIN_SERVER,
|
||||
"setup_hooks": [
|
||||
"${sleep_N_secs(0.5)}",
|
||||
"${hook_print(setup)}"
|
||||
],
|
||||
"teardown_hooks": [
|
||||
"${sleep_N_secs(1)}",
|
||||
"${hook_print(teardown)}"
|
||||
]
|
||||
},
|
||||
"teststeps": [
|
||||
{
|
||||
"name": "get token",
|
||||
"request": {
|
||||
"url": "http://127.0.0.1:5000/api/get-token",
|
||||
"method": "POST",
|
||||
"headers": {
|
||||
"content-type": "application/json",
|
||||
"user_agent": "iOS/10.3",
|
||||
"device_sn": "HZfFBh6tU59EdXJ",
|
||||
"os_platform": "ios",
|
||||
"app_version": "2.8.6"
|
||||
},
|
||||
"json": {
|
||||
"sign": "5188962c489d1a35effa99e9346dd5efd4fdabad"
|
||||
}
|
||||
},
|
||||
"validate": [
|
||||
{"check": "status_code", "expect": 200}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
tests_mapping = {
|
||||
"project_mapping": {
|
||||
"functions": self.debugtalk_functions
|
||||
},
|
||||
"testcases": testcases
|
||||
}
|
||||
prepared_config_dict = parser.prepare_lazy_data(config_dict, self.debugtalk_functions)
|
||||
test_runner = runner.Runner(prepared_config_dict, self.debugtalk_functions)
|
||||
parsed_tests_mapping = parser.parse_tests(tests_mapping)
|
||||
parsed_testcase = parsed_tests_mapping["testcases"][0]
|
||||
test_runner = runner.Runner(parsed_testcase["config"])
|
||||
end_time = time.time()
|
||||
# check if testcase setup hook executed
|
||||
self.assertGreater(end_time - start_time, 0.5)
|
||||
|
||||
start_time = time.time()
|
||||
test = {
|
||||
"name": "get token",
|
||||
"request": {
|
||||
"url": "http://127.0.0.1:5000/api/get-token",
|
||||
"method": "POST",
|
||||
"headers": {
|
||||
"content-type": "application/json",
|
||||
"user_agent": "iOS/10.3",
|
||||
"device_sn": "HZfFBh6tU59EdXJ",
|
||||
"os_platform": "ios",
|
||||
"app_version": "2.8.6"
|
||||
},
|
||||
"json": {
|
||||
"sign": "5188962c489d1a35effa99e9346dd5efd4fdabad"
|
||||
}
|
||||
},
|
||||
"validate": [
|
||||
{"check": "status_code", "expect": 200}
|
||||
]
|
||||
}
|
||||
test_runner.run_test(test)
|
||||
test_runner.run_test(test)
|
||||
test_runner.run_test(parsed_testcase["teststeps"][0])
|
||||
end_time = time.time()
|
||||
# testcase teardown hook has not been executed now
|
||||
self.assertLess(end_time - start_time, 1)
|
||||
|
||||
def test_run_testcase_with_hooks_assignment(self):
|
||||
config_dict = {
|
||||
"name": "basic test with httpbin",
|
||||
"base_url": HTTPBIN_SERVER
|
||||
}
|
||||
test = {
|
||||
"name": "modify request headers",
|
||||
"base_url": HTTPBIN_SERVER,
|
||||
"request": {
|
||||
"url": "/anything",
|
||||
"method": "POST",
|
||||
"headers": {
|
||||
"user_agent": "iOS/10.3",
|
||||
"os_platform": "ios"
|
||||
testcases = [
|
||||
{
|
||||
"config": {
|
||||
"name": "basic test with httpbin",
|
||||
"base_url": HTTPBIN_SERVER
|
||||
},
|
||||
"data": "a=1&b=2"
|
||||
"teststeps": [
|
||||
{
|
||||
"name": "modify request headers",
|
||||
"base_url": HTTPBIN_SERVER,
|
||||
"request": {
|
||||
"url": "/anything",
|
||||
"method": "POST",
|
||||
"headers": {
|
||||
"user_agent": "iOS/10.3",
|
||||
"os_platform": "ios"
|
||||
},
|
||||
"data": "a=1&b=2"
|
||||
},
|
||||
"setup_hooks": [
|
||||
{"total": "${sum_two(1, 5)}"}
|
||||
],
|
||||
"validate": [
|
||||
{"check": "status_code", "expect": 200}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
tests_mapping = {
|
||||
"project_mapping": {
|
||||
"functions": self.debugtalk_functions
|
||||
},
|
||||
"setup_hooks": [
|
||||
{"total": "${sum_two(1, 5)}"}
|
||||
],
|
||||
"validate": [
|
||||
{"check": "status_code", "expect": 200}
|
||||
]
|
||||
"testcases": testcases
|
||||
}
|
||||
parsed_test = parser.prepare_lazy_data(test, self.debugtalk_functions)
|
||||
test_runner = runner.Runner(config_dict, self.debugtalk_functions)
|
||||
test_runner.run_test(parsed_test)
|
||||
parsed_tests_mapping = parser.parse_tests(tests_mapping)
|
||||
parsed_testcase = parsed_tests_mapping["testcases"][0]
|
||||
test_runner = runner.Runner(parsed_testcase["config"])
|
||||
test_runner.run_test(parsed_testcase["teststeps"][0])
|
||||
test_variables_mapping = test_runner.session_context.test_variables_mapping
|
||||
self.assertEqual(test_variables_mapping["total"], 6)
|
||||
self.assertEqual(test_variables_mapping["request"]["data"], "a=1&b=2")
|
||||
|
||||
def test_run_testcase_with_hooks_modify_request(self):
|
||||
config_dict = {
|
||||
"name": "basic test with httpbin",
|
||||
"base_url": HTTPBIN_SERVER
|
||||
}
|
||||
test = {
|
||||
"name": "modify request headers",
|
||||
"base_url": HTTPBIN_SERVER,
|
||||
"request": {
|
||||
"url": "/anything",
|
||||
"method": "POST",
|
||||
"headers": {
|
||||
"content-type": "application/json",
|
||||
"user_agent": "iOS/10.3"
|
||||
testcases = [
|
||||
{
|
||||
"config": {
|
||||
"name": "basic test with httpbin",
|
||||
"base_url": HTTPBIN_SERVER
|
||||
},
|
||||
"json": {
|
||||
"os_platform": "ios",
|
||||
"sign": "5188962c489d1a35effa99e9346dd5efd4fdabad"
|
||||
}
|
||||
"teststeps": [
|
||||
{
|
||||
"name": "modify request headers",
|
||||
"base_url": HTTPBIN_SERVER,
|
||||
"request": {
|
||||
"url": "/anything",
|
||||
"method": "POST",
|
||||
"headers": {
|
||||
"content-type": "application/json",
|
||||
"user_agent": "iOS/10.3"
|
||||
},
|
||||
"json": {
|
||||
"os_platform": "ios",
|
||||
"sign": "5188962c489d1a35effa99e9346dd5efd4fdabad"
|
||||
}
|
||||
},
|
||||
"setup_hooks": [
|
||||
"${modify_request_json($request, android)}"
|
||||
],
|
||||
"validate": [
|
||||
{"check": "status_code", "expect": 200},
|
||||
{"check": "content.json.os_platform", "expect": "android"}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
tests_mapping = {
|
||||
"project_mapping": {
|
||||
"functions": self.debugtalk_functions
|
||||
},
|
||||
"setup_hooks": [
|
||||
"${modify_request_json($request, android)}"
|
||||
],
|
||||
"validate": [
|
||||
{"check": "status_code", "expect": 200},
|
||||
{"check": "content.json.os_platform", "expect": "android"}
|
||||
]
|
||||
"testcases": testcases
|
||||
}
|
||||
test_runner = runner.Runner(config_dict, self.debugtalk_functions)
|
||||
parsed_test = parser.prepare_lazy_data(test, self.debugtalk_functions, {"request"})
|
||||
test_runner.run_test(parsed_test)
|
||||
parsed_tests_mapping = parser.parse_tests(tests_mapping)
|
||||
parsed_testcase = parsed_tests_mapping["testcases"][0]
|
||||
test_runner = runner.Runner(parsed_testcase["config"])
|
||||
test_runner.run_test(parsed_testcase["teststeps"][0])
|
||||
|
||||
def test_run_testcase_with_teardown_hooks_success(self):
|
||||
test = {
|
||||
"name": "get token",
|
||||
"request": {
|
||||
"url": "http://127.0.0.1:5000/api/get-token",
|
||||
"method": "POST",
|
||||
"headers": {
|
||||
"content-type": "application/json",
|
||||
"user_agent": "iOS/10.3",
|
||||
"device_sn": "HZfFBh6tU59EdXJ",
|
||||
"os_platform": "ios",
|
||||
"app_version": "2.8.6"
|
||||
testcases = [
|
||||
{
|
||||
"config": {
|
||||
"name": "basic test with httpbin"
|
||||
},
|
||||
"json": {
|
||||
"sign": "5188962c489d1a35effa99e9346dd5efd4fdabad"
|
||||
}
|
||||
"teststeps": [
|
||||
{
|
||||
"name": "get token",
|
||||
"request": {
|
||||
"url": "http://127.0.0.1:5000/api/get-token",
|
||||
"method": "POST",
|
||||
"headers": {
|
||||
"content-type": "application/json",
|
||||
"user_agent": "iOS/10.3",
|
||||
"device_sn": "HZfFBh6tU59EdXJ",
|
||||
"os_platform": "ios",
|
||||
"app_version": "2.8.6"
|
||||
},
|
||||
"json": {
|
||||
"sign": "5188962c489d1a35effa99e9346dd5efd4fdabad"
|
||||
}
|
||||
},
|
||||
"validate": [
|
||||
{"check": "status_code", "expect": 200}
|
||||
],
|
||||
"teardown_hooks": ["${teardown_hook_sleep_N_secs($response, 2)}"]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
tests_mapping = {
|
||||
"project_mapping": {
|
||||
"functions": self.debugtalk_functions
|
||||
},
|
||||
"validate": [
|
||||
{"check": "status_code", "expect": 200}
|
||||
],
|
||||
"teardown_hooks": ["${teardown_hook_sleep_N_secs($response, 2)}"]
|
||||
"testcases": testcases
|
||||
}
|
||||
parsed_tests_mapping = parser.parse_tests(tests_mapping)
|
||||
parsed_testcase = parsed_tests_mapping["testcases"][0]
|
||||
test_runner = runner.Runner(parsed_testcase["config"])
|
||||
|
||||
start_time = time.time()
|
||||
self.test_runner.run_test(test)
|
||||
test_runner.run_test(parsed_testcase["teststeps"][0])
|
||||
end_time = time.time()
|
||||
# check if teardown function executed
|
||||
self.assertLess(end_time - start_time, 0.5)
|
||||
|
||||
def test_run_testcase_with_teardown_hooks_fail(self):
|
||||
test = {
|
||||
"name": "get token",
|
||||
"request": {
|
||||
"url": "http://127.0.0.1:5000/api/get-token2",
|
||||
"method": "POST",
|
||||
"headers": {
|
||||
"content-type": "application/json",
|
||||
"user_agent": "iOS/10.3",
|
||||
"device_sn": "HZfFBh6tU59EdXJ",
|
||||
"os_platform": "ios",
|
||||
"app_version": "2.8.6"
|
||||
testcases = [
|
||||
{
|
||||
"config": {
|
||||
"name": "basic test with httpbin"
|
||||
},
|
||||
"json": {
|
||||
"sign": "5188962c489d1a35effa99e9346dd5efd4fdabad"
|
||||
}
|
||||
"teststeps": [
|
||||
{
|
||||
"name": "get token",
|
||||
"request": {
|
||||
"url": "http://127.0.0.1:5000/api/get-token2",
|
||||
"method": "POST",
|
||||
"headers": {
|
||||
"content-type": "application/json",
|
||||
"user_agent": "iOS/10.3",
|
||||
"device_sn": "HZfFBh6tU59EdXJ",
|
||||
"os_platform": "ios",
|
||||
"app_version": "2.8.6"
|
||||
},
|
||||
"json": {
|
||||
"sign": "5188962c489d1a35effa99e9346dd5efd4fdabad"
|
||||
}
|
||||
},
|
||||
"validate": [
|
||||
{"check": "status_code", "expect": 404}
|
||||
],
|
||||
"teardown_hooks": ["${teardown_hook_sleep_N_secs($response, 2)}"]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
tests_mapping = {
|
||||
"project_mapping": {
|
||||
"functions": self.debugtalk_functions
|
||||
},
|
||||
"validate": [
|
||||
{"check": "status_code", "expect": 404}
|
||||
],
|
||||
"teardown_hooks": ["${teardown_hook_sleep_N_secs($response, 2)}"]
|
||||
"testcases": testcases
|
||||
}
|
||||
prepared_test = parser.prepare_lazy_data(test, self.debugtalk_functions, {"response"})
|
||||
parsed_tests_mapping = parser.parse_tests(tests_mapping)
|
||||
parsed_testcase = parsed_tests_mapping["testcases"][0]
|
||||
test_runner = runner.Runner(parsed_testcase["config"])
|
||||
|
||||
start_time = time.time()
|
||||
self.test_runner.run_test(prepared_test)
|
||||
test_runner.run_test(parsed_testcase["teststeps"][0])
|
||||
end_time = time.time()
|
||||
# check if teardown function executed
|
||||
self.assertGreater(end_time - start_time, 2)
|
||||
@@ -251,34 +289,51 @@ class TestRunner(ApiServerUnittest):
|
||||
def test_bugfix_type_match(self):
|
||||
testcase_file_path = os.path.join(
|
||||
os.getcwd(), 'tests/data/bugfix_type_match.yml')
|
||||
testcases = loader.load_file(testcase_file_path)
|
||||
|
||||
test = testcases[1]["test"]
|
||||
self.test_runner.run_test(test)
|
||||
tests_mapping = loader.load_tests(testcase_file_path)
|
||||
parsed_tests_mapping = parser.parse_tests(tests_mapping)
|
||||
parsed_testcase = parsed_tests_mapping["testcases"][0]
|
||||
test_runner = runner.Runner(parsed_testcase["config"])
|
||||
test_runner.run_test(parsed_testcase["teststeps"][0])
|
||||
|
||||
def test_run_validate_elapsed(self):
|
||||
test = {
|
||||
"name": "get token",
|
||||
"request": {
|
||||
"url": "http://127.0.0.1:5000/api/get-token",
|
||||
"method": "POST",
|
||||
"headers": {
|
||||
"content-type": "application/json",
|
||||
"user_agent": "iOS/10.3",
|
||||
"device_sn": "HZfFBh6tU59EdXJ",
|
||||
"os_platform": "ios",
|
||||
"app_version": "2.8.6"
|
||||
},
|
||||
"json": {
|
||||
"sign": "5188962c489d1a35effa99e9346dd5efd4fdabad"
|
||||
}
|
||||
testcases = [
|
||||
{
|
||||
"config": {},
|
||||
"teststeps": [
|
||||
{
|
||||
"name": "get token",
|
||||
"request": {
|
||||
"url": "http://127.0.0.1:5000/api/get-token",
|
||||
"method": "POST",
|
||||
"headers": {
|
||||
"content-type": "application/json",
|
||||
"user_agent": "iOS/10.3",
|
||||
"device_sn": "HZfFBh6tU59EdXJ",
|
||||
"os_platform": "ios",
|
||||
"app_version": "2.8.6"
|
||||
},
|
||||
"json": {
|
||||
"sign": "5188962c489d1a35effa99e9346dd5efd4fdabad"
|
||||
}
|
||||
},
|
||||
"validate": [
|
||||
{"check": "status_code", "expect": 200},
|
||||
{"check": "elapsed.seconds", "comparator": "lt", "expect": 1},
|
||||
{"check": "elapsed.days", "comparator": "eq", "expect": 0},
|
||||
{"check": "elapsed.microseconds", "comparator": "gt", "expect": 1000},
|
||||
{"check": "elapsed.total_seconds", "comparator": "lt", "expect": 1}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
tests_mapping = {
|
||||
"project_mapping": {
|
||||
"functions": self.debugtalk_functions
|
||||
},
|
||||
"validate": [
|
||||
{"check": "status_code", "expect": 200},
|
||||
{"check": "elapsed.seconds", "comparator": "lt", "expect": 1},
|
||||
{"check": "elapsed.days", "comparator": "eq", "expect": 0},
|
||||
{"check": "elapsed.microseconds", "comparator": "gt", "expect": 1000},
|
||||
{"check": "elapsed.total_seconds", "comparator": "lt", "expect": 1}
|
||||
]
|
||||
"testcases": testcases
|
||||
}
|
||||
self.test_runner.run_test(test)
|
||||
parsed_tests_mapping = parser.parse_tests(tests_mapping)
|
||||
parsed_testcase = parsed_tests_mapping["testcases"][0]
|
||||
test_runner = runner.Runner(parsed_testcase["config"])
|
||||
test_runner.run_test(parsed_testcase["teststeps"][0])
|
||||
@@ -61,35 +61,6 @@ class TestUtils(ApiServerUnittest):
|
||||
result = utils.query_json(json_content, query)
|
||||
self.assertEqual(result, "L")
|
||||
|
||||
def test_get_uniform_comparator(self):
|
||||
self.assertEqual(utils.get_uniform_comparator("eq"), "equals")
|
||||
self.assertEqual(utils.get_uniform_comparator("=="), "equals")
|
||||
self.assertEqual(utils.get_uniform_comparator("lt"), "less_than")
|
||||
self.assertEqual(utils.get_uniform_comparator("le"), "less_than_or_equals")
|
||||
self.assertEqual(utils.get_uniform_comparator("gt"), "greater_than")
|
||||
self.assertEqual(utils.get_uniform_comparator("ge"), "greater_than_or_equals")
|
||||
self.assertEqual(utils.get_uniform_comparator("ne"), "not_equals")
|
||||
|
||||
self.assertEqual(utils.get_uniform_comparator("str_eq"), "string_equals")
|
||||
self.assertEqual(utils.get_uniform_comparator("len_eq"), "length_equals")
|
||||
self.assertEqual(utils.get_uniform_comparator("count_eq"), "length_equals")
|
||||
|
||||
self.assertEqual(utils.get_uniform_comparator("len_gt"), "length_greater_than")
|
||||
self.assertEqual(utils.get_uniform_comparator("count_gt"), "length_greater_than")
|
||||
self.assertEqual(utils.get_uniform_comparator("count_greater_than"), "length_greater_than")
|
||||
|
||||
self.assertEqual(utils.get_uniform_comparator("len_ge"), "length_greater_than_or_equals")
|
||||
self.assertEqual(utils.get_uniform_comparator("count_ge"), "length_greater_than_or_equals")
|
||||
self.assertEqual(utils.get_uniform_comparator("count_greater_than_or_equals"), "length_greater_than_or_equals")
|
||||
|
||||
self.assertEqual(utils.get_uniform_comparator("len_lt"), "length_less_than")
|
||||
self.assertEqual(utils.get_uniform_comparator("count_lt"), "length_less_than")
|
||||
self.assertEqual(utils.get_uniform_comparator("count_less_than"), "length_less_than")
|
||||
|
||||
self.assertEqual(utils.get_uniform_comparator("len_le"), "length_less_than_or_equals")
|
||||
self.assertEqual(utils.get_uniform_comparator("count_le"), "length_less_than_or_equals")
|
||||
self.assertEqual(utils.get_uniform_comparator("count_less_than_or_equals"), "length_less_than_or_equals")
|
||||
|
||||
def current_validators(self):
|
||||
from httprunner import built_in
|
||||
functions_mapping = loader.load_module_functions(built_in)
|
||||
@@ -205,61 +176,6 @@ class TestUtils(ApiServerUnittest):
|
||||
self.assertIsInstance(ordered_dict, dict)
|
||||
self.assertIn("a", ordered_dict)
|
||||
|
||||
def test_extend_validators(self):
|
||||
def_validators = [
|
||||
{'eq': ['v1', 200]},
|
||||
{"check": "s2", "expect": 16, "comparator": "len_eq"}
|
||||
]
|
||||
current_validators = [
|
||||
{"check": "v1", "expect": 201},
|
||||
{'len_eq': ['s3', 12]}
|
||||
]
|
||||
def_validators = [
|
||||
parser.parse_validator(validator)
|
||||
for validator in def_validators
|
||||
]
|
||||
ref_validators = [
|
||||
parser.parse_validator(validator)
|
||||
for validator in current_validators
|
||||
]
|
||||
|
||||
extended_validators = utils.extend_validators(def_validators, ref_validators)
|
||||
self.assertIn(
|
||||
{"check": "v1", "expect": 201, "comparator": "eq"},
|
||||
extended_validators
|
||||
)
|
||||
self.assertIn(
|
||||
{"check": "s2", "expect": 16, "comparator": "len_eq"},
|
||||
extended_validators
|
||||
)
|
||||
self.assertIn(
|
||||
{"check": "s3", "expect": 12, "comparator": "len_eq"},
|
||||
extended_validators
|
||||
)
|
||||
|
||||
def test_extend_validators_with_dict(self):
|
||||
def_validators = [
|
||||
{'eq': ["a", {"v": 1}]},
|
||||
{'eq': [{"b": 1}, 200]}
|
||||
]
|
||||
current_validators = [
|
||||
{'len_eq': ['s3', 12]},
|
||||
{'eq': [{"b": 1}, 201]}
|
||||
]
|
||||
def_validators = [
|
||||
parser.parse_validator(validator)
|
||||
for validator in def_validators
|
||||
]
|
||||
ref_validators = [
|
||||
parser.parse_validator(validator)
|
||||
for validator in current_validators
|
||||
]
|
||||
|
||||
extended_validators = utils.extend_validators(def_validators, ref_validators)
|
||||
self.assertEqual(len(extended_validators), 3)
|
||||
self.assertIn({'check': {'b': 1}, 'expect': 201, 'comparator': 'eq'}, extended_validators)
|
||||
self.assertNotIn({'check': {'b': 1}, 'expect': 200, 'comparator': 'eq'}, extended_validators)
|
||||
|
||||
def test_extend_variables(self):
|
||||
raw_variables = [{"var1": "val1"}, {"var2": "val2"}]
|
||||
override_variables = [{"var1": "val111"}, {"var3": "val3"}]
|
||||
|
||||
@@ -74,3 +74,101 @@ class TestValidator(unittest.TestCase):
|
||||
func = lambda x: x + 1
|
||||
self.assertTrue(validator.is_function(func))
|
||||
self.assertTrue(validator.is_function(validator.is_testcase))
|
||||
|
||||
def test_get_uniform_comparator(self):
|
||||
self.assertEqual(validator.get_uniform_comparator("eq"), "equals")
|
||||
self.assertEqual(validator.get_uniform_comparator("=="), "equals")
|
||||
self.assertEqual(validator.get_uniform_comparator("lt"), "less_than")
|
||||
self.assertEqual(validator.get_uniform_comparator("le"), "less_than_or_equals")
|
||||
self.assertEqual(validator.get_uniform_comparator("gt"), "greater_than")
|
||||
self.assertEqual(validator.get_uniform_comparator("ge"), "greater_than_or_equals")
|
||||
self.assertEqual(validator.get_uniform_comparator("ne"), "not_equals")
|
||||
|
||||
self.assertEqual(validator.get_uniform_comparator("str_eq"), "string_equals")
|
||||
self.assertEqual(validator.get_uniform_comparator("len_eq"), "length_equals")
|
||||
self.assertEqual(validator.get_uniform_comparator("count_eq"), "length_equals")
|
||||
|
||||
self.assertEqual(validator.get_uniform_comparator("len_gt"), "length_greater_than")
|
||||
self.assertEqual(validator.get_uniform_comparator("count_gt"), "length_greater_than")
|
||||
self.assertEqual(validator.get_uniform_comparator("count_greater_than"), "length_greater_than")
|
||||
|
||||
self.assertEqual(validator.get_uniform_comparator("len_ge"), "length_greater_than_or_equals")
|
||||
self.assertEqual(validator.get_uniform_comparator("count_ge"), "length_greater_than_or_equals")
|
||||
self.assertEqual(validator.get_uniform_comparator("count_greater_than_or_equals"), "length_greater_than_or_equals")
|
||||
|
||||
self.assertEqual(validator.get_uniform_comparator("len_lt"), "length_less_than")
|
||||
self.assertEqual(validator.get_uniform_comparator("count_lt"), "length_less_than")
|
||||
self.assertEqual(validator.get_uniform_comparator("count_less_than"), "length_less_than")
|
||||
|
||||
self.assertEqual(validator.get_uniform_comparator("len_le"), "length_less_than_or_equals")
|
||||
self.assertEqual(validator.get_uniform_comparator("count_le"), "length_less_than_or_equals")
|
||||
self.assertEqual(validator.get_uniform_comparator("count_less_than_or_equals"), "length_less_than_or_equals")
|
||||
|
||||
def test_parse_validator(self):
|
||||
_validator = {"check": "status_code", "comparator": "eq", "expect": 201}
|
||||
self.assertEqual(
|
||||
validator.uniform_validator(_validator),
|
||||
{"check": "status_code", "comparator": "equals", "expect": 201}
|
||||
)
|
||||
|
||||
_validator = {'eq': ['status_code', 201]}
|
||||
self.assertEqual(
|
||||
validator.uniform_validator(_validator),
|
||||
{"check": "status_code", "comparator": "equals", "expect": 201}
|
||||
)
|
||||
|
||||
|
||||
def test_extend_validators(self):
|
||||
def_validators = [
|
||||
{'eq': ['v1', 200]},
|
||||
{"check": "s2", "expect": 16, "comparator": "len_eq"}
|
||||
]
|
||||
current_validators = [
|
||||
{"check": "v1", "expect": 201},
|
||||
{'len_eq': ['s3', 12]}
|
||||
]
|
||||
def_validators = [
|
||||
validator.uniform_validator(_validator)
|
||||
for _validator in def_validators
|
||||
]
|
||||
ref_validators = [
|
||||
validator.uniform_validator(_validator)
|
||||
for _validator in current_validators
|
||||
]
|
||||
|
||||
extended_validators = validator.extend_validators(def_validators, ref_validators)
|
||||
self.assertIn(
|
||||
{"check": "v1", "expect": 201, "comparator": "equals"},
|
||||
extended_validators
|
||||
)
|
||||
self.assertIn(
|
||||
{"check": "s2", "expect": 16, "comparator": "length_equals"},
|
||||
extended_validators
|
||||
)
|
||||
self.assertIn(
|
||||
{"check": "s3", "expect": 12, "comparator": "length_equals"},
|
||||
extended_validators
|
||||
)
|
||||
|
||||
def test_extend_validators_with_dict(self):
|
||||
def_validators = [
|
||||
{'eq': ["a", {"v": 1}]},
|
||||
{'eq': [{"b": 1}, 200]}
|
||||
]
|
||||
current_validators = [
|
||||
{'len_eq': ['s3', 12]},
|
||||
{'eq': [{"b": 1}, 201]}
|
||||
]
|
||||
def_validators = [
|
||||
validator.uniform_validator(_validator)
|
||||
for _validator in def_validators
|
||||
]
|
||||
ref_validators = [
|
||||
validator.uniform_validator(_validator)
|
||||
for _validator in current_validators
|
||||
]
|
||||
|
||||
extended_validators = validator.extend_validators(def_validators, ref_validators)
|
||||
self.assertEqual(len(extended_validators), 3)
|
||||
self.assertIn({'check': {'b': 1}, 'expect': 201, 'comparator': 'equals'}, extended_validators)
|
||||
self.assertNotIn({'check': {'b': 1}, 'expect': 200, 'comparator': 'equals'}, extended_validators)
|
||||
|
||||
Reference in New Issue
Block a user