mirror of
https://github.com/httprunner/httprunner.git
synced 2026-09-05 23:51:25 +08:00
feat: sort request and teststep by custom order
This commit is contained in:
+30
-4
@@ -5,6 +5,7 @@ This module handles compatibility issues between testcase format v2 and v3.
|
|||||||
from typing import List, Dict, Text, Union
|
from typing import List, Dict, Text, Union
|
||||||
|
|
||||||
from httprunner import exceptions
|
from httprunner import exceptions
|
||||||
|
from httprunner.utils import sort_dict_by_custom_order
|
||||||
|
|
||||||
|
|
||||||
def convert_jmespath(raw: Text) -> Text:
|
def convert_jmespath(raw: Text) -> Text:
|
||||||
@@ -57,7 +58,6 @@ def convert_extractors(extractors: Union[List, Dict]) -> Dict:
|
|||||||
|
|
||||||
|
|
||||||
def convert_validators(validators: List) -> List:
|
def convert_validators(validators: List) -> List:
|
||||||
|
|
||||||
for v in validators:
|
for v in validators:
|
||||||
if "check" in v and "expect" in v:
|
if "check" in v and "expect" in v:
|
||||||
# format1: {"check": "content.abc", "assert": "eq", "expect": 201}
|
# format1: {"check": "content.abc", "assert": "eq", "expect": 201}
|
||||||
@@ -71,8 +71,33 @@ def convert_validators(validators: List) -> List:
|
|||||||
return validators
|
return validators
|
||||||
|
|
||||||
|
|
||||||
def ensure_step_attachment(step: Dict) -> Dict:
|
def sort_request_by_custom_order(request: Dict) -> Dict:
|
||||||
|
custom_order = [
|
||||||
|
"method",
|
||||||
|
"url",
|
||||||
|
"params",
|
||||||
|
"headers",
|
||||||
|
"cookies",
|
||||||
|
"data",
|
||||||
|
"json",
|
||||||
|
"files",
|
||||||
|
"timeout",
|
||||||
|
"allow_redirects",
|
||||||
|
"proxies",
|
||||||
|
"verify",
|
||||||
|
"stream",
|
||||||
|
"auth",
|
||||||
|
"cert",
|
||||||
|
]
|
||||||
|
return sort_dict_by_custom_order(request, custom_order)
|
||||||
|
|
||||||
|
|
||||||
|
def sort_step_by_custom_order(step: Dict) -> Dict:
|
||||||
|
custom_order = ["name", "variables", "request", "testcase", "extract", "validate"]
|
||||||
|
return sort_dict_by_custom_order(step, custom_order)
|
||||||
|
|
||||||
|
|
||||||
|
def ensure_step_attachment(step: Dict) -> Dict:
|
||||||
test_dict = {
|
test_dict = {
|
||||||
"name": step["name"],
|
"name": step["name"],
|
||||||
}
|
}
|
||||||
@@ -90,12 +115,13 @@ def ensure_step_attachment(step: Dict) -> Dict:
|
|||||||
|
|
||||||
|
|
||||||
def ensure_testcase_v3_api(api_content: Dict) -> Dict:
|
def ensure_testcase_v3_api(api_content: Dict) -> Dict:
|
||||||
|
|
||||||
teststep = {
|
teststep = {
|
||||||
"request": api_content["request"],
|
"request": api_content["request"],
|
||||||
}
|
}
|
||||||
teststep.update(ensure_step_attachment(api_content))
|
teststep.update(ensure_step_attachment(api_content))
|
||||||
|
|
||||||
|
teststep = sort_step_by_custom_order(teststep)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"config": {"name": api_content["name"]},
|
"config": {"name": api_content["name"]},
|
||||||
"teststeps": [teststep],
|
"teststeps": [teststep],
|
||||||
@@ -103,7 +129,6 @@ def ensure_testcase_v3_api(api_content: Dict) -> Dict:
|
|||||||
|
|
||||||
|
|
||||||
def ensure_testcase_v3(test_content: Dict) -> Dict:
|
def ensure_testcase_v3(test_content: Dict) -> Dict:
|
||||||
|
|
||||||
v3_content = {"config": test_content["config"], "teststeps": []}
|
v3_content = {"config": test_content["config"], "teststeps": []}
|
||||||
|
|
||||||
for step in test_content["teststeps"]:
|
for step in test_content["teststeps"]:
|
||||||
@@ -117,6 +142,7 @@ def ensure_testcase_v3(test_content: Dict) -> Dict:
|
|||||||
teststep["testcase"] = step.pop("testcase")
|
teststep["testcase"] = step.pop("testcase")
|
||||||
|
|
||||||
teststep.update(ensure_step_attachment(step))
|
teststep.update(ensure_step_attachment(step))
|
||||||
|
teststep = sort_step_by_custom_order(teststep)
|
||||||
v3_content["teststeps"].append(teststep)
|
v3_content["teststeps"].append(teststep)
|
||||||
|
|
||||||
return v3_content
|
return v3_content
|
||||||
|
|||||||
@@ -2,10 +2,10 @@ import os
|
|||||||
|
|
||||||
from httprunner.ext.har2case.core import HarParser
|
from httprunner.ext.har2case.core import HarParser
|
||||||
from httprunner.ext.har2case.utils import load_har_log_entries
|
from httprunner.ext.har2case.utils import load_har_log_entries
|
||||||
from httprunner.ext.har2case.utils_test import TestUtils
|
from httprunner.ext.har2case.utils_test import TestHar2CaseUtils
|
||||||
|
|
||||||
|
|
||||||
class TestHar(TestUtils):
|
class TestHar(TestHar2CaseUtils):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.har_path = os.path.join(os.path.dirname(__file__), "data", "demo.har")
|
self.har_path = os.path.join(os.path.dirname(__file__), "data", "demo.har")
|
||||||
self.har_parser = HarParser(self.har_path)
|
self.har_parser = HarParser(self.har_path)
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import unittest
|
|||||||
from httprunner.ext.har2case import utils
|
from httprunner.ext.har2case import utils
|
||||||
|
|
||||||
|
|
||||||
class TestUtils(unittest.TestCase):
|
class TestHar2CaseUtils(unittest.TestCase):
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def create_har_file(file_name, content):
|
def create_har_file(file_name, content):
|
||||||
file_path = os.path.join(
|
file_path = os.path.join(
|
||||||
@@ -24,7 +24,7 @@ class TestUtils(unittest.TestCase):
|
|||||||
self.assertIn("response", log_entries[0])
|
self.assertIn("response", log_entries[0])
|
||||||
|
|
||||||
def test_load_har_log_key_error(self):
|
def test_load_har_log_key_error(self):
|
||||||
empty_json_file_path = TestUtils.create_har_file(
|
empty_json_file_path = TestHar2CaseUtils.create_har_file(
|
||||||
file_name="empty_json", content={}
|
file_name="empty_json", content={}
|
||||||
)
|
)
|
||||||
with self.assertRaises(SystemExit):
|
with self.assertRaises(SystemExit):
|
||||||
@@ -32,7 +32,9 @@ class TestUtils(unittest.TestCase):
|
|||||||
os.remove(empty_json_file_path)
|
os.remove(empty_json_file_path)
|
||||||
|
|
||||||
def test_load_har_log_empty_error(self):
|
def test_load_har_log_empty_error(self):
|
||||||
empty_file_path = TestUtils.create_har_file(file_name="empty", content="")
|
empty_file_path = TestHar2CaseUtils.create_har_file(
|
||||||
|
file_name="empty", content=""
|
||||||
|
)
|
||||||
with self.assertRaises(SystemExit):
|
with self.assertRaises(SystemExit):
|
||||||
utils.load_har_log_entries(empty_file_path)
|
utils.load_har_log_entries(empty_file_path)
|
||||||
os.remove(empty_file_path)
|
os.remove(empty_file_path)
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import unittest
|
|||||||
from httprunner.ext.scaffold import create_scaffold
|
from httprunner.ext.scaffold import create_scaffold
|
||||||
|
|
||||||
|
|
||||||
class TestUtils(unittest.TestCase):
|
class TestScaffold(unittest.TestCase):
|
||||||
def test_create_scaffold(self):
|
def test_create_scaffold(self):
|
||||||
project_name = "projectABC"
|
project_name = "projectABC"
|
||||||
create_scaffold(project_name)
|
create_scaffold(project_name)
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import collections
|
|||||||
import json
|
import json
|
||||||
import os.path
|
import os.path
|
||||||
import platform
|
import platform
|
||||||
|
from typing import Dict, List, Any
|
||||||
|
|
||||||
from loguru import logger
|
from loguru import logger
|
||||||
|
|
||||||
@@ -151,3 +152,16 @@ def get_platform():
|
|||||||
),
|
),
|
||||||
"platform": platform.platform(),
|
"platform": platform.platform(),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def sort_dict_by_custom_order(raw_dict: Dict, custom_order: List):
|
||||||
|
def get_index_from_list(lst: List, item: Any):
|
||||||
|
try:
|
||||||
|
return lst.index(item)
|
||||||
|
except ValueError:
|
||||||
|
# item is not in lst
|
||||||
|
return len(lst) + 1
|
||||||
|
|
||||||
|
return dict(
|
||||||
|
sorted(raw_dict.items(), key=lambda i: get_index_from_list(custom_order, i[0]))
|
||||||
|
)
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
import io
|
|
||||||
import os
|
import os
|
||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
@@ -89,3 +88,13 @@ class TestUtils(unittest.TestCase):
|
|||||||
def test_print_info(self):
|
def test_print_info(self):
|
||||||
info_mapping = {"a": 1, "t": (1, 2), "b": {"b1": 123}, "c": None, "d": [4, 5]}
|
info_mapping = {"a": 1, "t": (1, 2), "b": {"b1": 123}, "c": None, "d": [4, 5]}
|
||||||
utils.print_info(info_mapping)
|
utils.print_info(info_mapping)
|
||||||
|
|
||||||
|
def test_sort_dict_by_custom_order(self):
|
||||||
|
self.assertEqual(
|
||||||
|
list(
|
||||||
|
utils.sort_dict_by_custom_order(
|
||||||
|
{"C": 3, "D": 2, "A": 1, "B": 8}, ["A", "D"]
|
||||||
|
).keys()
|
||||||
|
),
|
||||||
|
["A", "D", "C", "B"],
|
||||||
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user