Jmeter介面測試使用beanshell斷言json返回

來源:互聯網
上載者:User

標籤:seo   data   北京   google   equals   lan   span   用例   tor   

一般情況下響應斷言就能解決很多問題,但是返回複雜的json時就需要用到beanshell斷言。

舉個例子

免費的介面API www.sojson.com/api/beian/sojson.com

host:    www.sojson.com

source:   api/beian/sojson.com (api/beian後面只能跟頂層網域如:baidu.com或者sojson.com)

建立好線程組HTTP請求,給該請求加beanshell斷言,代碼如下,其中必須加prev.setSuccessful(false);設定用例的結果

beanshell代碼如下

 1 import com.google.gson.JsonObject;     2 import com.google.gson.JsonParser; 3 import java.lang.*; 4 //擷取上一個請求的返回 5 response = prev.getResponseDataAsString();  6 log.info(response); 7  8 //正常情況的history應該為資料庫查詢出來的結果 9 String history="{\"nature\":\"企業\",\"icp\":\"京ICP備16038595號\",\"indexUrl\":\"www.sojson.com\",\"sitename\":\"JSON線上解析\",\"domain\":\" sojson.com \",\"nowIcp\":\"京ICP備16038595號-2\",\"type\":200,\"search\":\"sojson.com\",\"checkDate\":\"\",\"name\":\"北京都芳商貿有限公司\"}";10 //使用Gson解析json11 JsonParser parser = new JsonParser();    12 JsonObject responseObj = (JsonObject) parser.parse(response);13 14 JsonParser parser1 = new JsonParser();            15 JsonObject historyObj = (JsonObject) parser1.parse(history); 16 17 if(history == "")  18 {  19 //    Failure = true;  20     FailureMessage = "串連資料庫失敗或者資料庫內沒有曆史資料";   21       22 //調用Gson提供的Json對象euqals方法判斷是否一致  23 }else if(responseObj.equals(historyObj) == false)  24 {   25     log.info("不一樣");26     //設定該條用例結果,但是查看結果樹中不會列印出錯資訊FailureMessage27     //prev.setSuccessful(false);28 //把宣告失敗置為真,即用例失敗,並在結果樹中顯示FailureMessage 29 Failure = true;  30 FailureMessage = "請求返回和資料庫不匹配";   31 }32 else33 {34     log.info("倆一模一樣");    35     }

 

Jmeter介面測試使用beanshell斷言json返回

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.