对Cucumber的一点理解

Cucumber是BDD测试中的明星。

关键字

Cucumber中的几个关键字:

feature, scenario, given, when, then, and, but

简单说说这些关键字的含义。

Feature:你可以把它看作是一个user story

scenario用于描述一个简单的场景。

一个feature可以有1个或者多个scenario。

一个scenario可以有3~8个steps。

其中,steps的定义类似于Ruby中method的定义。

step中的5个关键字:

  • Given : 表示事件发生之前的状态, preconditions。
  • When: 表示事件本身,比如用户push 了一个button
  • Then:表示事件发生后的预期结果,比如push了一个button,要跳转到某个特定的页面
  • And & But:用来延续之前的steps

用例

看个例子:

Feature: user can manually add movie

Scenario: Add a movie
    Given I am on the RottenPotatoes home page
    When I follow "Add new movie"
    Then I should be on the Create New Movie page
    when I fill in "Title" with "Men In Black"
    And I select "PG-13" from "Rating"
    And I press "Save changes"
    Then I shoud be on the RottenPotatoes home page
    And I should see "Men in Black"

这里测试一个feature,用户可以手动添加电影,添加的电影会在首页显示,表明添加成功。

具体的steps是这样的:

Given:用户在烂番茄主页,这是precondition

When:用户点击新增电影,这是事件本身

Then:用户应该会跳转到新建电影的页面,这是事件发生后的预期结果

When:电影有title字段,用户将“Men in Black” 填入到title中,这是事件本身

And:用户在星级中选择了“PG-13”,这是上一步的延续

And:用户按下“保存”,继续延续上一步

Then:用户应该跳转回烂番茄首页,这是事件发生后的预期结果

And:用户应该看到“Men in Black”,这是上一步结果的延续

此外,用cucumber去测试的一个好处是,它会提示你如何做可以让测试通过。

再看一个实例,看看如何DRY:P。

用户在TMDb首页搜索movie,可能出现的两种结果: 搜索到该影片(happy path),没有搜索到(bad path)

测试文件长这样:

Feature: User can add movie by searching in The Movie
    Database(TMDb)
    As a movie fan
    so that I can add new movies without manual tedium
    I want to add movies by looking up their details in TMDb
Scenario: Try to add nonexistent movie(sad path)
    Given I am on the RottenPotatoes home page
    Then I should see "Search TMDb for a movie"
    When I fill in "Search Terms" with "Movie that Does Not Exist"
    And I press "Search TMDb"
    Then I should be on the RottenPotatoes home page
    And I should see "Movie That Does Not Exist" was not found in TMDb
Scenario: Try to add nonexistent movie(happy path)
    Given I am on the RottenPotatoes home page
    Then I should see "Search TMDb for a movie"
    When I fill in "Search Terms" with "Inception"
    And I press "Search TMDb"
    Then I should be on the RottenPotatoes home page
    And I should see "Inception"

其中,sad path ,happy path都有6个 steps,两者的前两个steps是一样的,how to DRY it?

这里用到background,我的理解是background类似于RSpec中的before。

我们重构一下:

Feature: User can add movie by searching in The Movie
    Database(TMDb)
    As a movie fan
    so that I can add new movies without manual tedium
    I want to add movies by looking up their details in TMDb
Background: Start from the Search form on the home page
            Given I am on the RottenPotatoes home page
            Then I should see "Search TMDb for a movie"
Scenario: Try to add nonexistent movie(sad path)
    When I fill in "Search Terms" with "Movie that Does Not Exist"
    And I press "Search TMDb"
    Then I should be on the RottenPotatoes home page
    And I should see "Movie That Does Not Exist" was not found in TMDb
Scenario: Try to add nonexistent movie(happy path)
    When I fill in "Search Terms" with "Inception"
    And I press "Search TMDb"
    Then I should be on the RottenPotatoes home page
    And I should see "Inception"

OK,大功告成。