接口持续集成测试----一个非常初级但还是有效果的方案

一直是这个思路,今天重写一下吧,一直想做成一个高度整合的类。另外,jenkins可以直接捕获exception,然后自动提单,很简略的接口自动化方案,不过效果还是可以滴,这里分享的是重写后的请求检测类。之所以要自己实现这些,主要是因为现有的开源接口框架(jmeter)没办法根据返回值里面不确定的字段做解析,所以重点是自己解析各种json文件,自己拼接了。

使用方法:
new ConnectBase(“http://www.baidu.com“);
就这一句:
1.根据参数类型自动判断get还是post,填入hashmap就好了
2.期望值可以是响应体的任意字段,也可以正则匹配
3.一个对象一个线程
这里写图片描述

给个示例:

new ConnectBase("http://www.baidu.com","百度");
new ConnectBase("http://www.qq.com","马化腾");
new ConnectBase("http://www.csdn.com","\\w+");

所以输出的结果就是这样的:
这里写图片描述

下面是源码:

package UrlTools;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Iterator;
import java.util.regex.Pattern;

public class ConnectBase {

    String _url;
    String resp_text;
    int resp_code;
    String expected;
    HashMap hashmap;

    public ConnectBase(String _url0, String expected0, HashMap hashmap0) {
        this._url = _url0;
        this.expected = expected0;
        this.hashmap = hashmap0;
        new Thread(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                ConnectNet(_url, expected, hashmap);
            }
        }).start();
    }

    public ConnectBase(String _url, HashMap hashmap) {
        this(_url, null, hashmap);
    }

    public ConnectBase(String _url) {
        this(_url, null, null);
    }

    public ConnectBase(String _url, String expected) {
        this(_url, expected, null);
    }

    private boolean ConnectNet(String _url, String expected, HashMap hashmap) {
        if (!_url.startsWith("http://")) {
            _url = "http://" + _url;
        }
        StringBuffer stringBuffer = null;
        if (hashmap != null) {
            stringBuffer = getParams(hashmap);
        }

        BufferedReader bufferedReader = null;
        HttpURLConnection connection = null;

        try {
            URL url = new URL(_url);
            connection = (HttpURLConnection) url.openConnection();
            connection.setDoOutput(true);
            connection.setDoInput(true);
            connection.setConnectTimeout(5000);
            connection.setReadTimeout(5000);
            if (stringBuffer == null) {
                connection.setRequestMethod("GET");
            } else {
                connection.setRequestMethod("POST");
            }
            connection.setRequestProperty("User-Agent",
                    "User-Agent: Dalvik/1.6.0 (Linux; U; Android 4.4.2; we20c Build/KOT49H)");
            connection.connect();
            resp_code = connection.getResponseCode();
            if (connection.getResponseCode() > 400) {
                String str = "[" + _url + "]" + "--->" + resp_code;
                System.out.println(str);
                try {
                    throw new MyException();
                } catch (MyException e) {
                }
                return false;
            }
            if (stringBuffer != null) {
                connection.getOutputStream().write(stringBuffer.toString().getBytes());
            }
            bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String tmp = null;
            String result = null;
            while ((tmp = bufferedReader.readLine()) != null) {
                result += tmp;
            }
            resp_text = result.substring(4);
            checkRsp(_url, resp_code, resp_text, expected);
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            if (bufferedReader != null) {
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if (connection != null) {
                connection.disconnect();
            }
        }
        return true;
    }

    private void checkRsp(String _url, int resp_code2, String resp_text2, String expected) {
        if (resp_text2 != null && expected != null) {
            Pattern pattern = Pattern.compile(expected);
            if (resp_text2.contains(expected) || pattern.matcher(resp_text2).find()) {
                String str = "[" + _url + "]" + "--->" + resp_code2 + "  期望值:" + expected + "|" + "匹配成功!";
                System.out.println(str);
            } else {
                String str = "[" + _url + "]" + "--->" + resp_code2 + "  期望值:" + expected + "|" + "匹配不到!";
                System.out.println(str);
                try {
                    throw new MyException();
                } catch (MyException e) {
                    // TODO Auto-generated catch block
                }
            }
        } else {
            String str = "[" + _url + "]" + "--->" + resp_code2;
            System.out.println(str);
        }
    }

    private StringBuffer getParams(HashMap hashmap) {
        StringBuffer stringBuffer = null;
        Iterator iterator = hashmap.keySet().iterator();
        while (iterator.hasNext()) {
            String _key = (String) iterator.next();
            stringBuffer.append(_key).append("=").append(hashmap.get(_key)).append("&");
        }
        stringBuffer.substring(0, stringBuffer.length() - 1);
        return stringBuffer;
    }
}

class MyException extends Exception {
    MyException() {
        String str = "some thing wrong,throws exception!!!";
        System.out.println(str);
    }
}

对于jenkins的配置也是非常简单,只需要直接运行项目的main里面的内容就行了,通过判断控制台输出的关键字,存在Exception这个关键字,那么判断不通过,然后自动发邮件。

和jmeter的结合:
我们知道jmeter扩展性能可以导入.jar包,用这个类还是没问题的,通过bashshell解析后把结果交给jmeter输出报告,也是可以滴,我是把定时任务和输出结果交给了Jenkins了。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值