BDD cucumber-api 安装与试用

超爱fitnesse · June 22, 2015 · Last by 超爱fitnesse replied at June 22, 2015 · 1657 hits

cucumber-api,用于验证响应格式为 JSON 的 Web Service API。

可以用来单独测试 Web Service API,或者与 Calabash 配合进行手机 APP 和 Web Service API 的联合测试。

主页: https://rubygems.org/gems/cucumber-api

源代码: https://github.com/hidroh/cucumber-api

安装步骤:

以 Debian 环境为例,其他环境如 Redhat,Windows 等主要是 Ruby 安装命令的差别。

安装 Ruby 1.9.3

说明: cucumber-api 依赖 ruby 版本要不低于 1.9.3

sudo apt-get install ruby
sudo apt-get install ruby1.9.1-dev

安装后版本:1.9.3p194

shen@debian:~$ ruby -v
ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-linux]
shen@debian:~$ gem -v
1.8.23

安装 Cucumber

先设置淘宝的 gem 源:

sudo gem sources --remove http://rubygems.org/  
sudo gem sources -a http://ruby.taobao.org/

再次查看 gem 源:

shen@debian:~$ sudo gem sources --list
*** CURRENT SOURCES ***

http://ruby.taobao.org/

安装 cucumber:

sudo gem install cucumber

查看已安装 cucumber 版本:

shen@debian:~$ cucumber --version
2.0.0

安装 cucumber-api

sudo gem install cucumber-api

查看 gem list:

shen@debian:~$ gem list

*** LOCAL GEMS ***

addressable (2.3.8)
builder (3.2.2)
cucumber (2.0.0)
cucumber-api (0.3)
cucumber-core (1.1.3)
diff-lcs (1.2.5)
domain_name (0.5.24)
gherkin (2.12.2)
http-cookie (1.0.2)
json-schema (2.5.1)
jsonpath (0.5.6)
mime-types (2.6.1)
multi_json (1.11.1)
multi_test (0.1.2)
netrc (0.10.3)
rest-client (1.8.0)
unf (0.1.4)
unf_ext (0.0.7.1)

试运行 sample 项目

创建 cucumber 项目

shen@debian:~/bdd-api-sample$ cucumber --init
  create   features
  create   features/step_definitions
  create   features/support
  create   features/support/env.rb
shen@debian:~/bdd-api-sample$ find
.
./features
./features/step_definitions
./features/support
./features/support/env.rb

在 features/support/env.rb 中加入 require 'cucumber-api'

shen@debian:~/bdd-api-sample$ cat features/support/env.rb 
require 'cucumber-api'

下载 https://github.com/hidroh/cucumber-api/blob/master/features/sample.feature

shen@debian:~/bdd-api-sample$ touch features/sample.feature
shen@debian:~/bdd-api-sample$ vi features/sample.feature 
shen@debian:~/bdd-api-sample$ cat features/sample.feature 
# https://github.com/HackerNews/API
Feature: Hacker News REST API validation

  Scenario: Verify top stories JSON schema
    When I send and accept JSON
    And I send a GET request to "https://hacker-news.firebaseio.com/v0/topstories.json?print=pretty"
    Then the response status should be "200"
    And the JSON response should follow "features/schemas/topstories.json"

  Scenario Outline: Verify item JSON schema
    When I send and accept JSON
    And I send a GET request to "https://hacker-news.firebaseio.com/v0/topstories.json?print=pretty"
    Then the response status should be "200"
    And the JSON response root should be array
    When I grab "$[0]" as "id"
    And I send a GET request to "https://hacker-news.firebaseio.com/v0/item/{id}.json" with:
      | print  |
      | pretty |
    Then the response status should be "200"
    And the JSON response root should be object
    And the JSON response should have <optionality> key "<key>" of type <value type>

    Examples:
      | key   | value type | optionality |
      | id    | numeric    | required    |
      | score | numeric    | required    |
      | url   | string     | optional    |

试运行 sample:

输入命令cucumber:

上述 sample 因缺少文件 features/schemas/topstories.json,导致第一个 Scenario 失败

下载 https://github.com/hidroh/cucumber-api/blob/master/features/schemas/topstories.json

shen@debian:~/bdd-api-sample$ cat features/schemas/topstories.json 
{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "array",
  "items": {
    "type": "number"
  }
}

再次运行 sample:

设置 verbose 输出

方式 1: 设置环境变量 cucumber_api_verbose=true

再次输入 cucumber 后,多输出了 RestClient 的 http 请求和响应信息,可以帮助调试。

方式 2: cucumber -p verbose

前提是 config/cucumber.yml 已经正确设置:

shen@debian:~/bdd-api-sample$ cat config/cucumber.yml 
# config/cucumber.yml
##YAML Template
---
verbose     : cucumber_api_verbose=true

输入命令cucumber -p verbose也可以看到 RestClient 的输出:

Step 扩展

定义 3 个常用的扩展指令

  1. 设置 Header,就是 curl 的-H 参数的内容
  2. 打印 response body
  3. 打印格式化后的 response body
shen@debian:~/bdd-api-sample$ cat features/step_definitions/api_steps.rb 
Given(/^I set header key "(.*?)" and value "(.*?)"$/) do |key, value|
  @headers = {} if @headers.nil?
  p_value = value
  @grabbed.each { |k, v| p_value = v if value == %/{#{k}}/ } unless @grabbed.nil?
  p_value = File.new %-#{Dir.pwd}/#{p_value.sub 'file://', ''}- if %/#{p_value}/.start_with? "file://"
  @headers[%/#{key}/] = p_value
end

Then(/^I dump the JSON response$/) do
  puts @response.to_s
end

Then(/^I dump the pretty JSON response$/) do
  puts @response.to_json_s
end
shen@debian:~/bdd-api-sample$ cat features/baidu-map.feature 
Feature: 根据ip获取地理位置

  Scenario: get location by ip
    When I send a GET request to "http://api.map.baidu.com/location/ip?ip=202.198.16.3&coor=bd09ll&ak=60IFKTCwlIsSpDcGfkx36L8u"
    Then I dump the pretty JSON response
    Then the response status should be "200"
shen@debian:~/bdd-api-sample$ 

cucumber 增加参数-p verbose时输出 response:

cucumber 无参数-p verbose时不输出 response:

共收到 5 条回复 时间 点赞

不错,赞分享,赞格式。

#1 楼 @monkey
这个开源项目是从 github 上直接搜索并筛选出来的,因为用了 calabash,所以想在 cucumber 平台上统一客户端测试和服务 API 测试,自己开发的 cucumber 测试服务 API 指令老是用的不舒服,还是老外的指令写的好。

#2 楼 @htmlbiji 嗯嗯明白。希望 Testerhome 越来越多你的分享以及他人相似质量的分享~~~

赞!格式很不错,内容也很详细!

另外,想请教一下接口测试用 BDD 来写的话编写用例的效率如何?
因为接口的 API 数量一般较多,所以用例数量不少,想了解下用 BDD 写的话编写速度怎样。

cucumber 支持 scenario 模板的,如:
Examples:
| key | value type | optionality |
| id | numeric | required |
| score | numeric | required |
| url | string | optional |

只要指令封装的合理,复用次数多,写起测试用例还是很快的。

需要 Sign In 后方可回复, 如果你还没有账号请点击这里 Sign Up