mirror of
https://github.com/httprunner/httprunner.git
synced 2026-07-06 23:11:21 +08:00
**Added**
- feat: implement global hooks `setup_testcase/teardown_testcase`, called before/after each testcase - feat: implement global hooks `setup_teststep/teardown_teststep`, called before/after each teststep **Changed** - remove default header `HRUN-Request-ID`, implement the same function with global hooks
This commit is contained in:
@@ -1,6 +1,11 @@
|
|||||||
# Release History
|
# Release History
|
||||||
|
|
||||||
## 3.0.2 (2020-04-12)
|
## 3.0.2 (2020-04-17)
|
||||||
|
|
||||||
|
**Added**
|
||||||
|
|
||||||
|
- feat: implement global hooks `setup_testcase/teardown_testcase`, called before/after each testcase
|
||||||
|
- feat: implement global hooks `setup_teststep/teardown_teststep`, called before/after each teststep
|
||||||
|
|
||||||
**Changed**
|
**Changed**
|
||||||
|
|
||||||
@@ -9,6 +14,7 @@
|
|||||||
- make `startproject` as hrun sub-command, usage: `hrun startproject <project_name>`
|
- make `startproject` as hrun sub-command, usage: `hrun startproject <project_name>`
|
||||||
- make `har2case` as hrun sub-command, usage: `hrun har2case -h`
|
- make `har2case` as hrun sub-command, usage: `hrun har2case -h`
|
||||||
- make `locusts` as hrun sub-command, usage: `hrun locusts -h`
|
- make `locusts` as hrun sub-command, usage: `hrun locusts -h`
|
||||||
|
- remove default header `HRUN-Request-ID`, implement the same function with global hooks
|
||||||
|
|
||||||
## 3.0.1 (2020-03-24)
|
## 3.0.1 (2020-03-24)
|
||||||
|
|
||||||
|
|||||||
@@ -200,7 +200,6 @@ class HttpRunner(object):
|
|||||||
)
|
)
|
||||||
testcase_summary["log"] = logs_file_abs_path
|
testcase_summary["log"] = logs_file_abs_path
|
||||||
|
|
||||||
testcase_summary["HRUN-Request-ID"] = testcase.runner.hrun_request_id
|
|
||||||
summary["details"].append(testcase_summary)
|
summary["details"].append(testcase_summary)
|
||||||
|
|
||||||
return summary
|
return summary
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
import copy
|
||||||
|
|
||||||
from httprunner import parser, utils
|
from httprunner import parser, utils
|
||||||
|
|
||||||
|
|
||||||
@@ -34,7 +36,7 @@ class SessionContext(object):
|
|||||||
}
|
}
|
||||||
|
|
||||||
"""
|
"""
|
||||||
variables_mapping = variables_mapping or {}
|
variables_mapping = copy.deepcopy(variables_mapping or {})
|
||||||
variables_mapping = utils.ensure_mapping_format(variables_mapping)
|
variables_mapping = utils.ensure_mapping_format(variables_mapping)
|
||||||
variables_mapping.update(self.session_variables_mapping)
|
variables_mapping.update(self.session_variables_mapping)
|
||||||
parsed_variables_mapping = parser.parse_variables_mapping(variables_mapping)
|
parsed_variables_mapping = parser.parse_variables_mapping(variables_mapping)
|
||||||
|
|||||||
@@ -1076,8 +1076,17 @@ def __prepare_config(config, project_mapping, session_variables_set=None):
|
|||||||
if raw_config_variables_mapping:
|
if raw_config_variables_mapping:
|
||||||
config["variables"] = raw_config_variables_mapping
|
config["variables"] = raw_config_variables_mapping
|
||||||
|
|
||||||
|
if "setup_testcase" in functions:
|
||||||
|
config.setdefault("setup_hooks", [])
|
||||||
|
config["setup_hooks"].insert(0, "${setup_testcase($variables)}")
|
||||||
|
|
||||||
|
if "teardown_testcase" in functions:
|
||||||
|
config.setdefault("teardown_hooks", [])
|
||||||
|
config["teardown_hooks"].append("${teardown_testcase()}")
|
||||||
|
|
||||||
check_variables_set = set(raw_config_variables_mapping.keys())
|
check_variables_set = set(raw_config_variables_mapping.keys())
|
||||||
check_variables_set |= (session_variables_set or set())
|
check_variables_set |= (session_variables_set or set())
|
||||||
|
check_variables_set.add("variables")
|
||||||
prepared_config = prepare_lazy_data(config, functions, check_variables_set, cached=True)
|
prepared_config = prepare_lazy_data(config, functions, check_variables_set, cached=True)
|
||||||
return prepared_config
|
return prepared_config
|
||||||
|
|
||||||
@@ -1110,7 +1119,7 @@ def __prepare_testcase_tests(tests, config, project_mapping, session_variables_s
|
|||||||
session_variables_set = set(config_variables.keys()) | (session_variables_set or set())
|
session_variables_set = set(config_variables.keys()) | (session_variables_set or set())
|
||||||
for test_dict in tests:
|
for test_dict in tests:
|
||||||
|
|
||||||
teststep_variables_set = {"request", "response"}
|
teststep_variables_set = {"request", "response", "variables"}
|
||||||
|
|
||||||
# 1, testcase config => testcase tests
|
# 1, testcase config => testcase tests
|
||||||
# override test_dict variables
|
# override test_dict variables
|
||||||
@@ -1120,6 +1129,14 @@ def __prepare_testcase_tests(tests, config, project_mapping, session_variables_s
|
|||||||
)
|
)
|
||||||
test_dict["variables"] = test_dict_variables
|
test_dict["variables"] = test_dict_variables
|
||||||
|
|
||||||
|
if "setup_teststep" in functions:
|
||||||
|
test_dict.setdefault("setup_hooks", [])
|
||||||
|
test_dict["setup_hooks"].insert(0, "${setup_teststep($request, $variables)}")
|
||||||
|
|
||||||
|
if "teardown_teststep" in functions:
|
||||||
|
test_dict.setdefault("teardown_hooks", [])
|
||||||
|
test_dict["teardown_hooks"].append("${teardown_teststep($response)}")
|
||||||
|
|
||||||
# base_url & verify: priority test_dict > config
|
# base_url & verify: priority test_dict > config
|
||||||
if (not test_dict.get("base_url")) and config_base_url:
|
if (not test_dict.get("base_url")) and config_base_url:
|
||||||
test_dict["base_url"] = config_base_url
|
test_dict["base_url"] = config_base_url
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
import uuid
|
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
from unittest.case import SkipTest
|
from unittest.case import SkipTest
|
||||||
|
|
||||||
@@ -72,12 +71,6 @@ class Runner(object):
|
|||||||
self.export = config.get("export") or config.get("output", [])
|
self.export = config.get("export") or config.get("output", [])
|
||||||
config_variables = config.get("variables", {})
|
config_variables = config.get("variables", {})
|
||||||
|
|
||||||
self.hrun_request_id = str(uuid.uuid4())
|
|
||||||
if "HRUN-Request-ID" not in config_variables:
|
|
||||||
config_variables["HRUN-Request-ID"] = self.hrun_request_id
|
|
||||||
else:
|
|
||||||
self.hrun_request_id = config_variables["HRUN-Request-ID"]
|
|
||||||
|
|
||||||
# testcase setup hooks
|
# testcase setup hooks
|
||||||
testcase_setup_hooks = config.get("setup_hooks", [])
|
testcase_setup_hooks = config.get("setup_hooks", [])
|
||||||
# testcase teardown hooks
|
# testcase teardown hooks
|
||||||
@@ -86,6 +79,10 @@ class Runner(object):
|
|||||||
self.http_client_session = http_client_session or HttpSession()
|
self.http_client_session = http_client_session or HttpSession()
|
||||||
self.session_context = SessionContext(config_variables)
|
self.session_context = SessionContext(config_variables)
|
||||||
|
|
||||||
|
self.session_context.update_session_variables({
|
||||||
|
"variables": config_variables
|
||||||
|
})
|
||||||
|
|
||||||
if testcase_setup_hooks:
|
if testcase_setup_hooks:
|
||||||
self.do_hook_actions(testcase_setup_hooks, HookTypeEnum.SETUP)
|
self.do_hook_actions(testcase_setup_hooks, HookTypeEnum.SETUP)
|
||||||
|
|
||||||
@@ -217,6 +214,9 @@ class Runner(object):
|
|||||||
parsed_test_request = self.session_context.eval_content(raw_request)
|
parsed_test_request = self.session_context.eval_content(raw_request)
|
||||||
self.session_context.update_test_variables("request", parsed_test_request)
|
self.session_context.update_test_variables("request", parsed_test_request)
|
||||||
|
|
||||||
|
test_variables.update(self.session_context.session_variables_mapping["variables"])
|
||||||
|
self.session_context.update_test_variables("variables", test_variables)
|
||||||
|
|
||||||
# setup hooks
|
# setup hooks
|
||||||
setup_hooks = test_dict.get("setup_hooks", [])
|
setup_hooks = test_dict.get("setup_hooks", [])
|
||||||
if setup_hooks:
|
if setup_hooks:
|
||||||
@@ -227,11 +227,6 @@ class Runner(object):
|
|||||||
base_url = self.session_context.eval_content(test_dict.get("base_url", ""))
|
base_url = self.session_context.eval_content(test_dict.get("base_url", ""))
|
||||||
parsed_url = utils.build_url(base_url, url)
|
parsed_url = utils.build_url(base_url, url)
|
||||||
|
|
||||||
request_headers = parsed_test_request.setdefault("headers", {})
|
|
||||||
if "HRUN-Request-ID" not in request_headers:
|
|
||||||
parsed_test_request["headers"]["HRUN-Request-ID"] = \
|
|
||||||
self.session_context.session_variables_mapping["HRUN-Request-ID"]
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
method = parsed_test_request.pop('method')
|
method = parsed_test_request.pop('method')
|
||||||
parsed_test_request.setdefault("verify", self.verify)
|
parsed_test_request.setdefault("verify", self.verify)
|
||||||
|
|||||||
@@ -2,6 +2,9 @@ import os
|
|||||||
import random
|
import random
|
||||||
import string
|
import string
|
||||||
import time
|
import time
|
||||||
|
import uuid
|
||||||
|
|
||||||
|
from loguru import logger
|
||||||
|
|
||||||
from tests.api_server import HTTPBIN_SERVER, gen_md5, get_sign
|
from tests.api_server import HTTPBIN_SERVER, gen_md5, get_sign
|
||||||
|
|
||||||
@@ -25,6 +28,26 @@ def get_default_request():
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def setup_testcase(variables):
|
||||||
|
logger.info(f"setup_testcase, variables: {variables}")
|
||||||
|
variables["request_id_prefix"] = str(int(time.time()))
|
||||||
|
|
||||||
|
|
||||||
|
def teardown_testcase():
|
||||||
|
logger.info(f"teardown_testcase.")
|
||||||
|
|
||||||
|
|
||||||
|
def setup_teststep(request, variables):
|
||||||
|
logger.info(f"setup_teststep, request: {request}, variables: {variables}")
|
||||||
|
request.setdefault("headers", {})
|
||||||
|
request_id_prefix = variables["request_id_prefix"]
|
||||||
|
request["headers"]["HRUN-Request-ID"] = request_id_prefix + "-" + str(uuid.uuid4())
|
||||||
|
|
||||||
|
|
||||||
|
def teardown_teststep(response):
|
||||||
|
logger.info(f"teardown_teststep, response status code: {response.status_code}")
|
||||||
|
|
||||||
|
|
||||||
def sum_two(m, n):
|
def sum_two(m, n):
|
||||||
return m + n
|
return m + n
|
||||||
|
|
||||||
|
|||||||
@@ -9,6 +9,8 @@ config:
|
|||||||
teststeps:
|
teststeps:
|
||||||
-
|
-
|
||||||
name: headers
|
name: headers
|
||||||
|
variables:
|
||||||
|
a: 123
|
||||||
request:
|
request:
|
||||||
url: /headers
|
url: /headers
|
||||||
method: GET
|
method: GET
|
||||||
|
|||||||
Reference in New Issue
Block a user