java接口自動化測試框架及斷言詳解

這篇文章主要介紹了java接口自動化測試框架及斷言詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

上篇文章,我們介紹了Get方法的設計過程和測試結果,現在我們需要對前面代碼進行重構和修改,本篇需要完成以下目標。

  • 1)重構Get方法
  • 2)如何進行JSON解析
  • 3)使用TestNG方法進行測試斷言

1.重構Get方法

在前面文章,說過,之前寫的Get方法比較繁瑣,不光寫了如何進行Get請求,還寫了獲取http響應狀態碼和JSON轉換。現在我們需要抽取出來,設計Get請求方法,就只幹一件事情,那就是如何發送get請求,其他的不要管。

我們知道,請求之後會返回一個HTTP的響應對象,所以,我們把get方法的返回值類型改成了響應對象,並帶上返回語句,重構代碼之後,get方法代碼如下。

package com.qa.restclient;
import java.io.IOException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
public class RestClient {
 //1. Get 請求方法
 public CloseableHttpResponse get(String url) throwsClientProtocolException, IOException {
  //創建一個可關閉的HttpClient對象
  CloseableHttpClienthttpclient = HttpClients.createDefault();
  //創建一個HttpGet的請求對象
  HttpGethttpget = newHttpGet(url);
  //執行請求,相當於postman上點擊發送按鈕,然後賦值給HttpResponse對象接收
  CloseableHttpResponsehttpResponse = httpclient.execute(httpget);
  return httpResponse;
 }
}

由於我們不想在代碼裏寫死例如像HTTP響應狀態碼200這樣的硬編碼,所以,這裏我們在TestBase.java裏把狀態碼給用常量寫出來,方便每一個TestNG測試用例去調用去斷言。

package com.qa.base;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
public class TestBase {
   public Properties prop;
   public int RESPNSE_STATUS_CODE_200 = 200;
   public int RESPNSE_STATUS_CODE_201 = 201;
   public int RESPNSE_STATUS_CODE_404 = 404;
   public int RESPNSE_STATUS_CODE_500 = 500;
   //寫一個構造函數
   public TestBase() {
     try{
       prop= new Properties();
       FileInputStreamfis = new FileInputStream(System.getProperty("user.dir")+
"/src/main/java/com/qa/config/config.properties");
       prop.load(fis);
     }catch (FileNotFoundException e) {
       e.printStackTrace();
     }catch (IOException e) {
       e.printStackTrace();
     }
   }
}

現在我們的測試類代碼修改之後如下。

package com.qa.tests;
import java.io.IOException;
importorg.apache.http.client.ClientProtocolException;
importorg.apache.http.client.methods.CloseableHttpResponse;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import com.qa.base.TestBase;
import com.qa.restclient.RestClient;
public class GetApiTest extends TestBase{
   TestBase testBase;
   String host;
   String url;
   RestClient restClient;
   CloseableHttpResponse closeableHttpResponse;
   @BeforeClass
   public void setUp() {
    testBase = new TestBase();
    host = prop.getProperty("HOST");
    url = host + "/api/users";   
   }
   @Test
   public void getAPITest() throws ClientProtocolException, IOException {
     restClient = new RestClient();
     closeableHttpResponse= restClient.get(url);
     //斷言狀態碼是不是200
     int statusCode = closeableHttpResponse.getStatusLine().getStatusCode();
     Assert.assertEquals(statusCode,RESPNSE_STATUS_CODE_200, "response status code is not 200");
   }
}

測試運行通過,沒毛病。

2.寫一個JSON解析的工具類

在上面部分,我們只是寫了執行Get請求和狀態碼是否200的斷言。接下來,我們需要寫有一個JSON解析工具類,這樣就方便我們去json內容的斷言。

下面這個JSON數據截圖

上面是一個標準的json的響應內容截圖,第一個紅圈”per_page”是一個json對象,我們可以根據”per_page”來找到對應值是3,而第二個紅圈“data”是一個JSON數組,而不是對象,不能直接去拿到裏面值,需要遍歷數組。

下面,我們寫一個JSON解析的工具方法類,如果是像第一個紅圈的JSON對象,我們直接返回對應的值,如果是需要解析類似data數組裏面的json對象的值,這裏我們構造方法默認解析數組第一個元素的內容。

在src/main/java下新建一個包:com.qa.util,然後在新包下創建一個TestUtil.java類。

