zoukankan      html  css  js  c++  java
  • 接口测试-自动化-Java实现-InterfaceTest

    InterfaceTest,在TestMain中可以看到是实现测试测试的基本方法。

    代码如下:

    public class InterfaceTest {
        final static String param = "param";
        HtmlFile hf = new HtmlFile();
    
        public  void test1(String p_paramcontent,
                String p_description, String p_url) {
    
            long startTime = System.currentTimeMillis();
            String resultString = "";
    
            String msg = p_paramcontent;
            JSONObject jsonObject1 = null;
            try {
                jsonObject1 = new JSONObject(msg);
            } catch (JSONException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
    
            Map<String, String> dataMap = new HashMap<String, String>();
    
            dataMap.put(param, jsonObject1.toString());
    
            try {
                resultString = new HttpUtil().doPost(p_url, dataMap);
                JSONObject jsonObject = new JSONObject(resultString);
                String resultStatues = jsonObject.getString("success");
                if (resultStatues != null && resultStatues != "") {
                    if (resultStatues.equalsIgnoreCase("true")) {
                        // 输出日志
                        hf.write(p_description, changeResultString(resultString), "true");
                    } else {
                        hf.write(p_description, changeResultString(resultString), "false");
                    }
    
                } else {
                    hf.write(p_description, changeResultString(resultString), "false");
                }
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                hf.write(p_description, changeResultString(resultString), "false");
            }
    
        }
    
        public  void test2(String p_paramcontent,
                String p_description, String p_url) {
    
            long startTime = System.currentTimeMillis();
    
            String resultString = "";
    
            String msg = p_paramcontent;
            JSONObject jsonObject1 = null;
            try {
                jsonObject1 = new JSONObject(msg);
            } catch (JSONException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
    
            Map<String, String> dataMap = new HashMap<String, String>();
    
            dataMap.put(param, jsonObject1.toString());
    
            try {
                resultString = new HttpUtil().doPost(p_url, dataMap);
                JSONObject jsonObject = new JSONObject(resultString);
                String resultStatues = jsonObject.getString("success");
                if (resultStatues != null && resultStatues != "") {
                    hf.write(p_description, changeResultString(resultString), "true");
                } else {
                    hf.write(p_description, changeResultString(resultString), "false");
                }
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                hf.write(p_description, changeResultString(resultString), "false");
            }
    
        }
    
    
        public static String changeResultString(String p_resultString) {
    
            StringBuilder resultString = new StringBuilder();
            if (p_resultString != "" && p_resultString != null) {
                if (p_resultString.length() > 70) {
                    resultString.append(p_resultString.substring(0, 70));
                    resultString.append("...");
    
                } else {
                    resultString.append(p_resultString);
                }
            } else {
                resultString.append("返回值为空");
            }
    
            return resultString.toString();
    
        }
    }

    其中包含了三个方法

    1. test1() 和 test2()其实是相同的方法,只是中间判断的方式不同,因为某些接口是使用了success 字段 == True 来判断是否成功的,而有些并非如此。

    2. test1()或者test2()中,使用HttpUtil().doPost方法,获得服务器返回的接口,从中匹配success值是否等于True或者False,来进行判断接口是否通过。

    3. 输出日志的方式是通过HtmlFile中的Write方法把结果写到Html中。

    4. changeResultString()方法,用来截获返回值中的长度,如果长度过长,显示省略号。

  • 相关阅读:
    古谚、评论与论断、名篇与名言
    重读《西游记》
    重读《西游记》
    命名之法 —— 时间、季节、地点
    命名之法 —— 时间、季节、地点
    文言的理解 —— 古时的称谓、别称、别名
    文言的理解 —— 古时的称谓、别称、别名
    Oracle GoldenGate for Oracle 11g to PostgreSQL 9.2.4 Configuration
    瀑布 敏捷 文档
    POJ 1325 ZOJ 1364 最小覆盖点集
  • 原文地址:https://www.cnblogs.com/kevinqinan/p/4581041.html
Copyright © 2011-2022 走看看