Cucumber 是一个能够理解用普通语言 描述的测试用例的支持行为驱动开发(BDD)的自动化测试工具,用Ruby编写,支持Java和.Net等多种开发语言。

现在看看Cucumber中用到的术语 。

在Cucumber中,每个用例用一个feature表示 ,其基本格式如下:
Feature: 描述 
  <story>

<scenario 1> 
  ... 
  <scenario N>

其中,story对feature进行描述 ,其常用格式如下:
In order <目的> 
As a <角色> 
I want <功能描述> 
So that <商业价值>

每个feature可由若干个scenario 构成,用以描述系统的行为 ,其常用格式如下:
Scenario Outline: 描述 
  Given <条件> 
  When <事件> 
  Then <结果> 
如果有多个条件等,可以用关键字And或But进行连接。每个步骤中,可选参数用"<>"标识。

scenario中的每个步骤都需要被定义 ,其格式如下:
关键字 /正则表达式/ do |参数名| 
  代码 
end 
这里 的参数来自于正则表达式,均为字符串类型。

下面看看如何用Cucumber进行开发 (参考自这个 例子)。

首先编写feature ,并保存为feature/addition.feature文件:

Feature: Addition
  In order to avoid silly mistakes
  As a math idiot
  I want to be told the sum of two numbers
 
  Scenario Outline: Add two numbers
    Given I have entered <input_1> into the calculator
    And I have entered <input_2> into the calculator
    When I press <button>
    Then the result should be <output> on the screen
 
  Examples:
    | input_1 | input_2 | button | output |
    | 20 | 30 | add | 50 |

然后用Ruby语言定义scenario中的每个步骤 ,并保存为features/step_definitions/addition.rb文件:

  1. Given /I have entered (/d+) into the calculator/ do |n|
  2. @calc = Calculator.new
  3. @calc.push n.to_i
  4. end
  5. When /I press (/w+)/ do |op|
  6. @result = @calc.send op
  7. end
  8. Then /the result should be (.*) on the screen/ do |result|
  9. @result.should == result.to_f
  10. end

最后编写相应代码 :

  1. class Calculator
  2. def push(n)
  3. @args ||= []
  4. @args << n
  5. end
  6. def add
  7. @args.inject(0){|n,sum| sum+=n}
  8. end
  9. end

进行测试 :
cucumber feature/addition.feature

如果得到类似于以下的结果,则表明代码无误:
1 scenario (1 passed) 
4 steps (4 passed) 
0m0.009s

否则,会得到红色提示,则需要修改相应代码。

Cucumber 行为驱动开发简介的更多相关文章

  1. Android HAL层与Linux Kernel层驱动开发简介

    近日稍微对Android中的驱动开发做了一些简要的了解. HAL:Hardware Abstract Layer 硬件抽象层,由于Linux Kernel需要遵循GPL开源协议,硬件厂商为了保护自己硬 ...

  2. Cucumber测试驱动开发

     Cucumber是一种BDD实践开发工具,属于敏捷开发的组成部分.      在敏捷开发中,对用户进行需求分析时,不是像传统的P&D的开发方式,首先编写大量的用户需求分析文档,而是通过一个个 ...

  3. 行为驱动开发BDD和Cucunber简介

    测试驱动开发(TDD) 1.测试驱动开发,即Test-Driven Development(TDD),测试驱动开发是敏捷开发中的一项核心实践和技术,也是一种设计方法论.TDD的原理是在开发功能代码之前 ...

  4. 1、课程简介-Spring 注解驱动开发

    1.课程简介-Spring 注解驱动开发

  5. 基本数据结构简介--ath9k网卡驱动开发总结(二)

    ath9k驱动代码主要数据结构概览. (1)在ath9k的驱动中,几乎是最顶层的数据结构是ath_softc,这个数据结构几乎随处可见.ath_softc是硬件与MAC层进行交互的中间载体,很多有用的 ...

  6. 驱动开发--【字符设备、块设备简介】【sky原创】

    驱动开发   字符设备,块设备,网络设备   字符设备 以字节流的方式访问, 不能随机访问 有例外,显卡.EEPROM可以随机访问   EEPROM可以擦写1亿次,是一种字符设备,可以随机访问 读写是 ...

  7. .NET里的行为驱动开发

    BDD (Given - When - then) Ruby Cucumber, Java FitNesse , Python RoboFramework, C# specflow nspec .NE ...

  8. 驱动开发入门——NTModel

    上一篇博文中主要说明了驱动开发中基本的数据类型,认识这些数据类型算是驱动开发中的入门吧,这次主要说明驱动开发中最基本的模型--NTModel.介绍这个模型首先要了解R3层是如何通过应用层API进入到内 ...

  9. SLAM+语音机器人DIY系列:(四)差分底盘设计——4.底盘ROS驱动开发

    摘要 运动底盘是移动机器人的重要组成部分,不像激光雷达.IMU.麦克风.音响.摄像头这些通用部件可以直接买到,很难买到通用的底盘.一方面是因为底盘的尺寸结构和参数是要与具体机器人匹配的:另一方面是因为 ...

随机推荐

  1. C++学习,两个小的语法错误-network-programming

    1.bool CServerSocket::initSocket(const char* ip=NULL,const UINT &port)://会出现默认参数为2的错误 解决方案: //C+ ...

  2. 3.数码相框-通过freetype库实现矢量显示

    本章主要内容如下: 1)矢量字体原理 2)使用freetype库实现矢量字体显示 1. 矢量字体原理 将汉字的笔划边缘用直线段描述成封闭的曲线,并将线段各端点的坐标经压缩存储,如下图所示: 由于每个汉 ...

  3. POJ - 1321 dfs [kuangbin带你飞]专题一

    枚举行和列即可,当前已经放下cnt个棋子,当前已经搜索到第r行,如果 n - r + cnt  < k 直接退出,因为后面无法放下剩下的棋子. AC代码 #include<cstdio&g ...

  4. Docker(一):Docker入门教程

    如今Docker的使用已经非常普遍,特别在一线互联网公司.使用Docker技术可以帮助企业快速水平扩展服务,从而到达弹性部署业务的能力.在云服务概念兴起之后,Docker的使用场景和范围进一步发展,如 ...

  5. Linux忘记开机密码怎么办?

    Linux忘记开机密码怎么办?1. 开机ESC/Shift,在出现grub画面时,用上下键选中你平时启动linux的那一项,然后按e键2. 再次用上下键选中你平时启动linux的那一项(类似于kern ...

  6. [Err] 1136 - Column count doesn't match value count at row 1

    1 错误描述 [Err] 1136 - Column count doesn't match value count at row 1 Procedure execution failed 1136 ...

  7. Caused by: com.mysql.jdbc.MysqlDataTruncation: Data truncation: Data too long for column 'content' a

    1.错误描述 org.hibernate.exception.DataException: could not execute statement at org.hibernate.exception ...

  8. java 后台封装json数据学习总结(一)

    一.数据封装 1. List集合转换成json代码 List list = new ArrayList(); list.add( "first" ); list.add( &quo ...

  9. IOS开发之XCode学习009:UIViewController使用

    此文学习来源为:http://study.163.com/course/introduction/1002858003.htm 此工程文件实现功能: 通过点击屏幕事件,调用ViewController ...

  10. python实现列表倒叙打印

    def func(listNode): listNode.reverse() for i in listNode: print(i) li = [1,2,3,4,5] func(li) 利用pytho ...