当前位置: 首页 > news >正文

[每周软件]:Cucumber:Hello World

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

##简介##


OSChina 的链接 Cucumber

ruby 的 BDD框架,类似自然语言的DSL,适用于BDD模式和验收测试。

Book

The Rspec Book

The Cucumber Book

##Before Start##

安装,Ruby和RubyGems环境就不说了,那就剩下一句话

<!-- lang: ruby -->
gem install cucumber

##Step By Step ##

1: 描述行为

文件 "./features/greeter_says_hello.feature"

<!-- lang: ruby -->
Feature: greeter says hello
   Cucumber的Hello World

  Scenario: 对世界打个招呼吧
    Given 如果我给了你一个测试的hello
    When 当我执行greet的时候
    Then 我应该看到 "Hello Cucumber!"

一个Feature 可以理解为一个功能,一个Feature由三部分组成:

  • 标题:greeter says hello
  • 描述:2~4行
  • Scenario(场景): 最后一部分

一个Feature可以有多个场景,标题和描述部分可以使用中文。 一个Scenario是对feature的划分,比如一个登录的feature,scenario可以是不同角色的登录情况。(细节待补) Given When Then 是这种DSL里的关键字 还有And

然后在.目录里运行一下

<!-- lang: ruby -->
 $ cucumber features
        Feature: greeter says hello
          Cucumber的Hello World
        
          Scenario: 对世界打个招呼吧             # features/greeter_says_hello.feature:3
            Given 如果我给了你一个测试的hello       # features/greeter_says_hello.feature:4
            When 当我执行greet的时候        # features/greeter_says_hello.feature:5
            Then 我应该看到 "Hello Cucumber!" # features/greeter_says_hello.feature:6
        
        1 scenario (1 undefined)
        3 steps (3 undefined)
        0m0.003s
        
        You can implement step definitions for undefined steps with these snippets:
        
        Given /^如该我给了你一个测试的hello$/ do
          pending # express the regexp above with the code you wish you had
        end
        
        When /^当我给你一个greet的时候$/ do
          pending # express the regexp above with the code you wish you had
        end
        
        Then /^我应该看到 "(.*?)"$/ do |arg1|
          pending # express the regexp above with the code you wish you had
        end

2: Write a step definition in Ruby

文件:./features/step_definitions/greeter_steps.rb

(注:这里文件名没有硬性规定 是根据内部的代码来查找的,greeter可以用greeter_says_hello_steps.rb)

把上面输出的内容copy进来

<!-- lang: ruby -->
# -*- encoding : utf-8 -*-
    Given /^如该我给了你一个测试的hello$/ do
      pending # express the regexp above with the code you wish you had
    end
    
    When /^当我给你一个greet的时候$/ do
      pending # express the regexp above with the code you wish you had
    end
    
    Then /^我应该看到 "(.*?)"$/ do |arg1|
      pending # express the regexp above with the code you wish you had
    end

运行一下看看结果

<!-- lang: ruby -->
$ cucumber features
    Feature: greeter says hello
      Cucumber的Hello World
    
      Scenario: 对世界打个招呼吧             # features/greeter_says_hello.feature:3
        Given 如该我给了你一个测试的hello       # features/step_definitions/g_steps.rb:2
          TODO (Cucumber::Pending)
          ./features/step_definitions/g_steps.rb:3:in `/^如该我给了你一个测试的hello$/'
          features/greeter_says_hello.feature:4:in `Given 如该我给了你一个测试的hello'
        When 当我给你一个greet的时候        # features/step_definitions/g_steps.rb:6
        Then 我应该看到 "Hello Cucumber!" # features/step_definitions/g_steps.rb:10
    
    1 scenario (1 pending)
    3 steps (2 skipped, 1 pending)
    0m0.004s

pending指正在进行中,因为Given是正在pending,所以跳过了when和then

OK,然后写一些真正的Step

文件:./features/step_definitions/greeter_steps.rb

<!-- lang: ruby -->
# -*- encoding : utf-8 -*-
Given /^如该我给了你一个测试的hello$/ do
  @hello = Hello.new
end

When /^当我给你一个greet的时候$/ do
  @message = @hello.greet
end

Then /^我应该看到 "(.*?)"$/ do |greeting|
  @message.should == greeting
end

这里需要注意的是then里面的 greeting代表的是前面那个描述里面的双引号扩起来的结果

3: Run and watch it fail

<!-- lang: ruby -->
 $ cucumber features
    Feature: greeter says hello
      Cucumber的Hello World
    
      Scenario: 对世界打个招呼吧             # features/greeter_says_hello.feature:3
        Given 如该我给了你一个测试的hello       # features/step_definitions/g_steps.rb:7
          uninitialized constant Hello (NameError)
          ./features/step_definitions/g_steps.rb:8:in `/^如该我给了你一个测试的hello$/'
          features/greeter_says_hello.feature:4:in `Given 如该我给了你一个测试的hello'
        When 当我给你一个greet的时候          # features/step_definitions/g_steps.rb:11
        Then 我应该看到 "Hello Cucumber!" # features/step_definitions/g_steps.rb:15
    
    Failing Scenarios:
    cucumber features/greeter_says_hello.feature:3 # Scenario: 对世界打个招呼吧
    
    1 scenario (1 failed)
    3 steps (1 failed, 2 skipped)
    0m0.003s

