接口測試_基礎_restAssured

實例GitHub:https://github.com/reese0329/rest_assured

 

dubbo協議

rpc

service

params

return

 

協議的共同點

  • protocal

http

soap

dubbo

  • target

host port serviceName

  • request

結構化

schema

  • response

結構化

schema

 

使用代理分析常見的協議

啓動代理工具開放代理端口

設置app或web瀏覽器的代理地址

如果是https需要導入證書

執行測試行爲

從代理工具中分析接口數據

11:00

burpsuite

https://portswigger.net/burp/

演練 

Android 4.4 

 

 

 

 

32‘

使用自定義代理解析特殊協議

流量轉發

協議解包

協議封包

原理是tcp proxy或者http proxy

參考工具 em-proxy nc

 

接口測試的質量目標

  • 質量保證維度

功能正常。保持新老版本的兼容

性能正常。單詞請求的響應跟總體qps相關

變更檢測。字段的缺失,字段的類型變更

  • 質量監控體系

構建接口層的快速問題的質量保證體系

構建接口監控體系

 

 

34:50

接口測試用例編寫

接口測試流程

接口的分析

接口分析

接口測試框架

測試用例維護

持續集成

質量監控

 

接口測試框架選擇

jmeter

soapUI

Robotframework

swagger

RestAssured

 

37‘

RestAssured

簡約的接口測試dsl

支持xml 寄送的結構化解析

支持xpath jsonpath gpath 等多種解析方式

對spring的支持比較全面

 

根據自己的原因提子自己封裝

需要滿足接口測試的幾大功能點

 

文檔(最好讀一遍,對接口測試有好處)

https://github.com/rest-assured/rest-assured

 

43'

創建maven,在pom.xml添加依賴

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>demoTest</groupId>
    <artifactId>demo</artifactId>
    <version>1.0-SNAPSHOT</version>



    <dependencies>
    <dependency>
        <groupId>io.rest-assured</groupId>
        <artifactId>rest-assured</artifactId>
        <version>3.0.3</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>io.rest-assured</groupId>
        <artifactId>json-schema-validator</artifactId>
        <version>3.0.1</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>io.rest-assured</groupId>
        <artifactId>spring-mock-mvc</artifactId>
        <version>3.0.1</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.2</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>io.appium</groupId>
        <artifactId>java-client</artifactId>
        <version>5.0.4</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.codehaus.groovy</groupId>
        <artifactId>groovy-all</artifactId>
        <version>1.8.1</version>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>org.hamcrest</groupId>
        <artifactId>groovy-all</artifactId>
        <version>1.8.1</version>
        <scope>provided</scope>
    </dependency>
    </dependencies>

</project>

 

 

reimport 依賴

 

創建測試目錄

 

 

get請求

package restassured;

import io.restassured.config.HttpClientConfig;
import io.restassured.response.Response;
import org.apache.http.client.params.ClientPNames;
import org.apache.http.params.CoreConnectionPNames;
import org.junit.*;

import static io.restassured.RestAssured.*;
import static io.restassured.matcher.RestAssuredMatchers.*;
import static org.hamcrest.Matchers.*;

public class Demo {
    @Test
    public void baidu() {
        given()
                .log().all()
                .queryParam("wd", "mp3")
                .when()
                .get("http://www.baidu.com/s")
                .then()
                .log().all()
                .statusCode(200)
                .body("html.head.title", equalTo("mp3_百度搜索"));
    }

}

 

given 參數信息

when觸發條件 get/post請求

then斷言

    .body 對返回內容做斷言

   .statusCode 對返回狀態做斷言  (返回內容爲html,則使用html語法)

 

返回結果

 

 

錯誤的斷言

package restassured;

import io.restassured.config.HttpClientConfig;
import io.restassured.response.Response;
import org.apache.http.client.params.ClientPNames;
import org.apache.http.params.CoreConnectionPNames;
import org.junit.*;

import static io.restassured.RestAssured.*;
import static io.restassured.matcher.RestAssuredMatchers.*;
import static org.hamcrest.Matchers.*;

public class Demo {
    @Test
    public void baidu() {
        given()
                .log().all()
                .queryParam("wd", "mp3")
                .when()
                .get("http://www.baidu.com/s")
                .then()
                .log().all()
                .statusCode(200)
                .body("html.head.title", equalTo("mp4_百度搜索"));
    }

//test
}

返回

 

 

 

 

50‘

用例編寫(演練)

  • 創建maven項目
  • 添加依賴 restassured + junit/testng
  • 編寫用例
  • 添加斷言
  • 調試

 

Post請求實例

藉助代理抓包工具驗證接口請求是否可以成功發送

burp suite 右鍵 copy as curl commands 

粘貼執行

驗證接口可以重複執行

 

 

可執行無報錯,但是有問題,貌似沒有真正發送成功

package restassured;

import io.restassured.config.HttpClientConfig;
import io.restassured.response.Response;
import org.apache.http.client.params.ClientPNames;
import org.apache.http.params.CoreConnectionPNames;
import org.junit.*;

