The httpclient interface tests the complete use case and the method to obtain the information

Original address https://blog.csdn.net/fhaohaizi/article/details/78088075

Original address https://blog.csdn.net/fhaohaizi/article/details/79030397

--------------------------------------- Very good automation blog ------ -------------------------------

Original address https://blog.csdn.net/fhaohaizi/article/category/7084915

Original address https://blog.csdn.net/fhaohaizi/article/category/6772734

In the process of using httpclient for interface testing, I summarized some methods and wrote a basic testing framework. The management of use cases and the storage of test results are placed in the database. Today, it is considered that the template of use cases has been basically completed and improved. Share it for your reference.

 

[java]  view plain copy  
 
  1. public void case_shequ_topic_cattlist() {  
  2.     String apiName = "shequ_topic_cattlist";  
  3.     mark = getMark(); // get timestamp as test mark  
  4.     List<Map<String, String>> date = LocalMySql.getInstance().getCaseFromMySql(apiName); // database read case  
  5.     for ( int i =  0; i < date. size(); i++) { // traverse execution cases  
  6.         int result = 0;  
  7.         String expect_value1 = "", actual_value1 = "", expect_value2 = "", actual_value2 = "", params = "",  
  8.                 actual_key1 = "", actual_key2 = "", actual_key3 = "", expect_value3 = "", actual_value3 = "";  
  9.         Map<String, String> use = date.get(i); // get a single use case  
  10.         String case_id = use.get("case_id");  
  11.         /* 
  12.          * Get the key and expect_value of the checkpoint. If there is one checkpoint, write one, compatible with multiple getresult methods. Compatible with the absence of verification points 
  13.          */  
  14.         expect_value1 = use.get( "verify_value1"); // Get the expected value of the verification point  
  15.         expect_value2 = use.get( "verify_value2"); // Get the expected value of the verification point  
  16.         expect_value3 = use.get( "verify_value3"); // Get the expected value of the verification point  
  17.         actual_key1 = use.get( "verify_key1"); //Get the checkpoint key  
  18.         actual_key2 = use.get( "verify_key2"); //Get the checkpoint key  
  19.         actual_key3 = use.get( "verify_key3"); //Get the checkpoint key  
  20.         params = use.toString(); // record incoming parameters  
  21.         JSONObject response = shequ_topic_cattlist(use); // get the corresponding result  
  22.         actual_value1 = getDateValue(response, actual_key1); //Get the actual value of the verification point  
  23.         actual_value2 = getDateValue(response, actual_key2); //Get the actual value of the verification point  
  24.         actual_value3 = getDateContains(response, actual_key3, expect_value3); //Get the actual value of the verification point, this is the containment verification  
  25.         result = getResult(expect_value1, actual_value1, expect_value2, actual_value2) ?  1 :  2; // Get the test result, 1 is passed, 2 is failed  
  26.         LocalMySql.getInstance().saveApiTestResult(case_id, mark, result, apiName, expect_value1, actual_value1,  
  27.                 expect_value2, actual_value2, expect_value3, actual_value3, params); // write to database  
  28.     }  
  29.     LocalMySql.getInstance().addApiTestResult(apiName, mark, test_mark); //Statistics of all test results in this run  
  30. }  

Method encapsulation for getting the response:

 

[java]  view plain copy  
 
  1. public JSONObject shequ_topic_cattlist(Map<String, String> apiCase) {  
  2.         String uri = host + "/shequ/topic/cattlist";  
  3.         deleteMap(apiCase);  
  4.         JSONObject jsonObject = new JSONObject(apiCase);  
  5.         HttpGet httpGet = getHttpGet (uri, jsonObject);  
  6.         JSONObject response = getHttpResponseEntityByJson(httpClient, httpGet);  
  7.         return response;  
  8.     }  


The template is ready, and other use cases can be reused directly, just change the interface name and method name, it looks very neat.

 

When using httpclient to send a request, the method of getting information

 

In the process of using httpclient for automated interface testing, I need to save the request to the database every time I make a request so that I can resend the request. And you have to save the request type, host address, interface name, and parameters separately, so I wrote a separate method to save and extract the requested information before using the client to send a request each time, compatible with get and post, and pass parameters The type is HttpRequestBase, and a forced conversion is involved in the middle. Share the code for your reference.

 

 

 

[java]  view plain copy  
 
  1. /** 
  2.  * Encapsulate the method of obtaining various information of the request 
  3.  *  
  4.  * @param httpRequestBase 
  5.  * incoming request object 
  6.  * @return returns a map, including api_name, host_name, type, method, params 
  7.  */  
  8. public Map<String, String> getRequestInfo(HttpRequestBase request) {  
  9.     Map<String, String> info =  new HashMap<>(); // Create a new map to save information  
  10.     String method = request.getMethod();// 获取method  
  11.     info.put("method", method);  
  12.     String uri = request.getURI().toString();// 获取uri  
  13.     info.put("uri", uri);  
  14.     String url = uri;  
  15.     if (uri.contains( "?")) { // Get the url, if it is a get request, intercept it first  
  16.         url = uri.substring(0, uri.indexOf("?"));  
  17.     }  
  18.     String one = url.substring(url.indexOf( "//") +  2);// delete http://  
  19.     String api_name = one.substring(one.indexOf( "/")); // Get the interface name  
  20.     info.put("api_name", api_name);  
  21.     String host_name = one.substring( 0, one.indexOf( "/")); // Get the host address  
  22.     info.put("host_name", host_name);  
  23.     String type = url.substring( 0, url.indexOf( "//") -  1);// Get the protocol type  
  24.     info.put("type", type);  
  25.     String params =  null; // parameters  
  26.     if (method.equals("GET")) {  
  27.         params = uri.substring(uri.indexOf("?") + 1, uri.length());  
  28.     } else if (method.equals("POST")) {  
  29.         HttpPost httpPost = (HttpPost) request; // Force the httppost request  
  30.         HttpEntity entity = httpPost.getEntity(); // get entity  
  31.         try {  
  32.             params = EntityUtils.toString(entity); // resolve entity  
  33.             EntityUtils.consume(entity); // Make sure the entity consumes  
  34.         } catch (ParseException e) {  
  35.             output( "Parse response entity exception!", e);  
  36.         } catch (IOException e) {  
  37.             output( "java IO exception while parsing response entity!", e);  
  38.         } catch (UnsupportedOperationException e) {  
  39.             params = "entity类型:" + entity.getClass();  
  40.             output( "Unsupported entity type!", e);  
  41.         }  
  42.     }  
  43.     info.put("params", params);  
  44.     return info;  
  45. }  

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325230870&siteId=291194637