字典 - 类型字典的值未正确更新/复制

时间:2018-01-06 17:01:14

标签: python python-3.x dictionary

我正在编写一个脚本,以{ "test_name":{test scenario} }格式为Rest API生成测试用例。例如,对于每个int, bool, string, [], {}等,创建一个新的测试用例,其中包含具有该指定项的所有JSON值。

from copy import copy
import pprint

#test json
test_json = {
    "id": 1,
    "name": "Leanne Graham",
    "username": "Bret",
    "email": "Sincere@april.biz",
    "address": {
      "street": "Kulas Light",
      "suite": "Apt. 556",
      "city": "Gwenborough",
      "zipcode": "92998-3874",
      "geo": {
        "lat": "-37.3159",
        "lng": "81.1496"
      }
    },
    "phone": "1-770-736-8031 x56442",
    "website": "hildegard.org",
    "company": {
      "name": "Romaguera-Crona",
      "catchPhrase": "Multi-layered client-server neural-net",
      "bs": "harness real-time e-markets"
    }
  }


class TestCaseGenerator:

    def update_json_values(self, json_o, value):
        #updates json_o values with given value
        for k, v in dict(json_o).items():
            if isinstance(v, dict):
                self.update_json_values(v, value)
            else:
                json_o[k] = value
        return json_o

    @staticmethod
    def set_test_case_name(http_verb, value):
        #builds a string with the given params
        test_name = http_verb + " all values as " + type(value).__name__
        return test_name

    def generate_test_cases(self, http_verbs, json_o, values):
        """
        Generates API test cases based on given JSON and a list of values
        :param http_verbs: (List) HTTP verbs to be used in tests (POST, PUT) etc.
        :param json_o: (JSON) object based on which the tests are created
        :param values: (List) values to be used for generated JSON test cases
        :return: (Dict) Test cases in key-value format:
         {
          "POST all values as int":{"id":1,"name":1..},
          "POST all values as bool":{"id":True,"name":True..}
         }
        """
        tests = dict()
        for http_verb in http_verbs:
            for value in values:
                #I think the problem is here somewhere
                test = {self.set_test_case_name(http_verb, value):/
                copy(self.update_json_values(json_o, value))}
                tests.update(test)
        return tests

#testing the script
t = TestCaseGenerator()
# to run the script, I give a list of HTTP verbs
verbs = ["POST", "PUT"]
# and a list of values that will be used to generate test cases
values = [10, 20.5, True, "string"]

pprint.pprint(t.generate_test_cases(verbs, test_json, values))

问题是,包含字典的字典键未正确更新。它采用给定值列表中的最后一个值:

'POST all values as int': {'address': {'city': 'test string', #incorrect, should be 'city': 10
                                    'geo': {'lat': 'test string',
                                            'lng': 'test string'},
                                    'street': 'test string',
                                    'suite': 'test string',
                                    'zipcode': 'test string'},
                        'company': {'bs': 'test string',
                                    'catchPhrase': 'test string',
                                    'name': 'test string'},
                        'email': 10,
                        'id': 10,
                        'name': 10,
                        'phone': 10,
                        'username': 10,
                        'website': 10},

0 个答案:

没有答案