package com.qa.util;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
public class TestUtil {
 /**
 *
 * @param responseJson ,這個變量是拿到響應字符串通過json轉換成json對象
 * @param jpath,這個jpath指的是用戶想要查詢json對象的值的路徑寫法
 * jpath寫法舉例:1) per_page 2)data[1]/first_name ,data是一個json數組,[1]表示索引
 * /first_name 表示data數組下某一個元素下的json對象的名稱爲first_name
 * @return,返回first_name這個json對象名稱對應的值
 */
 //1 json解析方法
 public static String getValueByJPath(JSONObject responseJson, String jpath){
  Objectobj = responseJson;
  for(String s : jpath.split("/")) {
  if(!s.isEmpty()) {
   if(!(s.contains("[") || s.contains("]"))) {
    obj = ((JSONObject) obj).get(s);
   }else if(s.contains("[") || s.contains("]")) {
    obj =((JSONArray)((JSONObject)obj).get(s.split("\\[")[0])).get(Integer.parseInt(s.split("\\[")[1].replaceAll("]", "")));
   }
  }
  }
  return obj.toString();
 }
}

簡單解釋下上面的代碼,主要是查詢兩種json對象的的值,第一種最簡單的,這個json對象在整個json串的第一層,例如上面截圖中的per_page,這個per_page就是通過jpath這個參數傳入,返回的結果就是3. 第二種jpath的查詢,例如我想查詢data下第一個用戶信息裏面的first_name的值,這個時候jpath的寫法就是data[0]/first_name,查詢結果應該是Eve。

3.TestNG測試用例

下面,我們TestNG測試用例代碼如下

package com.qa.tests;
import java.io.IOException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.util.EntityUtils;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.qa.base.TestBase;
import com.qa.restclient.RestClient;
import com.qa.util.TestUtil;
public class GetApiTest extends TestBase{
	TestBase testBase;
	String host;
	String url;
	RestClient restClient;
	CloseableHttpResponse closeableHttpResponse;
	@BeforeClass
	public void setUp() {
		testBase = new TestBase();
		host = prop.getProperty("HOST");
		url = host + "/api/users?page=2";
	}
	@Test
	public void getAPITest() throws ClientProtocolException, IOException {
		restClient = new RestClient();
		closeableHttpResponse = restClient.get(url);
		//斷言狀態碼是不是200
		int statusCode = closeableHttpResponse.getStatusLine().getStatusCode();
		Assert.assertEquals(statusCode, RESPNSE_STATUS_CODE_200, "response status code is not 200");
		//把響應內容存儲在字符串對象 
  String responseString = EntityUtils.toString(closeableHttpResponse.getEntity(),"UTF-8"); 
  //創建Json對象,把上面字符串序列化成Json對象 
  JSONObject responseJson = JSON.parseObject(responseString); 
  //System.out.println("respon json from API-->" + responseJson); 
  //json內容解析
  String s = TestUtil.getValueByJPath(responseJson,"data[0]/first_name");
  System.out.println(s);
	}	
}

運行測試結果:

[RemoteTestNG] detected TestNGversion 6.14.3

Eve

PASSED: getAPITest

你還可以多寫幾個jpath來測試這個json解析工具類。

String s = TestUtil.getValueByJPath(responseJson,"data[1]/id");
String s = TestUtil.getValueByJPath(responseJson,"per_page");

4.TestNG自帶的測試斷言方法

這裏簡單提一下TestNG的斷言方法,我們一般測試都需要寫斷言的代碼,否則這樣的單元測試代碼就沒有意義。下面,我在statusCode和json解析的first_name進行斷言。

package com.qa.tests;
import java.io.IOException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.util.EntityUtils;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.qa.base.TestBase;
import com.qa.restclient.RestClient;
import com.qa.util.TestUtil;
public class GetApiTest extends TestBase{
	TestBase testBase;
	String host;
	String url;
	RestClient restClient;
	CloseableHttpResponse closeableHttpResponse;
	@BeforeClass
	public void setUp() {
		testBase = new TestBase();
		host = prop.getProperty("HOST");
		url = host + "/api/users?page=2";
	}
	@Test
	public void getAPITest() throws ClientProtocolException, IOException {
		restClient = new RestClient();
		closeableHttpResponse = restClient.get(url);
		//斷言狀態碼是不是200
		int statusCode = closeableHttpResponse.getStatusLine().getStatusCode();
		Assert.assertEquals(statusCode, RESPNSE_STATUS_CODE_200, "response status code is not 200");
		//把響應內容存儲在字符串對象 
  String responseString = EntityUtils.toString(closeableHttpResponse.getEntity(),"UTF-8"); 
  //創建Json對象,把上面字符串序列化成Json對象 
  JSONObject responseJson = JSON.parseObject(responseString); 
  //System.out.println("respon json from API-->" + responseJson); 
  //json內容解析
  String s = TestUtil.getValueByJPath(responseJson,"data[0]/first_name");
  System.out.println(s);
  Assert.assertEquals(s, "Eve","first name is not Eve");
	}
}

經常使用的測試斷言:

Assert.assertEquals(“現實結果”, "期待結果","斷言失敗時候打印日誌消息");

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持神馬文庫。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章