#! /usr/bin/env pyhton
# -*- coding:utf-8 -*-
# author:jeff.xie
# datetime:2024/2/2 14:03
# software:PyCharm
import json
def get_case_name_status(path):
# 读取json文件
with open(path, 'r', encoding='utf-8') as f:
data = json.load(f)
# 解析json数据
function_name = data["name"]
print(function_name)
status = data["testStage"]["status"] # 解析 testStage对应json内容,取status对应的值
case_name = data["testStage"]["steps"][0]["name"]
# 解析 testStage对应json内容,取steps对应的json数据的第一个json数据,取name对应的值
print(status)
print(case_name)
if __name__ == '__main__':
path = 'case_status.json'
get_case_name_status(path)
Json文件内容
{
"uid" : "3bd701331fbd1356",
"name" : "test_05",
"fullName" : "testcases.test_weather.Test_weather#test_05",
"historyId" : "04bdebd4cda63fb0e230d98dbfff4f7f",
"time" : {
"start" : 1706839388327,
"stop" : 1706839388479,
"duration" : 152
},
"status" : "passed",
"flaky" : false,
"beforeStages" : [ {
"name" : "_xunit_setup_class_fixture_Test_weather",
"time" : {
"start" : 1706839388326,
"stop" : 1706839388326,
"duration" : 0
},
"status" : "passed",
"steps" : [ ],
"attachments" : [ ],
"parameters" : [ ],
"hasContent" : false,
"stepsCount" : 0,
"attachmentsCount" : 0,
"shouldDisplayMessage" : false
} ],
"testStage" : {
"status" : "passed",
"steps" : [ {
"name" : "获取上海天气",
"time" : {
"start" : 1706839388327,
"stop" : 1706839388479,
"duration" : 152
},
"status" : "passed",
"steps" : [ ],
"attachments" : [ ],
"parameters" : [ ],
"hasContent" : false,
"stepsCount" : 0,
"attachmentsCount" : 0,
"shouldDisplayMessage" : false
} ],
"attachments" : [ {
"uid" : "58a67a26a5822186",
"name" : "log",
"source" : "58a67a26a5822186.txt",
"type" : "text/plain",
"size" : 242
} ],
"parameters" : [ ],
"hasContent" : true,
"stepsCount" : 1,
"attachmentsCount" : 1,
"shouldDisplayMessage" : false
},
"afterStages" : [ {
"name" : "_xunit_setup_class_fixture_Test_weather::0",
"time" : {
"start" : 1706839388797,
"stop" : 1706839388798,
"duration" : 1
},
"status" : "passed",
"steps" : [ ],
"attachments" : [ ],
"parameters" : [ ],
"hasContent" : false,
"stepsCount" : 0,
"attachmentsCount" : 0,
"shouldDisplayMessage" : false
} ],
"labels" : [ {
"name" : "severity",
"value" : "trivial"
}, {
"name" : "parentSuite",
"value" : "testcases"
}, {
"name" : "suite",
"value" : "test_weather"
}, {
"name" : "subSuite",
"value" : "Test_weather"
}, {
"name" : "host",
"value" : "QH20121401L"
}, {
"name" : "thread",
"value" : "5420-MainThread"
}, {
"name" : "framework",
"value" : "pytest"
}, {
"name" : "language",
"value" : "cpython3"
}, {
"name" : "package",
"value" : "testcases.test_weather"
}, {
"name" : "resultFormat",
"value" : "allure2"
} ],
"parameters" : [ ],
"links" : [ {
"name" : "https://www.baidu.com",
"url" : "https://www.baidu.com",
"type" : "issue"
} ],
"hidden" : false,
"retry" : false,
"extra" : {
"severity" : "trivial",
"retries" : [ ],
"categories" : [ ],
"tags" : [ ]
},
"source" : "3bd701331fbd1356.json",
"parameterValues" : [ ]
}