Java教程

Java接口自动化框架系列04:接口自动化demo例子

本文主要是介绍Java接口自动化框架系列04:接口自动化demo例子,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

Java接口自动化框架系列04:接口自动化demo例子

1、接口的入参

Excel方式(调用封装类BaseRequest)

参考:apiData_all.xlsx

入参参数说明:

序号:1    
接口名称:add_role  
接口请求方法:POST   
用例描述:新增角色
​
接口地址:/uim/v1/role/add
​
接口参数:{
    "permissionIdList": [
        "0000",
        "1000100000",
        "1000101000",
        "1000200000",
        "1000201000"
    ],
    "roleName": "${roleName}",
    "roleDescribe": "JAVA AUTO INSERT角色描述"
}
预期响应结果:0
    
其中:${roleName} 是变量,需要在代码定义或者是上一个接口的返回值

2、接口参数直接在代码里

2.1、GET方法(直接调用工具类HttpsClientUtils)

     String reqUrl ="https://10.1.1.181/uim/v1/role/delete";
     Map<String, String> map_delete = new HashMap<>();
     map_delete.put("id", "1234567890212222");
     Map<String, String> headers=new HashMap<>();
     headers.put("Content-Type","application/json;charset=UTF-8");
     headers.put("Authorization",token);
     //发送请求
     HttpsClientUtils httpUtils = new HttpsClientUtils();
     HttpClientResult httpResult = httpUtils.doGet(reqUrl, headers, map_delete);

2.2、POST方法(直接调用工具类HttpsClientUtils)

    String loginUrl = "https://10.1.1.181/uim/v1/login";
    String jsonStr = "{\"username\":\"YWRtaW4=\",\"password\":\"bGlhblNoaTIwMjA=\"}";
    //请求头
    Map<String, String> headers=new HashMap<>();
    headers.put("Content-Type","application/json;charset=UTF-8");
    //发送请求
    HttpsClientUtils httpUtils = new HttpsClientUtils();
    HttpClientResult result=httpUtils.doPost(loginUrl,headers, jsonStr);

3、测试类代码示例

@Feature("用户权限管理")
@Listeners({com.ciphergateway.asserts.AssertionListener.class})
public class UserManage extends BaseRequest{
    //获取excel测试用例
    private String sheet1="用户权限接口用例";
    private List<ApiDataBean> listExcel;
    //存入excel,存储结果(测试结果相关)
    private List<ApiDataBean> dataList_w=new ArrayList<>();
    //设置公共参数存取相关
    private ParamUtil paramUtil=new ParamUtil();
    private Map<String, String> paramMap=new HashMap<>();
    //httpClient请求相关
    private String host;
    private String token;
    //这里主要清除数据使用header
    private Map<String, String> headers;

    //请求参数值相关
    private  String roleName;
    private  String longRoleName;
    private long startTime;
    private long endTime;

    //最后需要删除所有新增的数据
    private List<String> deleteList_role=new ArrayList<>();


    @BeforeTest
    public void setup() throws Exception {
        String st=Common.getTime();
        startTime = System.currentTimeMillis();    //获取开始时间
        System.out.println("-->setup(),用户权限管理的接口自动化测试,StartTime:"+st);
        //获取Excel数据
        String excelFile="src/data/testcase/apiData_all.xlsx";
        listExcel=ExcelUtil.readExcel(excelFile,sheet1);
        //设置公共参数存取
        //角色管理:初始化参数值(roleName,roleName_new)
        roleName = "roleName_"+ RandomUtil.getRandomStringNum(10);
        longRoleName="longRoleName_"+RandomUtil.getRandomString(20);
        String roleName_new = "roleName_new_"+ RandomUtil.getRandomStringNum(10);
        paramMap.put("roleName",roleName);
        paramMap.put("longRoleName",longRoleName);
        paramMap.put("roleName_new",roleName_new);

        paramUtil.setSaveDatas(paramMap);

        try {
            //调用父类的getHost()
            host = getHost();
            System.out.println("接口自动化测试url地址:"+host);
        } catch (Exception e) {
            e.printStackTrace();
        }

        //先登录获取token
        Login lo=new Login();
        token=lo.getToken();

        try {
            //调用父类的getHeader()
            headers = getHeader(token);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    @Story("角色管理")
    @Description("Description注解:验证新增角色接口")
    @Test(description = "add_role")
    public void a1_addRole() throws Exception {
        for (ApiDataBean data : listExcel) {
            if (data.getApiName().trim().equals("add_role")) {
                String resultContent=postRequest(token,data, paramUtil,dataList_w);
                String code= JsonUtil.gsonParserString(resultContent,"code");
                String expectData = data.getExpectData();
                //Assert.assertEquals(code, expectData,"新增角色接口");   //assert原有的断言 ,默认的断言,失败后,后面用例不再执行
                Assertion.verifyEquals(code, expectData, data.getDesc());
                //Allure展示测试步骤
                Allure.step("请求测试数据:接口名称" + data.getApiName() + "," + data.getDesc() + "," + data.getMethod() + "," + data.getUrl() + "," + data.getParam() + " ,预期结果:code:" + expectData + ",执行返回结果:" + resultContent);
            }
        }
    }
    @AfterTest
    public void teardown() throws Exception {
        System.out.println("<--teardown()-->");
        //新增的角色历史数据进行删除(可直接调用httpUtils)
        for (String item : deleteList_role) {
            String reqUrl = host + "/uim/v1/role/delete";
            Map<String, String> map_delete = new HashMap<>();
            map_delete.put("id", item);
            //发送请求
            HttpsClientUtils httpUtils = new HttpsClientUtils();
            HttpClientResult httpResult = httpUtils.doGet(reqUrl, headers, map_delete);
            System.out.println(item + ",角色新增的多余数据删除成功," + httpResult.getContent());
            log.info(item + ",角色新增的多余数据删除成功," + httpResult.getContent());
            Allure.step(item + ",角色新增的多余数据删除成功," + httpResult.getContent());
        }

        //调用excel方法写入数据
        String excelFile_result="src/data/testcase/apiData_result.xlsx";
        ExcelUtil.writeExcel(excelFile_result,sheet1,dataList_w);


        //清空数据,包括listExcel,paramMap等
        listExcel.clear();
        paramMap.clear();
        dataList_w.clear();
        deleteList_user.clear();
        deleteList_org.clear();
        deleteList_role.clear();

        String et=Common.getTime();
        endTime = System.currentTimeMillis();    //获取结束时间
        System.out.println("===========用户权限管理的接口自动化测试,EndTime:"+et+"===============");
        log.info("===========用户权限管理的接口自动化测试,EndTime:"+et+"===============");
        Allure.step("===========用户权限管理的接口自动化测试,EndTime:"+et+"===============");
        System.out.println("测试用例运行时间:" + (endTime - startTime) + "ms");    //输出程序运行时间

    }

}

4.TestNg配置

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<!-- Suite代表一个测试套件,1个suite可包含多个Test -->
<suite name="casbTests">
    <!-- Test代表1个测试,里面可包含多个测试class -->
    <test name="CasbServer" preserve-order="true" >
        <classes>
            <class name="com.ciphergateway.testcase.UserManage"/>
        </classes>
    </test>
    <listeners>
        <listener class-name="com.ciphergateway.asserts.AssertionListener"></listener>
    </listeners>
</suite>

这篇关于Java接口自动化框架系列04:接口自动化demo例子的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!