(九)Java版接口自动化-使用钉钉机器人自动推送测试结果


一、配置钉钉机器人

可以参考钉钉机器人官方文档:https://open.dingtalk.com/document/robots/custom-robot-access

1、在钉钉上创建一个群聊或者项目群

 根据自己的喜欢,取一个群名

 对该群设置钉钉的机器人:设置-智能群助手-添加机器人

也可以在已有的群里点击智能群助手,添加机器人

选择自定义webhook,点击添加

设置机器人的名称,安全设置看自己需要设置即可,必需要选一个才能添加

点击完成,复制webhooK地址,后面会用到这个地址,也可以设置成功后,通过点击机器人-机器人设置-查看机器人的信息

 二、创建钉钉推送消息的方法

package com.automation.interfacetest.util;

import com.automation.interfacetest.base.RestTemplateRequest;
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 java.text.NumberFormat;


/**
 * @title:
 * @author: 2022/2/2514:10
 * @date: 2022/2/2514:10
 */
public class DingDingUtil {

    //通过钉钉机器人发送消息到钉钉群中
    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);
        HttpEntity request = 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;
    }

    public static void main(String[] args) {
        String c=DingDingUtil.folatToPer((float) 5/7);
        System.out.println(c);

    }
}

三、在ExtentTestNGIReporterListener中添加调用钉钉发送消息的方法,

下面的URL就是钉钉机器人的webhook地址,如果没有定义suiteSize的需要在前面定义下,初始化为0 

        suiteSize = suiteFailSize + suitePassSize + suiteSkipSize;
        Date now = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String NowData = formatter.format(now);

        //新加钉钉机器人测试报告
        String pass= DingDingUtil.folatToPer((float)suitePassSize/suiteSize);
        String url ="https://oapi.dingtalk.com/robot/send?access_token=6196580504bf64867628e6d0ed767cd40e73a8beb5138dd07c9fa0a0d3e9fdc8";
        String context="【本次接口自动化用例执行结果】" + "\n用例执行时间:" + NowData + "\n总用例数为:" +suiteSize+ ";\n通过用例数为:" +suitePassSize+ ";\n失败用例数为:" +suiteFailSize+ ";\n跳过用例数为:" +suiteSkipSize+ ";\n通过率为:"+pass;
        try {
            DingDingUtil.sendVal(url,context);
        } catch (Exception e) {
            e.printStackTrace();
        }

四、测试调用

1、新建一个测试类:

package com.automation.interfacetest;

import org.testng.Assert;
import org.testng.annotations.Test;

/**
 * @title:
 * @author: 2022/2/2810:38
 * @date: 2022/2/2810:38
 */
public class DingDingTestCases {
    @Test
    public void test001() throws Exception{
        String in = "Hello";
        Assert.assertEquals("Hello",in);
    }

    @Test
    public void test002() throws Exception{
        String in = "Hello";
        Assert.assertEquals("Hello1",in);
    }
    @Test
    public void test003() throws Exception{
        String in = "Hello";
        Assert.assertEquals("Hello",in);
    }

    @Test
    public void test004() throws Exception{
        String in = "Hello";
        Assert.assertEquals("Hello",in);
    }
}

2、在testng.xml里引入测试类,运行测试用例集

<?xml version="1.0" encoding="UTF-8"?>

    
        
            <class name="com.automation.interfacetest.DingDingTestCases">class>
        
    
    
        class-name="com.automation.interfacetest.extend.ExtentTestNGIReporterListener"/>
    

3、查看推送结果

相关