美文网首页自动化测试
一、rest-assured接口测试框架

一、rest-assured接口测试框架

作者: by小杰 | 来源:发表于2017-06-26 17:48 被阅读400次

    使用rest-assrued框架做接口自动化测试,框架的发掘来自于一次沙龙的学习,愿意更深入学习的同学可参考:https://testerhome.com/topics/7060
    一、常用方法的类

    //常用的post、get、when、given等方法所在类
    io.restassured.RestAssured.*
    io.restassured.matcher.RestAssuredMatchers.*
    //常用的equalTo、hasItems等断言方法所在类
    org.hamcrest.Matchers.*
    //如果要使用Json Schema Validation对返回的response结果进行结果判断,需要导入该类
    io.restassured.module.jsv.JsonSchemaValidator.*
    #另外,官方文档说明:为了提高使用rest-assured的效率,推荐静态导入以上方法
    

    二、应用场景
    首先,做了一些接口之后发现有很多同类型的请求如:get请求,其基本语法都是一致的,只不过在请求的时候传递的参数有所不同,所以在工程中新建了一个特定的工具类,将所有的请求都写到该类中,这样只需要在该类中区分不同请求方式、不同请求参数的请求即可,在测试主函数中,我们只需要调用该类中的相关应方法,传递URL、参数及信cookies等信息。
    下面是我工具类的一些方法,或存在不完善的地方也只是初步学习,谅解:

    public class JJApiTools {
    
        /**类型一:
         * 请求参数为json数据结构的post请求
         */
        public static Response post(String apiPath, String jsonData){
    
            Response response = given().
                    contentType("application/json;charset=UTF-8").
                    body(jsonData).
                    when().log().all().post(apiPath.trim());
    
            return response;
        }
    
    
        /**类型二:
         * 请求无参数结构的post请求
         */
        public static Response post(String apiPath){
    
            Response response = given().contentType("application/json; charset=UTF-8").
                    headers("headers1", "value1", "header2", "value2").
                    cookies("cookies1","value1", "cookies2", "value2").
                    expect().statusCode(200).
                    when().post(apiPath.trim());
    
            return response;
    
        }
    
        /**类型三:
         * 请求有多个参数结构的post请求
         */
        //在请求URL中如果有很多个参数,可以用map进行传递,类似于字典
       public static Response mapPost(String apiPath, Map map){
    
            Response response =  given().params(map).
                    expect().statusCode(200).
                    when().post(apiPath);
    
            return response;
        }
    
        /**类型三:
         * 含有cookie的post请求
         */
        public static  Response cookiesPost(String apiPath, String cookies){
            System.out.println("Cookie = "+cookies);
    
            Response response = given().contentType("application/json; charset=UTF-8").
                    cookies("web-session", cookies).
                    expect().statusCode(200).
                    when().
                    //log().all().
                    post(apiPath.trim());
    
            return response;
        }
    
    
        /**类型三:
         * get请求
         */
        public static Response get(String apiPath) {
    
            Response response = given().contentType("application/json; charset=UTF-8").
                    //headers("headers1", "value1", "headers2", "values2").
                    //cookies("cookies1", "values1", "cookies2", "values2").
                    expect().statusCode(200).
                    when().get(apiPath.trim());
            System.out.println("_________________________________________");
    
            return response;
    
    
        }
    
    
        public static Response get(String apiPath, String cookie, Headers headers) {
    
            System.out.println("Cookie = "+cookie);
    
            Response response = given().contentType("application/json; charset=UTF-8").
                    headers("Headers", headers).
                    cookies("web-session", cookie).
                    when().log().all().get(apiPath.trim());
    
            //given().contentType("application/json").when().get("/xxx").then().body();
    
            return response;
    
    
        }
        public static Response get(String apiPath, String cookie) {
    
            System.out.println("Cookie = "+cookie);
    
            Response response = given().contentType("application/json; charset=UTF-8").
                    cookies("web-session", cookie).
                    expect().statusCode(200).
                    when().get(apiPath.trim());
    
            //given().contentType("application/json").when().get("/xxx").then().body();
    
            return response;
    
    
        }
        public static Response get(String apiPath, Map map, String cookie){
    
            Response response = given().
                    contentType("application/json; charset=UTF-8").
                    params(map).expect().statusCode(200).
                    when().get(apiPath.trim());
    
            return response;
        }
    }
    
    #对上面方法中的函数作用记录一下:
    log().all() :会在控制台打印request的相关信息,出现错误时可以用来做调试
    trim():情况URL中的空格
    expect().statusCode(200):断言返回的响应码是不是200
    

    以上不同情况的请求,可以相互组合使用,因为目前我的接口需求也就这些,我也就考虑到了这些,如果有更多的需求欢迎留言讨论!

    相关文章

      网友评论

        本文标题:一、rest-assured接口测试框架

        本文链接:https://www.haomeiwen.com/subject/oosjcxtx.html