I have a list of test-failures as shown below -
all_failures = [
'test1/path/to/test1/log/failure_reason1',
'test1/path/to/test1/log/failure_reason2',
'test2/path/to/test2/log/failure_reason1',
'test2/path/to/test2/log/failure_reason2',
'test3/path/to/test3/log/failure_reason1',
'test4/path/to/test4/log/failure_reason1',
]
I am trying to construct a JSON like object by parsing each failure in the list. So far, i have tried to write the following code -
for failure in all_failures:
data = failure.split('/',1)
test = data[0]
failure_details_dict[test] = []
data = '/' + data[1]
data = data.rsplit('/', 1)
test_details_dict['path'] = data[0] + '/'
test_details_dict['reason'] = data[1]
failure_details_dict[test].append(test_details_dict)
test_details_dict = {}
for key,value in failure_details_dict.items():
print(key)
print(value)
print()
The output i am getting is -
test4
[{'reason': 'failure_reason1', 'path': '/path/to/test4/log/'}]
test3
[{'reason': 'failure_reason1', 'path': '/path/to/test3/log/'}]
test1
[{'reason': 'failure_reason2', 'path': '/path/to/test1/log/'}]
test2
[{'reason': 'failure_reason2', 'path': '/path/to/test2/log/'}]
whereas, the expected output is -
{
"test1": [
{
"path": "/path/to/test1/log/",
"reason": "failure_reason1"
},
{
"path": "/path/to/test1/log/",
"reason": "failure_reason2"
}
],
"test2": [
{
"path": "/path/to/test2/log/",
"reason": "failure_reason1"
},
{
"path": "/path/to/test2/log/",
"reason": "failure_reason2"
}
],
"test3": [
{
"path": "/path/to/test3/log/",
"reason": "failure_reason1"
},
],
"test4": [
{
"path": "/path/to/test4/log/",
"reason": "reason1"
},
]
}
As we can see, I have not been able to add the second path and reason for failure to the same key. Example - test1 and test2 have two reasons for failure.
Can someone please help to understand what i am missing? Thank you!