4: Write code to make the step pass

文件:./features/step_definitions/greeter_steps.rb

(注:为了简单起见我就直接写到这里面了)

<!-- lang: ruby -->
# -*- encoding : utf-8 -*-
class Hello
  def greet
    "Hello Cucumber!"
  end
end
Given /^如该我给了你一个测试的hello$/ do
  @hello = Hello.new
end

When /^当我给你一个greet的时候$/ do
  @message = @hello.greet
end

Then /^我应该看到 "(.*?)"$/ do |greeting|
  @message.should == greeting
end

5: Run again and see the step pass

<!-- lang: ruby -->
 $ cucumber features
    Feature: greeter says hello
      Cucumber的Hello World
    
      Scenario: 对世界打个招呼吧             # features/greeter_says_hello.feature:3
        Given 如该我给了你一个测试的hello       # features/step_definitions/g_steps.rb:7
        When 当我给你一个greet的时候          # features/step_definitions/g_steps.rb:11
        Then 我应该看到 "Hello Cucumber!" # features/step_definitions/g_steps.rb:15
    
    1 scenario (1 passed)
    3 steps (3 passed)
    0m0.003s

6: Repeat 2-5 until green like a cuke

##Summary##

从Hello World可以看出,cucumber还是很强大滴,并且输出和定义对非专业人员(需求啊 Boss啊 客户啊)都很友好,如果是个不爱写文档的程序员,可以考虑用cucumber来代替一下。

BTW:虽然例子是ruby,不过cucumber还支持其他语言的,这就得看自己了。

转载于:https://my.oschina.net/jerrytao/blog/103587

相关文章:

  • 为终端服务用户指定在用户登录时自动启动的程序
  • 语音视频聊天的强大技术支持
  • 使用Google Maps API 做一个简单地图源码
  • 100-99
  • PDFCreator 安装在Win 2008 R2 Server.
  • Animation Showcase
  • iphone应用程序生命周期浅析
  • Stack Gallery View
  • 文件与二进制流互转
  • 编译与链接的问题 gcc -fPIC -shared
  • PHP为什么会被认为是草根语言?
  • 希腊神话确实是人类精神文明的宝藏
  • 2013应届毕业生“人人网”校招应聘总结
  • ABAP SY标签一览表
  • 选择排序与冒泡排序
  • [iOS]Core Data浅析一 -- 启用Core Data
  • Android组件 - 收藏集 - 掘金
  • export和import的用法总结
  • Facebook AccountKit 接入的坑点
  • JavaScript的使用你知道几种?(上)
  • Java小白进阶笔记(3)-初级面向对象
  • jquery cookie
  • 官方解决所有 npm 全局安装权限问题
  • 力扣(LeetCode)22
  • 前端学习笔记之原型——一张图说明`prototype`和`__proto__`的区别
  • 手机app有了短信验证码还有没必要有图片验证码?
  • 我与Jetbrains的这些年
  • 小程序01:wepy框架整合iview webapp UI
  • 赢得Docker挑战最佳实践
  • 远离DoS攻击 Windows Server 2016发布DNS政策
  • 关于Android全面屏虚拟导航栏的适配总结
  • ​力扣解法汇总1802. 有界数组中指定下标处的最大值
  • #QT(智能家居界面-界面切换)
  • (a /b)*c的值
  • (floyd+补集) poj 3275
  • (LeetCode 49)Anagrams
  • (博弈 sg入门)kiki's game -- hdu -- 2147
  • (大众金融)SQL server面试题(1)-总销售量最少的3个型号的车及其总销售量
  • (南京观海微电子)——I3C协议介绍
  • (十) 初识 Docker file
  • (图)IntelliTrace Tools 跟踪云端程序
  • (一)pytest自动化测试框架之生成测试报告(mac系统)
  • (原創) 博客園正式支援VHDL語法著色功能 (SOC) (VHDL)
  • (原創) 人會胖會瘦,都是自我要求的結果 (日記)
  • (最全解法)输入一个整数,输出该数二进制表示中1的个数。
  • ***linux下安装xampp,XAMPP目录结构(阿里云安装xampp)
  • ***检测工具之RKHunter AIDE
  • .NET delegate 委托 、 Event 事件
  • .NET 反射 Reflect
  • .NET/C# 使用 SpanT 为字符串处理提升性能
  • .net操作Excel出错解决
  • /proc/interrupts 和 /proc/stat 查看中断的情况
  • @angular/cli项目构建--Dynamic.Form
  • @param注解什么意思_9000字,通俗易懂的讲解下Java注解
  • [ MSF使用实例 ] 利用永恒之蓝(MS17-010)漏洞导致windows靶机蓝屏并获取靶机权限