(十二)Java版接口自动化-Jenkins+java+发送钉钉消息
一、通过钉钉机器人发送钉钉消息的代码实现
1、钉钉机器人配置我已经创建成功,并已经配置好
2、通过读取allure生成的测试结果,取值出来发送钉钉消息
package com.automation.interfacetest.util; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.web.client.RestTemplate; import org.testng.Reporter; import org.testng.annotations.Test; import java.io.BufferedReader; import java.io.FileReader; import java.text.NumberFormat; import java.util.HashMap; import java.util.Map; /** * @title: * @author: 2022/2/2514:10 * @date: 2022/2/2514:10 */ public class DingDingUtil { public static String pathname = "D:\\github\\interface-test\\interfacetest\\interfacetest\\target\\allure-report\\export\\prometheusData.txt"; public static String url ="https://oapi.dingtalk.com/robot/send?access_token=6196580504bf64867628e6d0ed767cd40e73a8beb5138dd07c9fa0a0d3e9fdc8"; //通过钉钉机器人发送消息到钉钉群中 public static void sendVal(String url,String context)throws Exception{ System.out.println("====打印content====:" + context); String entityString ="{'msgtype': 'text', 'text': {'content': '"+context+"'}}"; RestTemplate restTemplate = new RestTemplate(); //设置请求头 HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON); HttpEntityrequest = new HttpEntity<>(entityString, headers); ResponseEntity response = restTemplate.postForEntity( url, request , String.class ); } //folat数字转换为百分号格式 public static String folatToPer(float folatNum){ NumberFormat numberFormat = NumberFormat.getPercentInstance(); String per=null; try { numberFormat.setMaximumFractionDigits(2); //精确到2位 per = numberFormat.format(folatNum); Reporter.log("小数点数字转百分数字符串:" + folatNum + " 转为 " + per); } catch (Exception e) { e.printStackTrace(); } return per; } //读取txt文件内容 public static Map readText(String pathname) throws Exception{ //String pathname = "D:\\github\\interface-test\\interfacetest\\interfacetest\\target\\allure-report\\export\\prometheusData.txt"; FileReader reader = new FileReader(pathname); BufferedReader br = new BufferedReader(reader); String line=null; Map map = new HashMap<>(); while ((line = br.readLine()) != null) { String launch = line.split(" ")[0]; String num = line.split(" ")[1]; map.put(launch,num); } return map; } //读取TXT的测试结果通过钉钉机器人发送钉钉消息 public static void readTxtSendDingTalk()throws Exception{ Map map = new HashMap(); map = readText(pathname); String total = map.get("launch_retries_run").toString(); String pass = map.get("launch_status_passed").toString(); String fail = map.get("launch_status_failed").toString(); String skip = map.get("launch_status_skipped").toString(); //新加钉钉机器人测试报告 //String passRate= DingDingUtil.folatToPer((float)pass/total); //String url ="https://oapi.dingtalk.com/robot/send?access_token=6196580504bf64867628e6d0ed767cd40e73a8beb5138dd07c9fa0a0d3e9fdc8"; String context="【本次接口自动化用例执行结果】" + "\n总用例数为:" +total+ ";\n通过用例数为:" +pass+ ";\n失败用例数为:" +fail+ ";\n跳过用例数为:" +skip; try { DingDingUtil.sendVal(url,context); } catch (Exception e) { e.printStackTrace(); } } //@Test //public void test() throws Exception{ // readTxtSendDingTalk(); //} public static void main(String[] args) throws Exception{ //String c=DingDingUtil.folatToPer((float) 5/7); //System.out.println(c); readTxtSendDingTalk(); } }
二、Jenkins上配置运行脚步
1、Jenkins新增Post Build task 插件
2、配置运行脚步
没有编译的需要先编译
使用mvn运行java程序的可以参考:https://blog.csdn.net/qbg19881206/article/details/19850857
cd D:\github\interface-test\interfacetest\interfacetest mvn exec:java -Dexec.mainClass="com.automation.interfacetest.util.DingDingUtil"
三、测试验证
参考文章:https://blog.csdn.net/juhua2012/article/details/97811836