Integration Test With Cucumber And Webrat

Post on 19-Jan-2015

5.135 views 1 download

Tags:

description

my talk about cucumber and webrat on ruby tuesday

Transcript of Integration Test With Cucumber And Webrat

Integration Testwith Cucumber and Webrat

Kang-min Liugugod@gugod.org

Integration Test

• 整合測試

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

• Web App: 以瀏覽器進行

整合測試範例: twitter

輸入

輸入

輸出

整合測試範例: twitter

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

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

3. 按下「update」按鈕

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

Webrat 版本

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

response.body.should =~ /lorem ipsum/

WebratBrowser emulation / control

Webrat 能力

• 拜訪網址

• 填寫表單

• 點擊連結或按鈕

• 取得目前網頁的內容

Webrat 能力

• 無 javascript

• 無 css

Webrat 能力

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

• Selenium

• Watir

Webrat Core API• visit

• click_link

• fill_in

• check

• uncheck

• choose

• select

• attach_file

• click_button

Webrat Core API

• visit, click_link

• get + assert_response :success

• click_button

• submit form + assert_response :success

• submit form default values if any

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

?

Goods

• 輕量

• 能自動驗證頁面 HTML

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

Cucumber

• BDD (Behavior Driven Development) Tool

• Ruby implementation

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

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

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”

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”

說明

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”

說明

本文

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 – 期望

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

?

Goods...

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

• 測試失敗時

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

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

Goods...

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

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

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”

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