对嵌套字典和列表的操作

时间:2018-10-09 11:27:58

标签: python

我有一个包含以下格式的测试结果的文件:

TEST1=passed
TEST2=failed
TEST3=passed

我想比较测试范围:

test_scope = {
    'SCOPE1': [
        'TEST1',
        'TEST2',
    ],
    'SCOPE2': [
        'TEST3',
    ],
    'SCOPE3': [
        'TEST4',
        'TEST5',
    ],
    'SCOPE4': [
        'TEST6',
        'TEST7',
    ],
}

理想的输出为:

test_results = {
    "SCOPE1": [
        {
            "name": "TEST1", 
            "result": "passed"
            "id": 0
        },
        {
            "name": "TEST2", 
            "result": "failed"
            "id": 1
        },
    ],
    "SCOPE2": [
        {
            "name": "TEST3", 
            "result": "passed"
            "id": 1
        },
    ],
    "SCOPE3": [
        {
            "name": "TEST4", 
            "result": "not run"
            "id": 1
        },
        {
            "name": "TEST5", 
            "result": "not run"
            "id": 1
        },
    ]
    "SCOPE4": [
        {
            "name": "TEST6", 
            "result": "not run"
            "id": 0
        },
        {
            "name": "TEST7", 
            "result": "not run"
            "id": 1
        },
    ],
}

其中idtest_scope列表中的测试名称索引。

我所取得的成就:

class TBD(object):
   def get_test_results(self, path, results_file):
        test_results = []
        with open('path', 'r') as results_file:
            lines = results_file.readlines()
            for line in lines:
                test_case = line.rstrip().split('=')[0]
                test_result = line.rstrip().split('=')[1]
                for section, tests in test_scope.iteritems():
                    if test_case in tests:
                        section = section
                        id = tests.index(test_case)
                        test_results.append({'name': test_case,
                                             'result': test_result,
                                             'section': section,
                                             'id': id)
        return test_results

输出:

[{'id': 0, 
  'section': 'SCOPE1', 
  'name': 'TEST1', 
  'result': 'passed'},
...]

但是我很困惑,不知道如何获得不运行结果的测试用例(那些出现在test_scope中但没有出现在test_results中)。

关于如何从这里进步的任何建议?当然,如果有更简单的存储方法,则可以自由更改数据结构,因为我还没有经验:)

1 个答案:

答案 0 :(得分:1)

将读取测试结果并输出test_results数据结构分为两个单独的步骤,而不是通过测试结果文件来驱动输出。

在第一步中,读取文件,并将信息存储到字典映射测试名称->结果中。在第二步中,通过遍历test_results数据结构来生成test_scope输出,使用步骤1生成的字典来确定测试结果。这使得包含test_scope all 条目变得微不足道,即使该测试不是测试运行的一部分。

# step 1, read the results file
results = {}
with open('path', 'r') as results_file:
    results = dict(line.strip().split('=') for line in results_file if line.strip())

# step 2, build the output
test_results = {
    scope: [
        {
            "id": i,
            "name": test_name,
            "result": results.get(test_name, "not run"),
        } for i, test_name in enumerate(tests)
    ] for scope, tests in test_scope.iteritems()
}

演示:

>>> from io import BytesIO
>>> results_file_data = '''\
... TEST1=passed
... TEST2=failed
... TEST3=passed
... '''
>>> with BytesIO(results_file_data) as results_file:
...     results = dict(line.strip().split('=') for line in results_file if line.strip())
...
>>> test_results = {
...     scope: [
...         {
...             "id": i,
...             "name": test_name,
...             "result": results.get(test_name, "not run"),
...         } for i, test_name in enumerate(tests)
...     ] for scope, tests in test_scope.iteritems()
... }
>>> from pprint import pprint
>>> pprint(test_results)
{'SCOPE1': [{'id': 0, 'name': 'TEST1', 'result': 'passed'},
            {'id': 1, 'name': 'TEST2', 'result': 'failed'}],
 'SCOPE2': [{'id': 0, 'name': 'TEST3', 'result': 'passed'}],
 'SCOPE3': [{'id': 0, 'name': 'TEST4', 'result': 'not run'},
            {'id': 1, 'name': 'TEST5', 'result': 'not run'}],
 'SCOPE4': [{'id': 0, 'name': 'TEST6', 'result': 'not run'},
            {'id': 1, 'name': 'TEST7', 'result': 'not run'}]}