代码改变世界

接口自动化框架(java)--3.get,delete请求,Excel管理多种请求类型

2019-03-29 23:02  清风软件测试开发  阅读(610)  评论(0编辑  收藏  举报

 这套框架的报告是自己封装的

每种请求类型放入不同的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