29
Integration Test with Cucumber and Webrat Kang-min Liu [email protected]

Integration Test With Cucumber And Webrat

Embed Size (px)

DESCRIPTION

my talk about cucumber and webrat on ruby tuesday

Citation preview

Page 1: Integration Test With Cucumber And Webrat

Integration Testwith Cucumber and Webrat

Kang-min [email protected]

Page 2: Integration Test With Cucumber And Webrat

Integration Test

• 整合測試

• 軟體主要功能的黑箱測試

• Web App: 以瀏覽器進行

Page 3: Integration Test With Cucumber And Webrat

整合測試範例: twitter

Page 4: Integration Test With Cucumber And Webrat
Page 5: Integration Test With Cucumber And Webrat

輸入

Page 6: Integration Test With Cucumber And Webrat

輸入

輸出

Page 7: Integration Test With Cucumber And Webrat

整合測試範例: twitter

1. 以瀏覽器開啟 http://twitter.com/home

2. 在上方「status」一欄中輸入文字

3. 按下「update」按鈕

4. 確認:剛剛輸入的文字應出現在頁面上

Page 8: Integration Test With Cucumber And Webrat

Webrat 版本

visit "http://twitter.com/home"fill_in "status", :with => "lorem ipsum"click_button "update"

response.body.should =~ /lorem ipsum/

Page 9: Integration Test With Cucumber And Webrat

WebratBrowser emulation / control

Page 10: Integration Test With Cucumber And Webrat

Webrat 能力

• 拜訪網址

• 填寫表單

• 點擊連結或按鈕

• 取得目前網頁的內容

Page 11: Integration Test With Cucumber And Webrat

Webrat 能力

• 無 javascript

• 無 css

Page 12: Integration Test With Cucumber And Webrat

Webrat 能力

• 底層能與其他系對接,能控制瀏覽器

• Selenium

• Watir

Page 13: Integration Test With Cucumber And Webrat

Webrat Core API• visit

• click_link

• fill_in

• check

• uncheck

• choose

• select

• attach_file

• click_button

Page 14: Integration Test With Cucumber And Webrat

Webrat Core API

• visit, click_link

• get + assert_response :success

• click_button

• submit form + assert_response :success

• submit form default values if any

Page 15: Integration Test With Cucumber And Webrat

Webrat + RSpecdescribe "tweeting" do it "should show my tweets" do visit "/home" fill_in "status", :with => "lorem ipsum" click_button "update" response.body.should =~ /lorem ipsum/ endend

Page 16: Integration Test With Cucumber And Webrat

?

Page 17: Integration Test With Cucumber And Webrat

Goods

• 輕量

• 能自動驗證頁面 HTML

• 先保證網頁親和力,再加強前端介面

Page 18: Integration Test With Cucumber And Webrat

Cucumber

• BDD (Behavior Driven Development) Tool

• Ruby implementation

• 以簡單語句描述系統行為

• 將簡單語句對應到測試程式碼

Page 19: Integration Test With Cucumber And Webrat

Feature: Update my status In order to keep friends posted As a friendly person I post my status to twitter

Scenario: Update my status Given I go to “/home” When I fill in “status” with “lorem ipsum” And I click “send” Then I should see “lorem ipsum”

Page 20: Integration Test With Cucumber And Webrat

Feature: Update my status In order to keep friends posted As a friendly person I post my status to twitter

Scenario: Update my status Given I go to “/home” When I fill in “status” with “lorem ipsum” And I click “send” Then I should see “lorem ipsum”

說明

Page 21: Integration Test With Cucumber And Webrat

Feature: Update my status In order to keep friends posted As a friendly person I post my status to twitter

Scenario: Update my status Given I go to “/home” When I fill in “status” with “lorem ipsum” And I click “send” Then I should see “lorem ipsum”

說明

本文

Page 22: Integration Test With Cucumber And Webrat

Scenario: Update my status Given I go to “/home” When I fill in “status” with “lorem ipsum” And I click “send” Then I should see “lorem ipsum”

Given – 前提When – 情境Then – 期望

Page 23: Integration Test With Cucumber And Webrat

Given /^I go to "(.*)"$/ do |url| visit urlend

When /^I fill in "(.*)" with "(.*)"$/ do |field,value| fill_in field, :with => valueend

When /^I click "(.*)"$/ do |link| click_link(link)end

Then /^I should see "(.*)"$/ do |text| response.body.should =~ /#{text}/mend

Page 24: Integration Test With Cucumber And Webrat

?

Page 25: Integration Test With Cucumber And Webrat

Goods...

• 每個步驟自然而然成為可重複使用的單元,鮮少需要重構測試

• 測試失敗時

• 馬上能知道失敗步驟及情境

• 接連的測試步驟便不會執行

Page 26: Integration Test With Cucumber And Webrat

Goods...

• 先由自然語言出發去描述軟體行為

• 方便先用這段文字當做溝通用

Page 27: Integration Test With Cucumber And Webrat

Scenario: Update my status Given I go to “/home” When I fill in “status” with “lorem ipsum” And I click “send” Then I should see “lorem ipsum”

Page 28: Integration Test With Cucumber And Webrat

describe "tweeting" do it "should show my tweets" do visit "/home" fill_in "status", :with => "lorem ipsum" click_button "update" response.body.should =~ /lorem ipsum/ endend