Automating Features with cucumber

cucumber

  1. Given /^I am not yet playing$/ do
  2. pending # express the regexp above with the code you wish you had
  3. end

这就是一个cucumber的步骤定义。你可以把它想成是一个方法,然后在definition中定义一个这样的方法。

  1. mkdir features/step_definitions
  2. vi features/step_definitions/codebreaker_steps.rb

把上面的内容拷贝进去,并且删除pending一行。

  1. Given /^I am not yet playing$/ do
  2. end

执行下面的命令

  1. cucumber features/codebreaker_start_game.feature
  1. Feature: code-breaker starts game
  2. As a code-breaker
  3. I want to start a game
  4. So that I can break the code
  5. Scenario: start game                            # features/codebreaker_start_game.feature:7
  6. Given I am not yet playing                    # features/step_definitions/codebreaker_steps.rb:1
  7. When I start a new game                       # features/codebreaker_start_game.feature:9
  8. Then I should see "Welcome to Codebreaker!"   # features/codebreaker_start_game.feature:10
  9. And I should see "Enter guess:"               # features/codebreaker_start_game.feature:11
  10. But I should not see "What is your question?" # features/codebreaker_start_game.feature:12
  11. 1 scenario (1 undefined)
  12. 5 steps (4 undefined, 1 passed)
  13. 0m0.005s
  14. You can implement step definitions for undefined steps with these snippets:
  15. When /^I start a new game$/ do
  16. pending # express the regexp above with the code you wish you had
  17. end
  18. Then /^I should see "(.*?)"$/ do |arg1|
  19. pending # express the regexp above with the code you wish you had
  20. end
  21. Then /^I should not see "(.*?)"$/ do |arg1|
  22. pending # express the regexp above with the code you wish you had
  23. end

结果告诉我们有一个scenario,五个步骤,四个还没有定义,一个已经通过。

cucumber命令的参数是features/codebreaker_start_game.feature,在cucumber启动的时候会加载目录下所有的.rb文件,已经子目录下面的.rb文件,包括前面的features/step_definitions/codebreaker_steps.rb。

我们通过调用cucumber提供的五个方法来定义feature中的步骤:Given(), When(), Then(), And(), But()。And()和But()会呈现跟在前面的Given, When, Then的意思。在上面的例子中,scenario中最后两行的And和But被当做是Then来处理。

每个方法都通过正则表达来匹配。

Given方法对应的正则表达式是:/^I am not yet playing$/。就会匹配feature中的Given部分。

When方法中需要我们创建一个新游戏,然后开始这个游戏。

  1. When /^I start a new game$/ do
  2. Codebreaker::Game.new.start
  3. end

这时候,我们的应用还没有任何代码,我们只是按照我们想要的方式来写。我们希望简单,尽可能的简单。
The code you wish you had.

再次运行

  1. cucumber features/codebreaker_start_game.feature

你会发现一些红色的信息
uninitialized constant Codebreaker (NameError)

因为你还没有定义Codebreaker以及Game以及start方法。好吧,我们来定义一下。

  1. mkdir -p lib/codebreaker/
  2. vi lib/codebreaker/game.rb

输入下面的内容

  1. module Codebreaker
  2. class Game
  3. def start
  4. end
  5. end
  6. end

再次执行

  1. cucumber features/codebreaker_start_game.feature

还是出现之前的红色信息。

这是因为按照约定,在lib目录需要有一个以顶级module名称命名的文件。

  1. vi lib/codebreaker.rb

写入下面的内容

  1. require 'codebreaker/game'
  1. vi features/support/env.rb

写入下面的内容

  1. $LOAD_PATH << File.expand_path('../../../lib', __FILE__)
  2. require 'codebreaker'

再次执行

  1. cucumber features/codebreaker_start_game.feature

看到的内容又变绿色了。

在完成第二个步骤之后,我们来看看Then这个步骤。

在这个步骤中,我们希望在console中看到Welcome to Codebreaker!。这意味着我们需要一个工具,来捕获Game发送到STDOUT的信息。

  1. vi features/step_definitions/codebreaker_steps.rb

加入下面的内容

  1. Then /^I should see "(.*?)"$/ do |message|
  2. output.messages.should include(message)
  3. end

修改When的内容

  1. When /^I start a new game$/ do
  2. game=Codebreaker::Game.new(output)
  3. game.start
  4. end

增加下面的内容

  1. class Output
  2. def messages
  3. @messages ||= []
  4. end
  5. def puts(message)
  6. messages << message
  7. end
  8. end
  9. def output
  10. @output ||= Output.new
  11. end

再次执行

  1. cucumber features/codebreaker_start_game.feature

提示我们wrong number of arguments(1 for 0)(ArgumentError)

是因为我没有定义带有参数的initialize。

  1. vi lib/codebreaker/game.rb

用下面的内容替换

  1. module Codebreaker
  2. class Game
  3. def initialize(output)
  4. end
  5. def start
  6. end
  7. end
  8. end

再次执行

  1. cucumber features/codebreaker_start_game.feature

这时候提示我们有逻辑错误,因为我们的game还没有完成。我们会在后面完成。

总结

我们先在.feature文件中写一个feature的scenario,已经scenario的steps,然后在step_definitions中定义Given,When,Then,然后在通过测试cucumber .feature文件来驱动我们编写实现代码。

当目前为止,我们已经学会使用cucumber从外部描述一件事。在接下来的章节中,我们将会进行从外到里的工作方式,使用RSpec来驱动单个对象的外部行为。

