[每周软件]:Cucumber:Hello World

简介

OSChina 的链接 Cucumber

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

Book

The Rspec Book

The Cucumber Book

Before Start

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


gem install cucumber

Step By Step

1: 描述行为

文件 “./features/greeter_says_hello.feature”


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

然后在.目录里运行一下


 $ 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进来


# -*- 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

运行一下看看结果


$ 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


# -*- 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


 $ 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

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


# -*- 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


 $ 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还支持其他语言的,这就得看自己了。

你可能感兴趣的:([每周软件]:Cucumber:Hello World)