介面測試-自動化-Java實現-InterfaceTest

來源:互聯網
上載者:User

標籤:

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()方法,用來截獲傳回值中的長度,如果長度過長,顯示省略符號。

介面測試-自動化-Java實現-InterfaceTest

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.