import static io.restassured.RestAssured.*;
import static io.restassured.matcher.RestAssuredMatchers.*;
import static org.hamcrest.Matchers.*;

public class Demo {

    @Test
    //post請求
    public void testerhome() {
        given().log().all()
                .header("Content-Type", "application/x-www-form-urlencoded")
                .header("Cookie", "ll=\"108288\"; bid=ufm8svqInPI; __utmc=30149280; douban-fav-remind=1; gr_user_id=4171796e-842c-4c4a-90a0-26e6738a48d0; _vwo_uuid_v2=DCD0C06E626E1D6DFE13B60EAD6341A39|d72f1edb0d54ead74d312fdccc346905; __yadk_uid=D5htNM4K5N9sqGUCr3fbvz5kAD82ZqNh; push_noty_num=0; push_doumail_num=0; __utmv=30149280.4603; __utmz=30149280.1554109383.4.2.utmcsr=baidu|utmccn=(organic)|utmcmd=organic; dbcl2=\"46030598:5+UMwWFA/7I\"; ck=rwMr; ap_v=0,6.0; ct=y; _pk_ref.100001.8cb4=%5B%22%22%2C%22%22%2C1554692809%2C%22https%3A%2F%2Faccounts.douban.com%2Fpassport%2Flogin_popup%3Flogin_source%3Danony%22%5D; _pk_id.100001.8cb4=159c3a6479396543.1553674557.11.1554692809.1554687227.; _pk_ses.100001.8cb4=*; __utma=30149280.526818714.1553674558.1554690313.1554692810.14; __utmt=1; __utmb=30149280.2.10.1554692810")
                .formParam("ck", "aIY0")
                .formParam("comment", "test19")
                .when()
                .log().all()
                .post("https://www.douban.com/");
//                .then()
//                .log().all()
//                .body("success", equalTo(true));
    }

    }

 

 

 

1‘04’

數據驅動

https://github.com/junit-team/junit4/wiki/Parameterized-tests

有問題,大概這個意思

import static org.junit.Assert.assertEquals;

import java.util.Arrays;
import java.util.Collection;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameter;
import org.junit.runners.Parameterized.Parameters;

@RunWith(Parameterized.class)
public class FibonacciTest {
    @Parameters
    public static Collection<Object[]> data() {
        return Arrays.asList(new Object[][] {
                 { 0, 0 }, { 1, 1 }, { 2, 1 }, { 3, 2 }, { 4, 3 }, { 5, 5 }, { 6, 8 }  
           });
    }

    @Parameter // first data value (0) is default
    public /* NOT private */ int fInput;

    @Parameter(1)
    public /* NOT private */ int fExpected;

    @Test
    public void test() {
        assertEquals(fExpected, Fibonacci.compute(fInput));
    }
}

public class Fibonacci {
    ...
}
package restassured;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import java.util.Arrays;
import java.util.Collection;

import static io.restassured.RestAssured.given;
import static org.hamcrest.CoreMatchers.equalTo;

@RunWith(Parameterized.class)
public class Demo {
    @Parameterized.Parameters
    public static Collection<Object[]> data() {
        return Arrays.asList(new Object[][]{
                {0, 0}, {1, 1}, {2, 1}, {3, 2}, {4, 3}, {5, 5}, {6, 8}
        });
    }

    //    @Parameterized.Parameter
    //導入一維數組
    public   /* NOT private */ int fInput;

    //    @Parameterized.Parameter
    //二維數組
    public   /* NOT private */ int fExpected;

    @Test
    //get 請求
    public void baidu() {
        given()
                .log().all()
                .queryParam("wd", fInput)
                .when()
                .get("http://www.baidu.com/s")
                .then()
                .log().all()
                .statusCode(200)
                .body("html.head.title", equalTo(fExpected));
    }
}

 

 

 

重命名參數

 @Parameterized.Parameters(name ="{index}:baidu search wd={0} expected={1}")

package restassured;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import org.junit.runners.Parameterized.Parameter;

import java.util.Arrays;
import java.util.Collection;

import static io.restassured.RestAssured.given;
import static org.hamcrest.CoreMatchers.equalTo;

@RunWith(Parameterized.class)
public class Demo {
    @Parameterized.Parameters(name ="{index}:baidu search wd={0} expected={1}")
    public static Collection<Object[]> data() {
        return Arrays.asList(new Object[][]{
                {0, 0}, {1, 1}, {2, 1}, {3, 2}, {4, 3}, {5, 5}, {6, 8}
        });
    }

    //    @Parameterized.Parameter
    //導入一維數組
    public   /* NOT private */ int fInput;

    //    @Parameterized.Parameter
    //二維數組
    public   /* NOT private */ int fExpected;

    @Test
    //get 請求
    public void baidu() {
        given()
                .log().all()
                .queryParam("wd", fInput)
                .when()
                .get("http://www.baidu.com/s")
                .then()
                .log().all()
                .statusCode(200)
                .body("html.head.title", equalTo(fExpected));
    }
}

 

 

 

 

.log().all()

打印所有的網絡請求日誌

 

可以自己再封裝一個小框架

讀數據驅動,讀xml,讀數據庫,遠程文件

 

 

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