本文转自 virusswb 51CTO博客,原文链接:http://blog.51cto.com/virusswb/1060620,如需转载请自行联系原作者

The RSpec Book笔记《三》Automating Features with Cucumber使用cucumber自动完成features相关推荐

  1. The RSpec Book笔记《一》初步认识TDD,BDD,RSpec,Cucumber

    TDD(Test-Driven Development)测试驱动开发. 在编写代码之前先编写一个测试,这时候测试是失败的,red,因为还没有写对应的实现代码.然后开始编写实现的代码,然后跑刚才写的测试 ...

  2. J2EE学习笔记三:EJB基础概念和知识 收藏

    J2EE学习笔记三:EJB基础概念和知识 收藏 EJB正是J2EE的旗舰技术,因此俺直接跳到这一章来了,前面的几章都是讲Servlet和JSP以及JDBC的,俺都懂一些.那么EJB和通常我们所说的Ja ...

  3. 吴恩达机器学习笔记55-异常检测算法的特征选择(Choosing What Features to Use of Anomaly Detection)

    吴恩达机器学习笔记55-异常检测算法的特征选择(Choosing What Features to Use of Anomaly Detection) 对于异常检测算法,使用特征是至关重要的,下面谈谈 ...

  4. tensorflow学习笔记(三十二):conv2d_transpose (解卷积)

    tensorflow学习笔记(三十二):conv2d_transpose ("解卷积") deconv解卷积,实际是叫做conv_transpose, conv_transpose ...

  5. Ethernet/IP 学习笔记三

    Ethernet/IP 学习笔记三 原文为硕士论文: 工业以太网Ethernet/IP扫描器的研发 知网网址: http://kns.cnki.net/KCMS/detail/detail.aspx? ...

  6. 深度学习入门教程UFLDL学习实验笔记三:主成分分析PCA与白化whitening

     深度学习入门教程UFLDL学习实验笔记三:主成分分析PCA与白化whitening 主成分分析与白化是在做深度学习训练时最常见的两种预处理的方法,主成分分析是一种我们用的很多的降维的一种手段,通 ...

  7. AT91RM9200Linux移植笔记(三)-移植Linux kernel 2.6.17

    AT91RM9200Linux移植笔记(三)-移植Linux kernel 2.6.17 手上板子原来自带的是2.4.19的内核, 打算移植新的2.6的内核,从网上下了2.6.17的kernel,下载 ...

  8. iView学习笔记(三):表格搜索,过滤及隐藏列操作

    iView学习笔记(三):表格搜索,过滤及隐藏某列操作 1.后端准备工作 环境说明 python版本:3.6.6 Django版本:1.11.8 数据库:MariaDB 5.5.60 新建Django ...

  9. 吴恩达《机器学习》学习笔记三——多变量线性回归

    吴恩达<机器学习>学习笔记三--多变量线性回归 一. 多元线性回归问题介绍 1.一些定义 2.假设函数 二. 多元梯度下降法 1. 梯度下降法实用技巧:特征缩放 2. 梯度下降法的学习率 ...

  10. mysql数据库权威指南_MySQL_MySQL权威指南读书笔记(三),第二章:MYSQL数据库里面的数 - phpStudy...

    MySQL权威指南读书笔记(三) 第二章:MYSQL数据库里面的数据 用想用好MYSQL,就必须透彻理解MYSQL是如何看待和处理数据的.本章主要讨论了两个问题:一是SQL所能处理的数据值的类型:二是 ...

最新文章

  1. Android FrameWork——Touch事件派发过程详解
  2. vue项目中使用JSX
  3. c语言 单词长度统计,编写一个程序,打印输入中单词长度的直方图
  4. 【人工智能】全网首发!2020年AI、CV、NLP等最全国际会议、顶会时间汇总!!
  5. oracle备份恢复之rman恢复到异机(二)
  6. 释放只有声明没有定义的对象,都是耍流氓
  7. 基于mini2440的两种触屏中断程序(T35)
  8. 【bug解决】No OpKernel was registered to support Op 'CudnnRNN' with these attrs.
  9. pycharm 修改darcual(暗黑)主题滚动条颜色
  10. dnf脚本是php,dnf自动搬砖脚本教程autojs在使用
  11. 正点原子T100智能焊台体验,顺便咱们来说说它的软件菜单、界面切换如何来实现?
  12. 老龄化带来新机遇 银发经济实力不容小觑
  13. MFC双人五子棋(VS2019)
  14. 计算机与通信学院方阵解说词,方阵解说词
  15. RxSwift取消定时
  16. Memcached完整教程
  17. windows安装证书后无法信任问题解决
  18. App前端及后端接口,模拟数据及返回值
  19. 统计学中基础概念说明
  20. 在线预览office文件

热门文章

  1. 利用matlab函数创建数组
  2. 51单片机流水灯程序
  3. 阿里巴巴电话面试(遭到了阿里的完虐,被一顿痛批)
  4. 模电:集成运算放大器
  5. 现代数字图像处理技术提高及应用案例详解
  6. 基于 VisualStudio2019 的 ASP.NET 后台环境搭建
  7. 计算机辅助设计和工程图学,工程制图与计算机辅助设计
  8. Energyplus运行提示缺失.OCX文件的解决方法
  9. 联想启天M5710不开机,开机后卡logo
  10. 如何长期记忆GRE词汇而保持不忘呢?