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.
- public void case_shequ_topic_cattlist() {
- String apiName = "shequ_topic_cattlist";
- mark = getMark(); // get timestamp as test mark
- List<Map<String, String>> date = LocalMySql.getInstance().getCaseFromMySql(apiName); // database read case
- for ( int i = 0; i < date. size(); i++) { // traverse execution cases
- int result = 0;
- String expect_value1 = "", actual_value1 = "", expect_value2 = "", actual_value2 = "", params = "",
- actual_key1 = "", actual_key2 = "", actual_key3 = "", expect_value3 = "", actual_value3 = "";
- Map<String, String> use = date.get(i); // get a single use case
- String case_id = use.get("case_id");
- /*
- * 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
- */
- expect_value1 = use.get( "verify_value1"); // Get the expected value of the verification point
- expect_value2 = use.get( "verify_value2"); // Get the expected value of the verification point
- expect_value3 = use.get( "verify_value3"); // Get the expected value of the verification point
- actual_key1 = use.get( "verify_key1"); //Get the checkpoint key
- actual_key2 = use.get( "verify_key2"); //Get the checkpoint key
- actual_key3 = use.get( "verify_key3"); //Get the checkpoint key
- params = use.toString(); // record incoming parameters
- JSONObject response = shequ_topic_cattlist(use); // get the corresponding result
- actual_value1 = getDateValue(response, actual_key1); //Get the actual value of the verification point
- actual_value2 = getDateValue(response, actual_key2); //Get the actual value of the verification point
- actual_value3 = getDateContains(response, actual_key3, expect_value3); //Get the actual value of the verification point, this is the containment verification
- result = getResult(expect_value1, actual_value1, expect_value2, actual_value2) ? 1 : 2; // Get the test result, 1 is passed, 2 is failed
- LocalMySql.getInstance().saveApiTestResult(case_id, mark, result, apiName, expect_value1, actual_value1,
- expect_value2, actual_value2, expect_value3, actual_value3, params); // write to database
- }
- LocalMySql.getInstance().addApiTestResult(apiName, mark, test_mark); //Statistics of all test results in this run
- }
Method encapsulation for getting the response:
- public JSONObject shequ_topic_cattlist(Map<String, String> apiCase) {
- String uri = host + "/shequ/topic/cattlist";
- deleteMap(apiCase);
- JSONObject jsonObject = new JSONObject(apiCase);
- HttpGet httpGet = getHttpGet (uri, jsonObject);
- JSONObject response = getHttpResponseEntityByJson(httpClient, httpGet);
- return response;
- }
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.
- /**
- * Encapsulate the method of obtaining various information of the request
- *
- * @param httpRequestBase
- * incoming request object
- * @return returns a map, including api_name, host_name, type, method, params
- */
- public Map<String, String> getRequestInfo(HttpRequestBase request) {
- Map<String, String> info = new HashMap<>(); // Create a new map to save information
- String method = request.getMethod();// 获取method
- info.put("method", method);
- String uri = request.getURI().toString();// 获取uri
- info.put("uri", uri);
- String url = uri;
- if (uri.contains( "?")) { // Get the url, if it is a get request, intercept it first
- url = uri.substring(0, uri.indexOf("?"));
- }
- String one = url.substring(url.indexOf( "//") + 2);// delete http://
- String api_name = one.substring(one.indexOf( "/")); // Get the interface name
- info.put("api_name", api_name);
- String host_name = one.substring( 0, one.indexOf( "/")); // Get the host address
- info.put("host_name", host_name);
- String type = url.substring( 0, url.indexOf( "//") - 1);// Get the protocol type
- info.put("type", type);
- String params = null; // parameters
- if (method.equals("GET")) {
- params = uri.substring(uri.indexOf("?") + 1, uri.length());
- } else if (method.equals("POST")) {
- HttpPost httpPost = (HttpPost) request; // Force the httppost request
- HttpEntity entity = httpPost.getEntity(); // get entity
- try {
- params = EntityUtils.toString(entity); // resolve entity
- EntityUtils.consume(entity); // Make sure the entity consumes
- } catch (ParseException e) {
- output( "Parse response entity exception!", e);
- } catch (IOException e) {
- output( "java IO exception while parsing response entity!", e);
- } catch (UnsupportedOperationException e) {
- params = "entity类型:" + entity.getClass();
- output( "Unsupported entity type!", e);
- }
- }
- info.put("params", params);
- return info;
- }