mirror of
https://github.com/httprunner/httprunner.git
synced 2026-05-12 02:21:29 +08:00
fix #418: deepcopy testcase when testcase contains BufferReader
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
# encoding: utf-8
|
||||
|
||||
import ast
|
||||
import copy
|
||||
import os
|
||||
import re
|
||||
|
||||
@@ -601,7 +600,6 @@ def parse_tests(testcases, variables_mapping=None):
|
||||
list: parsed testcases list, with config variables/parameters/name/request parsed.
|
||||
|
||||
"""
|
||||
# exception_stage = "parse tests"
|
||||
variables_mapping = variables_mapping or {}
|
||||
parsed_testcases_list = []
|
||||
|
||||
@@ -629,7 +627,7 @@ def parse_tests(testcases, variables_mapping=None):
|
||||
) or [{}]
|
||||
|
||||
for parameter_mapping in cartesian_product_parameters_list:
|
||||
testcase_dict = copy.deepcopy(testcase)
|
||||
testcase_dict = utils.deepcopy_dict(testcase)
|
||||
config = testcase_dict.get("config")
|
||||
|
||||
# parse config variables
|
||||
|
||||
@@ -203,6 +203,42 @@ def convert_mappinglist_to_orderdict(mapping_list):
|
||||
return ordered_dict
|
||||
|
||||
|
||||
def deepcopy_dict(data):
|
||||
""" deepcopy dict data, ignore file object (_io.BufferedReader)
|
||||
|
||||
Args:
|
||||
data (dict): dict data structure
|
||||
{
|
||||
'a': 1,
|
||||
'b': [2, 4],
|
||||
'c': lambda x: x+1,
|
||||
'd': open('LICENSE'),
|
||||
'f': {
|
||||
'f1': {'a1': 2},
|
||||
'f2': io.open('LICENSE', 'rb'),
|
||||
}
|
||||
}
|
||||
|
||||
Returns:
|
||||
dict: deep copied dict data, with file object unchanged.
|
||||
|
||||
"""
|
||||
try:
|
||||
return copy.deepcopy(data)
|
||||
except TypeError:
|
||||
copied_data = {}
|
||||
for key, value in data.items():
|
||||
if isinstance(value, dict):
|
||||
copied_data[key] = deepcopy_dict(value)
|
||||
else:
|
||||
try:
|
||||
copied_data[key] = copy.deepcopy(value)
|
||||
except TypeError:
|
||||
copied_data[key] = value
|
||||
|
||||
return copied_data
|
||||
|
||||
|
||||
def update_ordered_dict(ordered_dict, override_mapping):
|
||||
""" override ordered_dict with new mapping.
|
||||
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import io
|
||||
import os
|
||||
import shutil
|
||||
|
||||
@@ -214,6 +215,26 @@ class TestUtils(ApiServerUnittest):
|
||||
self.assertIsInstance(ordered_dict, dict)
|
||||
self.assertIn("a", ordered_dict)
|
||||
|
||||
def test_deepcopy_dict(self):
|
||||
data = {
|
||||
'a': 1,
|
||||
'b': [2, 4],
|
||||
'c': lambda x: x+1,
|
||||
'd': open('LICENSE'),
|
||||
'f': {
|
||||
'f1': {'a1': 2},
|
||||
'f2': io.open('LICENSE', 'rb'),
|
||||
}
|
||||
}
|
||||
new_data = utils.deepcopy_dict(data)
|
||||
data["a"] = 0
|
||||
self.assertEqual(new_data["a"], 1)
|
||||
data["f"]["f1"] = 123
|
||||
self.assertEqual(new_data["f"]["f1"], {'a1': 2})
|
||||
self.assertEqual(id(new_data["d"]), id(data["d"]))
|
||||
self.assertNotEqual(id(new_data["b"]), id(data["b"]))
|
||||
self.assertEqual(id(new_data["c"]), id(data["c"]))
|
||||
|
||||
def test_update_ordered_dict(self):
|
||||
map_list = [
|
||||
{"a": 1},
|
||||
|
||||
Reference in New Issue
Block a user