mirror of
https://github.com/httprunner/httprunner.git
synced 2026-05-18 13:27:37 +08:00
update: httprunner make v4
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
"""
|
||||
This module handles compatibility issues between testcase format v2 and v3.
|
||||
This module handles compatibility issues between testcase format v2, v3 and v4.
|
||||
"""
|
||||
import os
|
||||
import sys
|
||||
@@ -14,9 +14,8 @@ from httprunner.utils import sort_dict_by_custom_order
|
||||
|
||||
|
||||
def convert_variables(
|
||||
raw_variables: Union[Dict, Text], test_path: Text
|
||||
raw_variables: Union[Dict, Text], test_path: Text
|
||||
) -> Dict[Text, Any]:
|
||||
|
||||
if isinstance(raw_variables, Dict):
|
||||
return raw_variables
|
||||
|
||||
@@ -33,6 +32,18 @@ def convert_variables(
|
||||
)
|
||||
|
||||
|
||||
def _convert_request(request: Dict) -> Dict:
|
||||
if "body" in request:
|
||||
content_type = ""
|
||||
if "headers" in request and "Content-Type" in request["headers"]:
|
||||
content_type = request["headers"]["Content-Type"]
|
||||
if content_type.startswith("application/json"):
|
||||
request["json"] = request.pop("body")
|
||||
else:
|
||||
request["data"] = request.pop("body")
|
||||
return _sort_request_by_custom_order(request)
|
||||
|
||||
|
||||
def _convert_jmespath(raw: Text) -> Text:
|
||||
if not isinstance(raw, Text):
|
||||
raise exceptions.TestCaseFormatError(f"Invalid jmespath extractor: {raw}")
|
||||
@@ -153,6 +164,9 @@ def _ensure_step_attachment(step: Dict) -> Dict:
|
||||
"name": step["name"],
|
||||
}
|
||||
|
||||
if "request" in step:
|
||||
test_dict["request"] = _convert_request(step["request"])
|
||||
|
||||
if "variables" in step:
|
||||
test_dict["variables"] = step["variables"]
|
||||
|
||||
@@ -181,11 +195,11 @@ def _ensure_step_attachment(step: Dict) -> Dict:
|
||||
return test_dict
|
||||
|
||||
|
||||
def ensure_testcase_v3_api(api_content: Dict) -> Dict:
|
||||
logger.info("convert api in v2 to testcase format v3")
|
||||
def ensure_testcase_v4_api(api_content: Dict) -> Dict:
|
||||
logger.info("convert api in v2/v3 to testcase format v4")
|
||||
|
||||
teststep = {
|
||||
"request": _sort_request_by_custom_order(api_content["request"]),
|
||||
"request": _convert_request(api_content["request"]),
|
||||
}
|
||||
teststep.update(_ensure_step_attachment(api_content))
|
||||
|
||||
@@ -202,8 +216,8 @@ def ensure_testcase_v3_api(api_content: Dict) -> Dict:
|
||||
}
|
||||
|
||||
|
||||
def ensure_testcase_v3(test_content: Dict) -> Dict:
|
||||
logger.info("ensure compatibility with testcase format v2")
|
||||
def ensure_testcase_v4(test_content: Dict) -> Dict:
|
||||
logger.info("ensure compatibility with testcase format v2/v3")
|
||||
|
||||
v3_content = {"config": test_content["config"], "teststeps": []}
|
||||
|
||||
@@ -221,7 +235,7 @@ def ensure_testcase_v3(test_content: Dict) -> Dict:
|
||||
teststep = {}
|
||||
|
||||
if "request" in step:
|
||||
teststep["request"] = _sort_request_by_custom_order(step.pop("request"))
|
||||
pass
|
||||
elif "api" in step:
|
||||
teststep["testcase"] = step.pop("api")
|
||||
elif "testcase" in step:
|
||||
|
||||
@@ -26,6 +26,53 @@ class TestCompat(unittest.TestCase):
|
||||
with self.assertRaises(exceptions.TestCaseFormatError):
|
||||
compat.convert_variables(None, "examples/data/a-b.c/1.yml")
|
||||
|
||||
def test_convert_request(self):
|
||||
request_with_json_body = {
|
||||
"method": "POST",
|
||||
"url": "https://postman-echo.com/post",
|
||||
"headers": {
|
||||
"Content-Type": "application/json"
|
||||
},
|
||||
"body": {
|
||||
"k1": "v1",
|
||||
"k2": "v2"
|
||||
}
|
||||
}
|
||||
self.assertEqual(
|
||||
compat._convert_request(request_with_json_body),
|
||||
{
|
||||
"method": "POST",
|
||||
"url": "https://postman-echo.com/post",
|
||||
"headers": {
|
||||
"Content-Type": "application/json"
|
||||
},
|
||||
"json": {
|
||||
"k1": "v1",
|
||||
"k2": "v2"
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
request_with_text_body = {
|
||||
"method": "POST",
|
||||
"url": "https://postman-echo.com/post",
|
||||
"headers": {
|
||||
"Content-Type": "text/plain"
|
||||
},
|
||||
"body": "have a nice day"
|
||||
}
|
||||
self.assertEqual(
|
||||
compat._convert_request(request_with_text_body),
|
||||
{
|
||||
"method": "POST",
|
||||
"url": "https://postman-echo.com/post",
|
||||
"headers": {
|
||||
"Content-Type": "text/plain"
|
||||
},
|
||||
"data": "have a nice day"
|
||||
}
|
||||
)
|
||||
|
||||
def test_convert_jmespath(self):
|
||||
self.assertEqual(compat._convert_jmespath("content.abc"), "body.abc")
|
||||
self.assertEqual(compat._convert_jmespath("json.abc"), "body.abc")
|
||||
@@ -85,7 +132,7 @@ class TestCompat(unittest.TestCase):
|
||||
[{"eq": ["body[0].name", 201]}],
|
||||
)
|
||||
|
||||
def test_ensure_testcase_v3_api(self):
|
||||
def test_ensure_testcase_v4_api(self):
|
||||
api_content = {
|
||||
"name": "get with params",
|
||||
"request": {
|
||||
@@ -98,7 +145,7 @@ class TestCompat(unittest.TestCase):
|
||||
"validate": [{"eq": ["content.varB", 200]}, {"lt": ["json.0.varC", 0]}],
|
||||
}
|
||||
self.assertEqual(
|
||||
compat.ensure_testcase_v3_api(api_content),
|
||||
compat.ensure_testcase_v4_api(api_content),
|
||||
{
|
||||
"config": {
|
||||
"name": "get with params",
|
||||
@@ -126,7 +173,7 @@ class TestCompat(unittest.TestCase):
|
||||
},
|
||||
)
|
||||
|
||||
def test_ensure_testcase_v3(self):
|
||||
def test_ensure_testcase_v4(self):
|
||||
testcase_content = {
|
||||
"config": {"name": "xxx", "base_url": "https://httpbin.org"},
|
||||
"teststeps": [
|
||||
@@ -150,7 +197,7 @@ class TestCompat(unittest.TestCase):
|
||||
],
|
||||
}
|
||||
self.assertEqual(
|
||||
compat.ensure_testcase_v3(testcase_content),
|
||||
compat.ensure_testcase_v4(testcase_content),
|
||||
{
|
||||
"config": {"name": "xxx", "base_url": "https://httpbin.org"},
|
||||
"teststeps": [
|
||||
|
||||
@@ -11,8 +11,8 @@ from httprunner import __version__, exceptions
|
||||
from httprunner.compat import (
|
||||
convert_variables,
|
||||
ensure_path_sep,
|
||||
ensure_testcase_v3,
|
||||
ensure_testcase_v3_api,
|
||||
ensure_testcase_v4,
|
||||
ensure_testcase_v4_api,
|
||||
)
|
||||
from httprunner.loader import (
|
||||
convert_relative_project_root_dir,
|
||||
@@ -332,8 +332,8 @@ def make_teststep_chain_style(teststep: Dict) -> Text:
|
||||
|
||||
def make_testcase(testcase: Dict, dir_path: Text = None) -> Text:
|
||||
"""convert valid testcase dict to pytest file path"""
|
||||
# ensure compatibility with testcase format v2
|
||||
testcase = ensure_testcase_v3(testcase)
|
||||
# ensure compatibility with testcase format v2/v3
|
||||
testcase = ensure_testcase_v4(testcase)
|
||||
|
||||
# validate testcase format
|
||||
load_testcase(testcase)
|
||||
@@ -373,9 +373,9 @@ def make_testcase(testcase: Dict, dir_path: Text = None) -> Text:
|
||||
if not isinstance(test_content, Dict):
|
||||
raise exceptions.TestCaseFormatError(f"Invalid teststep: {teststep}")
|
||||
|
||||
# api in v2 format, convert to v3 testcase
|
||||
# api in v2/v3 format, convert to v4 testcase
|
||||
if "request" in test_content and "name" in test_content:
|
||||
test_content = ensure_testcase_v3_api(test_content)
|
||||
test_content = ensure_testcase_v4_api(test_content)
|
||||
|
||||
test_content.setdefault("config", {})["path"] = ref_testcase_path
|
||||
ref_testcase_python_abs_path = make_testcase(test_content)
|
||||
@@ -473,9 +473,9 @@ def __make(tests_path: Text):
|
||||
)
|
||||
continue
|
||||
|
||||
# api in v2 format, convert to v3 testcase
|
||||
# api in v2/v3 format, convert to v4 testcase
|
||||
if "request" in test_content and "name" in test_content:
|
||||
test_content = ensure_testcase_v3_api(test_content)
|
||||
test_content = ensure_testcase_v4_api(test_content)
|
||||
|
||||
if "config" not in test_content:
|
||||
logger.warning(
|
||||
|
||||
Reference in New Issue
Block a user