cucumber-api,用于验证响应格式为JSON的WebServiceAPI。可以用来单独测试WebServiceAPI,或者与Calabash配合进行手机APP和WebServiceAPI的联合测试。
安装步骤:以Debia环境为例,其他环境如Redhat,Widows等主要是Ruby安装命令的差别。
安装Ruby1.9.3说明: cucumber-api依赖ruby版本要不低于1.9.3
sudo apt-get istall rubysudo apt-get istall ruby1.9.1-dev安装后版本:1.9.3p194
she@debia:~$ ruby -vruby 1.9.3p194 (2012-04-20 revisio 35410) [x86_64-liux]she@debia:~$ gem -v 1.8.23安装Cucumber先设置淘宝的gem源:
sudo gem sources --remove https://rubygems.org/ sudo gem sources -a https://ruby.taobao.org/再次查看gem源:
she@debia:~$ sudo gem sources --list *** CURRENT SOURCES *** https://ruby.taobao.org/安装cucumber:
sudo gem istall cucumber查看已安装cucumber版本:
she@debia:~$ cucumber --versio 2.0.0安装cucumber-apisudo gem istall cucumber-api查看gemlist:
she@debia:~$ 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)domai_ame (0.5.24)gherki (2.12.2)http-cookie (1.0.2)jso-schema (2.5.1)jsopath (0.5.6)mime-types (2.6.1)multi_jso (1.11.1)multi_test (0.1.2)etrc (0.10.3)rest-cliet (1.8.0)uf (0.1.4)uf_ext (0.0.7.1)试运行sample项目创建cucumber项目she@debia:~/bdd-api-sample$ cucumber --iit create features create features/step_defiitios create features/support create features/support/ev.rbshe@debia:~/bdd-api-sample$ fid../features./features/step_defiitios./features/support./features/support/ev.rb在features/support/ev.rb中加入 require'cucumber-api':
she@debia:~/bdd-api-sample$ cat features/support/ev.rb require 'cucumber-api'下载 https://github.com/hidroh/cucumber-api/blob/master/features/sample.feature
she@debia:~/bdd-api-sample$ touch features/sample.featureshe@debia:~/bdd-api-sample$ vi features/sample.feature she@debia:~/bdd-api-sample$ cat features/sample.feature # https://github.com/HackerNews/API Feature: Hacker News REST API validatio Sceario: Verify top stories JSON schema Whe I sed ad accept JSON Ad I sed a GET request to "https://hacker-ews.firebaseio.com/v0/topstories.jso?prit=pretty" The the respose status should be "200" Ad the JSON respose should follow "features/schemas/topstories.jso" Sceario Outlie: Verify item JSON schema Whe I sed ad accept JSON Ad I sed a GET request to "https://hacker-ews.firebaseio.com/v0/topstories.jso?prit=pretty" The the respose status should be "200" Ad the JSON respose root should be array Whe I grab "$[0]" as "id" Ad I sed a GET request to "https://hacker-ews.firebaseio.com/v0/item/{id}.jso" with: | prit | | pretty | The the respose status should be "200" Ad the JSON respose root should be object Ad the JSON respose should have <optioality> key "<key>" of type <value type> Examples: | key | value type | optioality | | id | umeric | required | | score | umeric | required | | url | strig | optioal |下载 https://github.com/hidroh/cucumber-api/blob/master/features/schemas/topstories.jso :she@debia:~/bdd-api-sample$ cat features/schemas/topstories.jso { "$schema": "https://jso-schema.org/draft-04/schema#", "type": "array", "items": { "type": "umber" }}试运行sample:设置verbose输出方式1:设置环境变量cucumber_api_verbose=true再次输入cucumber后,多输出了RestCliet的http请求和响应信息,可以帮助调试。
export cucumber_api_verbose=true方式2:cucumber-pverbose前提是cofig/cucumber.yml已经正确设置:
she@debia:~/bdd-api-sample$ cat cofig/cucumber.yml # cofig/cucumber.yml ##YAML Template ---verbose : cucumber_api_verbose=true输入命令cucumber-pverbose也可以看到RestCliet的输出:
Step扩展定义3个常用的扩展指令设置Header,就是curl的-H参数的内容
打印resposebody
打印格式化后的resposebody
she@debia:~/bdd-api-sample$ cat features/step_defiitios/api_steps.rb Give(/^I set header key "(.*?)" ad value "(.*?)"$/) do |key, value| @headers = {} if @headers.il? p_value = value @grabbed.each { |k, v| p_value = v if value == %/{#{k}}/ } uless @grabbed.il? p_value = File.ew %-#{Dir.pwd}/#{p_value.sub 'file://', ''}- if %/#{p_value}/.start_with? "file://" @headers[%/#{key}/] = p_value ed The(/^I dump the JSON respose$/) do puts @respose.to_s ed The(/^I dump the pretty JSON respose$/) do puts @respose.to_jso_s edshe@debia:~/bdd-api-sample$ cat features/baidu-map.feature Feature: 根据ip获取地理位置 Sceario: get locatio by ip Whe I sed a GET request to "https://api.map.baidu.com/locatio/ip?ip=202.198.16.3&coor=bd09ll&ak=60IFKTCwlIsSpDcGfkx36L8u" The I dump the pretty JSON respose The the respose status should be "200" she@debia:~/bdd-api-sample$cucumber增加参数-pverbose时输出respose:
cucumber无参数-pverbose时不输出respose:
评论