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()方法,用来截获返回值中的长度,如果长度过长,显示省略号。

  • 相关阅读:
    Magicodes.IE之Excel模板导出教材订购表
    【总结】《氨基酸新晋管理者领导力培训》第一次课_Day2_学习心得
    【总结】《氨基酸新晋管理者领导力培训》第一次课_Day1_学习心得
    【管理】从技术走向管理
    python写入excel(xlswriter)--生成图表
    python-写入excel(xlswriter)
    python读取excel(xlrd)
    python接口自动化测试(八)-unittest-生成测试报告
    python接口自动化测试(六)-unittest-单个用例管理
    python接口自动化测试(七)-unittest-批量用例管理
  • 原文地址:https://www.cnblogs.com/kevinqinan/p/4581041.html
Copyright © 2011-2022 走看看