博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
cucumber-api安装与试用
阅读量:6543 次
发布时间:2019-06-24

本文共 4632 字,大约阅读时间需要 15 分钟。

hot3.png

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

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

主页:

源代码:

安装步骤:

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

安装Ruby 1.9.3

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

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

安装后版本:1.9.3p194

shen@debian:~$ ruby -vruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-linux]shen@debian:~$ gem -v1.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 --version2.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.rbshen@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'

下载

shen@debian:~/bdd-api-sample$ touch features/sample.featureshen@debian:~/bdd-api-sample$ vi features/sample.feature shen@debian:~/bdd-api-sample$ cat features/sample.feature # https://github.com/HackerNews/APIFeature: 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 
key "
" of type
Examples: | key | value type | optionality | | id | numeric | required | | score | numeric | required | | url | string | optional |

试运行sample:

输入命令cucumber:

输入图片说明

输入图片说明

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

下载 :

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请求和响应信息,可以帮助调试。

export cucumber_api_verbose=true

输入图片说明

方式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_valueendThen(/^I dump the JSON response$/) do  puts @response.to_sendThen(/^I dump the pretty JSON response$/) do  puts @response.to_json_send
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: 输入图片说明

转载于:https://my.oschina.net/fitnessefan/blog/469372

你可能感兴趣的文章
SONY控制键盘JX-11,EVI-D70P控制方案
查看>>
Spring AOP 之二:Pointcut注解表达式
查看>>
在普通台式机上搭建服务器虚拟化架构Esxi平台
查看>>
电话线路 30B+D 名词解释
查看>>
python字典嵌套字典实例
查看>>
吉炬消费系统软件输入密码后无法打开软件界面故障处理
查看>>
Hibernate学习系列————注解一对多双向实例
查看>>
Cannot load from mysql.proc
查看>>
网络运维之 EX4200消除var分区使用过高的告警
查看>>
【最好的流程是没有流程】
查看>>
Apache Thrift 教程
查看>>
Python Epoll
查看>>
AS3歌词同步详解
查看>>
单例模式
查看>>
Linux环境NetBeans字体通用抗据齿解决方法
查看>>
Eclipse的花括号(brace)的输入偏好问题
查看>>
工作记录
查看>>
python之重写父类方法
查看>>
cucumber-api安装与试用
查看>>
计算硬件简史
查看>>