第一次发分享帖,鸡冻。
之前想学习接口测试,在论坛里看到了 rest-assured 项目,感觉用起来很方便,但是论坛里关于 rest-assured 的介绍还比较少,只能自己鼓捣一点了,希望各位勿喷,多谢!
由于稍微了解一些 cucumber,所以在尝试将 rest-assured 和 cucumber 结合一起使用。下面拿豆瓣读书 API 举例

依赖

rest-assured 相关依赖:

<!-- https://mvnrepository.com/artifact/io.rest-assured/rest-assured -->
<dependency>
    <groupId>io.rest-assured</groupId>
    <artifactId>rest-assured</artifactId>
    <version>3.0.2</version>
</dependency>

<!-- https://mvnrepository.com/artifact/io.rest-assured/json-schema-validator -->
<dependency>
    <groupId>io.rest-assured</groupId>
    <artifactId>json-schema-validator</artifactId>
    <version>3.0.3</version>
</dependency>

cucumber 相关依赖:

<!-- https://mvnrepository.com/artifact/info.cukes/cucumber-core -->
<dependency>
    <groupId>info.cukes</groupId>
    <artifactId>cucumber-core</artifactId>
    <version>1.2.5</version>
</dependency>

<dependency>
    <groupId>info.cukes</groupId>
    <artifactId>cucumber-picocontainer</artifactId>
    <version>1.2.5</version>
</dependency>

<!-- https://mvnrepository.com/artifact/info.cukes/cucumber-junit -->
<dependency>
    <groupId>info.cukes</groupId>
    <artifactId>cucumber-junit</artifactId>
    <version>1.2.5</version>
</dependency>

<!-- https://mvnrepository.com/artifact/info.cukes/cucumber-java -->
<dependency>
    <groupId>info.cukes</groupId>
    <artifactId>cucumber-java</artifactId>
    <version>1.2.5</version>
</dependency>

cucumber feature

cucumber 使用 feature 来描述一个场景,相当于是测试用例,这样的好处是可读性比较好

book.feature

Scenario:
  Given there is basic info
  |baseURL|https://api.douban.com/|
  When I get the book with asin "9787108059444"
  Then state code will be "200"
  And response includes the following data
  |id|27029497|

request steps

实现 there is basic info 的步骤,在此步骤中,赋值一些基本信息,如 rest-assured 中的 baseUrl、auth、param 等。

@Given("^there is basic info$")
public void base_info(Map<String,String>infoMap){
    String baseURL = null;

    for (Map.Entry<String, String> field : infoMap.entrySet()) {
        if (field.getKey().equalsIgnoreCase("baseURL")){
            baseURL = field.getValue();
        }
    }
    request = given().log().all().baseUri(baseURL);
}

实现访问具体 api 的步骤,可以根据实际需要开启与否 log

@When("^I get the book with asin \"([^\"]*)\"$")
public void get_book_with_ASIN(String ASIN){
    URI uri = URI.create("/v2/book/isbn/" + ASIN);
    response = request.when().log().all().get(uri);
}

结果验证

状态码验证

@Then("^state code will be \"([^\"]*)\"$")
public void verify_state_code(int stateCode){
    response.then().assertThat().statusCode(stateCode);
}

response 验证

@And("^response includes the following data$")
public void response_equals(Map<String,String> responseFields){
    for (Map.Entry<String, String> field : responseFields.entrySet()) {
        response.then().assertThat().body(field.getKey(), equalTo(field.getValue()));
    }
}

当然,也可以使用 json schema

@And("^verify response with Json Schema \"([^\"]*)\"$")
public void responseWithJsonSchema(String JsonSchemaName){
    JsonSchemaFactory jsonSchemaFactory = JsonSchemaFactory.newBuilder().setValidationConfiguration(ValidationConfiguration.newBuilder().setDefaultVersion(DRAFTV4).freeze()).freeze();
    json.assertThat().body(matchesJsonSchemaInClasspath("jsonSchema/" + JsonSchemaName).using(jsonSchemaFactory));
}

大概的实例就是这样,欢迎交流,谢谢!


↙↙↙阅读原文可查看相关链接,并与作者交流