1. 程式人生 > >接口自動化框架(java)--3.get,delete請求,Excel管理多種請求類型

接口自動化框架(java)--3.get,delete請求,Excel管理多種請求類型

密碼 圖片 ide point 文件 return als tca print

這套框架的報告是自己封裝的

每種請求類型放入不同的sheet中,就可以避免新建太多的excel去做數據驅動。

XSSFSheet類提供了一個讀取sheet的方法,getSheetAt(int),通過下標去訪問想要的sheet

1.Excel,添加兩個sheet頁改名成get , delete,代表這兩種類型的接口

技術分享圖片

2. 在用例的dataProvider中將這兩個sheet作兩個方法分別讀取。再傳入對應的test中

 1 package com.qa.tests;
 2  
 3 import com.alibaba.fastjson.JSON;
 4 import
com.qa.base.TestBase; 5 import com.qa.Parameters.postParameters; 6 import com.qa.restclient.RestClient; 7 import com.qa.util.TestUtil; 8 import org.apache.http.client.methods.CloseableHttpResponse; 9 import org.testng.Assert; 10 import org.testng.annotations.BeforeClass; 11 import org.testng.annotations.DataProvider;
12 import org.testng.annotations.Test; 13 14 import java.io.IOException; 15 import java.util.HashMap; 16 17 import static com.qa.util.TestUtil.dtt; 18 19 public class testCase1 extends TestBase { 20 TestBase testBase; 21 RestClient restClient; 22 CloseableHttpResponse closeableHttpResponse;
23 //host根url 24 String host; 25 //Excel路徑 26 String testCaseExcel; 27 //header 28 HashMap<String ,String> postHeader = new HashMap<String, String>(); 29 @BeforeClass 30 public void setUp(){ 31 testBase = new TestBase(); 32 restClient = new RestClient(); 33 postHeader.put("Content-Type","application/json"); 34 //載入配置文件,接口endpoint 35 host = prop.getProperty("Host"); 36 //載入配置文件,post接口參數 37 testCaseExcel=prop.getProperty("testCase1data"); 38 39 } 40 41 @DataProvider(name = "postData") 42 public Object[][] post() throws IOException { 43 //post類型接口 44 return dtt(testCaseExcel,0); 45 46 } 47 48 @DataProvider(name = "get") 49 public Object[][] get() throws IOException{ 50 //get類型接口 51 return dtt(testCaseExcel,1); 52 } 53 54 @DataProvider(name = "delete") 55 public Object[][] delete() throws IOException{ 56 //delete類型接口 57 return dtt(testCaseExcel,2); 58 } 59 @Test(dataProvider = "postData") 60 public void login(String loginUrl,String username, String passWord) throws Exception { 61 //使用構造函數將傳入的用戶名密碼初始化成登錄請求參數 62 postParameters loginParameters = new postParameters(username,passWord); 63 //將登錄請求對象序列化成json對象 64 String userJsonString = JSON.toJSONString(loginParameters); 65 //發送登錄請求 66 closeableHttpResponse = restClient.postApi(host+loginUrl,userJsonString,postHeader); 67 //從返回結果中獲取狀態碼 68 int statusCode = TestUtil.getStatusCode(closeableHttpResponse); 69 Assert.assertEquals(statusCode,200); 70 71 } 72 73 @Test(dataProvider = "get") 74 public void getApi(String url) throws Exception{ 75 closeableHttpResponse = restClient.getApi(host+ url); 76 int statusCode = TestUtil.getStatusCode(closeableHttpResponse); 77 Assert.assertEquals(statusCode,200); 78 } 79 80 @Test(dataProvider = "delete") 81 public void deleteApi(String url) throws Exception{ 82 System.out.println(url); 83 closeableHttpResponse = restClient.deleteApi(url); 84 int statusCode = TestUtil.getStatusCode(closeableHttpResponse); 85 Assert.assertEquals(statusCode,204); 86 } 87 88 @BeforeClass 89 public void endTest(){ 90 System.out.print("測試結束"); 91 } 92 93 }

技術分享圖片

原文地址https://blog.csdn.net/qq_34693151/article/details/81875790

接口自動化框架(java)--3.get,delete請求,Excel管理多種請求類型