sendmsg_test.py 2.6 KB
# -*- coding:utf-8 -*-
# @Time     :2019/12/4 9:37
# @Author   :dengyuting
# @File     :sendmsg.py
import os
import json

import requests

JENKINS_URL = str(os.getenv("JENKINS_URL"))
BUILD_NUMBER = str(os.getenv("BUILD_NUMBER"))
JOB_NAME = str(os.getenv("JOB_NAME"))
CAUSE = str(os.getenv("CAUSE"))
BUILD_URL = str(os.getenv("BUILD_URL"))
JOB_URL = str(os.getenv("JOB_URL"))

path = os.path.dirname(__file__)
summary_file = os.path.join(path, 'logs','testsuites','partner_testsuite.summary.json')

def getresult():
    try:
        with open(summary_file, 'r', encoding='UTF-8') as f:
            f = f.read()
            jsonfile = json.loads(f)
            if jsonfile["success"] == True:
                result = "SUCCESS"
            else:
                result = "FAIL"
            total = jsonfile["stat"]["testcases"]["total"]
            success = jsonfile["stat"]["testcases"]["success"]
            fail = jsonfile["stat"]["testcases"]["fail"]
            duration = round(jsonfile["time"]["duration"])
            # teststeps_total = jsonfile["stat"]["teststeps"]["total"]
            # teststeps_successes = jsonfile["stat"]["teststeps"]["successes"]
            # teststeps_failures = jsonfile["stat"]["teststeps"]["failures"]
            # teststeps_errors = jsonfile["stat"]["teststeps"]["errors"]
            # teststeps_skipped = jsonfile["stat"]["teststeps"]["skipped"]
            return result, total, success, fail, duration
    except Exception as err:
        print("this is err:{}" .format(err))



def sendinfo():
    r =getresult()
    data = {}
    data['msgtype'] = 'text'
    data['text'] = {}
    data['text']['content'] = '天联共创项目【mmm_partner_api_test】:' \
                              '\n测试结果:'+r[0]+ \
                              '\n执行时间:' +str(r[4])+ \
                              's\n共执行测试用例:' +str(r[1]) +'\nSUCCESS:' +str(r[2])+ '\nFAIL:' +str(r[3])+ \
                              '\n测试报告地址:http://jenkins.fjmaimaimai.com/view/test/job/mmm_partner_api_test/HTML_20Report/'
    # data['text']['mentioned_mobile_list'] = ["13559112969","@all"]
    # data['text']['content'] = '【opp_api】测试执行结果:' + r[0] + \
    #                           '\n 执行时间:' + str(r[4]) + \
    #                           's\n TOTAL:' + str(r[1]) + '\n SUCCESS:' + str(r[2]) + '\n FAIL:' + str(r[3])

    url = 'https://oapi.dingtalk.com/robot/send?access_token=157a80f4a09380f1338a182de1e120b42ba2d07cd6b729b288543e4fd3f6d160'
    headers = {'Content-Type': 'application/json'}

    requests.post(url, json=data, headers=headers)

if __name__ == '__main__':
    sendinfo()