51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

手机号码,快捷登录

查看: 3169|回复: 6
打印 上一主题 下一主题

[转贴] 使用 rest-assured 和 cucumber 进行接口测试实例

[复制链接]

该用户从未签到

跳转到指定楼层
1#
发表于 2017-5-27 13:25:43 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 测试的味道 于 2017-5-27 13:52 编辑

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

  2. <!-- https://mvnrepository.com/artifact/io.rest-assured/rest-assured -->
  3. <dependency>
  4.     <groupId>io.rest-assured</groupId>
  5.     <artifactId>rest-assured</artifactId>
  6.     <version>3.0.2</version>
  7. </dependency>

  8. <!-- https://mvnrepository.com/artifact/io.rest-assured/json-schema-validator -->
  9. <dependency>
  10.     <groupId>io.rest-assured</groupId>
  11.     <artifactId>json-schema-validator</artifactId>
  12.     <version>3.0.3</version>
  13. </dependency>
复制代码
  1. cucumber 相关依赖:

  2. <!-- https://mvnrepository.com/artifact/info.cukes/cucumber-core -->
  3. <dependency>
  4.     <groupId>info.cukes</groupId>
  5.     <artifactId>cucumber-core</artifactId>
  6.     <version>1.2.5</version>
  7. </dependency>

  8. <dependency>
  9.     <groupId>info.cukes</groupId>
  10.     <artifactId>cucumber-picocontainer</artifactId>
  11.     <version>1.2.5</version>
  12. </dependency>

  13. <!-- https://mvnrepository.com/artifact/info.cukes/cucumber-junit -->
  14. <dependency>
  15.     <groupId>info.cukes</groupId>
  16.     <artifactId>cucumber-junit</artifactId>
  17.     <version>1.2.5</version>
  18. </dependency>

  19. <!-- https://mvnrepository.com/artifact/info.cukes/cucumber-java -->
  20. <dependency>
  21.     <groupId>info.cukes</groupId>
  22.     <artifactId>cucumber-java</artifactId>
  23.     <version>1.2.5</version>
  24. </dependency>

  25. cucumber feature
复制代码
  1. cucumber 使用 feature 来描述一个场景,相当于是测试用例,这样的好处是可读性比较好
  2. book.feature

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

  10. request steps
复制代码
  1. 实现 there is basic info 的步骤,在此步骤中,赋值一些基本信息,如 rest-assured 中的 baseUrl、auth、param 等。

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

  5.     for (Map.Entry<String, String> field : infoMap.entrySet()) {
  6.         if (field.getKey().equalsIgnoreCase("baseURL")){
  7.             baseURL = field.getValue();
  8.         }
  9.     }
  10.     request = given().log().all().baseUri(baseURL);
  11. }
复制代码
  1. 实现访问具体 api 的步骤,可以根据实际需要开启与否 log

  2. @When("^I get the book with asin \"([^\"]*)\"$")
  3. public void get_book_with_ASIN(String ASIN){
  4.     URI uri = URI.create("/v2/book/isbn/" + ASIN);
  5.     response = request.when().log().all().get(uri);
  6. }
复制代码
  1. 结果验证
  2. 状态码验证

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

  7. response 验证

  8. @And("^response includes the following data$")
  9. public void response_equals(Map<String,String> responseFields){
  10.     for (Map.Entry<String, String> field : responseFields.entrySet()) {
  11.         response.then().assertThat().body(field.getKey(), equalTo(field.getValue()));
  12.     }
  13. }
复制代码
  1. 当然,也可以使用 json schema

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


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

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏
回复

使用道具 举报

该用户从未签到

4#
 楼主| 发表于 2017-5-27 16:24:10 | 只看该作者
回复 支持 反对

使用道具 举报

该用户从未签到

6#
 楼主| 发表于 2017-5-27 17:03:44 | 只看该作者
回复 支持 反对

使用道具 举报

本版积分规则

关闭

站长推荐上一条 /1 下一条

小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

GMT+8, 2024-9-21 11:14 , Processed in 0.068765 second(s), 22 queries .

Powered by Discuz! X3.2

© 2001-2024 Comsenz Inc.

快速回复 返回顶部 返回列表