JMeter之BeanShell使用实例: 断言返回的数据类型

先把用到的jar包org.json放在jmeter/lib文件下,并在测试计划中导入 

思路 

function(standardData,respData){ 

  standardJson =getJson(); 

  respJson = getJson(respData); 

   循环N次{ 

         if(standardJson.getType == respJson.getType); //逐个判断json的字段类型是否符合标准 

Beanshell代码 

import org.json.JSONArray; 

import org.json.JSONObject; 

import java.util.Iterator; 

import java.util.Map; 

  

public static String equalsJson(JSONObject standardJson, JSONObject responseJson) {//输入两个json,判断第一个里面的所有字段在第二个中的类型是否相同 

    String err_message = "";  

    Iterator it = standardJson.keys(); // 储存所有要验证的Key 

    //log.error("it=:"+it.toString()); 

     while (it.hasNext()) { 

          String key = (String) it.next(); 

          //log.error("key=:"+key); 

          String thisKetType = standardJson.get(key).getClass().getName(); //获取当前Key的标准type 

          log.error("standard Key = " + key + ", Value = " + standardJson.get(key) + ", Type = " + thisKetType); 

  

          if(responseJson.isNull(key)){ //判断response中有无当前Key 

             log1 = "------ExistError: " + key + " Not found."; 

             log.info("!!Failed: " + log1); 

             err_message = (err_message + "\n" + log1); 

          } 

          else{  //response中找到Key了,再判断type 

               String respKetType = responseJson.get(key).getClass().getName(); //获取响应的字段类型 

  

               if(respKetType.equals(thisKetType)){ 

                    log.info("Passed."); 

                    if(thisKetType.equals("org.json.JSONObject")){ //object类型的字段继续往内层判断 

                        err_message += equalsJson(standardJson.getJSONObject(key), responseJson.getJSONObject(key)); //!!进入递归时,保存当前错误信息 

                    } 

                } else { 

                    String log1 = "------TypeError : " + key + " is " + respKetType + " (should be " + thisKetType + ")"; 

                    log.info("!!Failed: " + log1); 

                    err_message = (err_message + "\n" + log1); 

                } 

            } 

        } 

     return err_message; 

public static Boolean respTypeAssertion(String standardData) { //输入标准响应,转为json并调用比较函数,得到断言结果 

    String resData = prev.getResponseDataAsString(); //获取前一个请求的响应 

    log.info("接口返回result: " + resData); 

    JSONObject standardJson = new JSONObject(standardData);  

    JSONObject jo = new JSONObject(resData); 

    // log.info("jo=:"+jo.toString()); 

    JSONObject responseJson = jo.getJSONObject("result"); 

    //log.info("responseJson=:"+responseJson.toString()); 

    log.info("------------------------Beanshell assertion-------------"); 

    String message = equalsJson(standardJson, responseJson); 

    log.info("------------------------ResultMessage--------------------" + message); 

    if(message == ""){    //如果错误信息是空,说明断言结果通过 

        FailureMessage = "Pass!"; 

        return true; 

    } 

    else{   //有错误信息打印到断言结果里面 

        Failure=true; 

        FailureMessage = "Type Error!" + message; 

    } 

    return false; 

}    

  

String standardData = "{'status':'true','diaryId':'10216','firstDiary':'0'}";//上传新建日记hcUploadNewDiary接口的返回数据 

//String standardData = "{'uid':'123','phone':'13580478329','has_password':true,'location':{'province':true,'city':'\u6c55\u5934\u5e02'},'cpma':null}"; 

respTypeAssertion(standardData); 

你可能感兴趣的:(测试,jmeter)