1. 程式人生 > 其它 >pytest_runtest_makereport 獲取pytest的測試結果和caseid

pytest_runtest_makereport 獲取pytest的測試結果和caseid

目的: 自動獲取pytest case執行結果和caseid 存為變數,後續可以和case管理工具整合

@pytest.hookimpl(hookwrapper=True, tryfirst=True)

def pytest_runtest_makereport(item, call):

out = yield
print("Case result is: - ", out)
report = out.get_result()

if report.when == "call":
print("test report: %s" % report)
print("test step: %s" % report.when)
print("nodeid: %s" % report.nodeid)
# print("description is : %s" % str(item.function.__doc__))
print("execute result: %s" % report.outcome)

global Case_Result, Case_Name, Case_ID

case_result = (True if report.outcome == "passed" else False) # case執行結果轉換成boolean 型別
Case_Result = case_result

case_name = report.nodeid.split("::")[-1]  # 獲取 testcase name, nodeid: testcase/SmartSharedList/test_SmartSharedList.py::Test_SmartSharedList::test_TC_13096_ARM4329_56789


case_id = case_name.split("_")[-3] # 獲取caseID, test_TC_13096_ARM4329_56789
Case_Name = case_name
Case_ID = case_id

#  Upload result to qtest through caseid and result
print("Upload result to qtest")