测试的味道 发表于 2017-5-27 13:25:43

使用 rest-assured 和 cucumber 进行接口测试实例

本帖最后由 测试的味道 于 2017-5-27 13:52 编辑

之前想学习接口测试,在论坛里看到了 rest-assured 项目,感觉用起来很方便,但是论坛里关于 rest-assured 的介绍还比较少,只能自己鼓捣一点了,希望各位勿喷,多谢!
由于稍微了解一些 cucumber,所以在尝试将 rest-assured 和 cucumber 结合一起使用。下面拿豆瓣读书 API 举例
依赖
rest-assured 相关依赖:
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));
}

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

梦想家 发表于 2017-5-27 15:08:39

小皮球的故事 发表于 2017-5-27 15:11:38

赞!

测试的味道 发表于 2017-5-27 16:24:10

梦想家 发表于 2017-5-27 15:08


:lol

小爸爸 发表于 2017-5-27 17:03:03

:victory:

测试的味道 发表于 2017-5-27 17:03:44

小爸爸 发表于 2017-5-27 17:03


:)

jingzizx 发表于 2017-5-28 18:12:24

:lol
页: [1]
查看完整版本: 使用 rest-assured 和 cucumber 进行接口测试实例