Blog

Blog

PHODAL

cucumber——有趣的ruby bdd框架

这里只是因为看到当前的项目中有这个神奇的东西,所以试着去写点东西来记录这个过程,结果是有趣的。相比之下,更像是一种有趣的自然语言,看起来有种文档的感觉。放一下官方示例的一个结果,感觉很容易阅读,似乎还有中文,不过我想是用不到了。

[WARNING] MultiJson is using the default adapter (ok_json).We recommend loading a different JSON library to improve performance.
Feature: Hello World Feature
  In order to ensure that my installation works
  As a Developer
  I want to run a quick Cucumber test

  Scenario: Hello World Scenario      # features/basic.feature:6
    Given The Action is Hello         # features/setp_definitions/step_steps.rb:3
    When The Subject is World         # features/setp_definitions/step_steps.rb:7
    Then The Greeting is Hello, World # features/setp_definitions/step_steps.rb:11

1 scenario (1 passed)
3 steps (3 passed)
0m0.002s

==看上去是不是有点奇怪,这个就是结果。

cucumber

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

如官网所说,Making BDD fun,确实有点意思。

官网给出的七个步骤

  • Describe behaviour in plain text
  • Write a step definition in Ruby
  • Run and watch it fail
  • Write code to make the step pass
  • Run again and see the step pass
  • Repeat 2-5 until green like a cuke
  • Repeat 1-6 until the money runs out

使用cucumber

那么,就我们开始这个有趣的过程吧。

安装

 gem install cucumber

省去一些废话,这里就只用这些来表达。

使用cucumber

最后项目结果如下所示

└── features
    ├── basic.feature
    └── setp_definitions
        └── step_steps.rb

下面是basic.feature的最后结果:

Feature: Hello World Feature
  In order to ensure that my installation works
  As a Developer
  I want to run a quick Cucumber test

  Scenario: Hello World Scenario
    Given The Action is Hello
    When The Subject is World
    Then The Greeting is Hello, World

看上去和我们的运行结果有点相似,我们看看step_steps.rb

require 'rspec/expectations'

Given /The Action is ([A-z]*)/ do |action|
  @action = action
end

When /The Subject is ([A-z]*)/ do |subject|
  @subject = subject
end

Then /The Greeting is (.*)/ do |greeting|
  greeting.should == "#{@action}, #{@subject}"
end

似乎这个看不出什么结果,等等,我们修改一下basic.features。

于是我们很愉快的将,Hello改为Hello1

[WARNING] MultiJson is using the default adapter (ok_json).We recommend loading a different JSON library to improve performance.
Feature: Hello World Feature
  In order to ensure that my installation works
  As a Developer
  I want to run a quick Cucumber test

  Scenario: Hello World Scenario       # features/basic.feature:6
    Given The Action is Hello          # features/step_definitions/step_steps.rb:3
    When The Subject is World          # features/step_definitions/step_steps.rb:7
    Then The Greeting is Hello1, World # features/step_definitions/step_steps.rb:11
      expected: "Hello, World"
           got: "Hello1, World" (using ==) (RSpec::Expectations::ExpectationNotMetError)
      ./features/step_definitions/step_steps.rb:12:in `/The Greeting is (.*)/'
      features/basic.feature:9:in `Then The Greeting is Hello1, World'

Failing Scenarios:
cucumber features/basic.feature:6 # Scenario: Hello World Scenario

1 scenario (1 failed)
3 steps (1 failed, 2 passed)
0m0.002s

这个就是我们需要的过程,红->绿,不过这里把这反了过来。

关于我

Github: @phodal     微博:@phodal     知乎:@phodal    

微信公众号(Phodal)

围观我的Github Idea墙, 也许,你会遇到心仪的项目

QQ技术交流群: 321689806
comment

Feeds

RSS / Atom

最近文章

关于作者

Phodal Huang

Engineer, Consultant, Writer, Designer

ThoughtWorks 技术专家

工程师 / 咨询师 / 作家 / 设计学徒

开源深度爱好者

出版有《前端架构:从入门到微前端》、《自己动手设计物联网》、《全栈应用开发:精益实践》

联系我: h@phodal.com

微信公众号: 最新技术分享

标签