2012-01-16 58 views
1

我目前正在嘗試將黃瓜和水豚一起用於Web應用的一些集成測試。黃瓜動態加載數據表

有一個測試,我只想點擊網頁應用程序的所有(或大部分)頁面,查看是否沒有錯誤返回。我希望能夠看到哪些頁面無法正常工作。

我認爲,方案輪廓將是最好的辦法,所以我以這種方式開始:

Scenario Outline: Checking all pages pages 

    When I go on the page <page> 
    Then the page has no HTTP error response 

    Examples: 
     | page      | 
     | "/resource1"    | 
     | "/resource2"    | 
     ... 

我現在有82頁,並且工作正常。

但是,我發現這種方法不可維護,因爲可能會刪除新的資源和資源。

更好的方法是從某處加載表中的數據(解析索引頁的HTML,數據庫等)。

但我沒有弄清楚如何做到這一點。

我碰到一個article about table transformation但我無法弄清楚如何在場景大綱中使用這種轉換。

有什麼建議嗎?

好,因爲存在一些混淆。如果你看看上面的例子。所有我想要做的是改變它,這樣的表幾乎是空的:

Scenario Outline: Checking all pages pages 

    When I go on the page <page> 
    Then the page has no HTTP error response 

    Examples: 
    | page      | 
    | "will be generated"   | 

然後我想補充一個轉型看起來是這樣的:

Transform /^table:page$/ do 
    all_my_pages.each do |page| 
    table.hashes << {:page => page} 
    end 
    table.hashes 
end 

我指定的轉換在同文件,但它沒有被執行,所以我假設轉換不適用於Scenario輪廓。

+0

是的,Transform只能在不包含大綱表的步驟參數表上運行。 – mschneider 2012-01-25 13:54:53

回答

3

黃瓜確實是該任務的錯誤的工具,你應該在功能方面描述的功能。如果你想以編程方式描述行爲,你應該使用rspec或test-unit之類的東西。

另外,您的場景步驟應該是描述性的,並且像書面文本一樣專用,而不是像編程語言中使用的抽象短語。他們不應該包含「附帶的細節」,比如資源的確切url或它的id。

請仔細閱讀http://blog.carbonfive.com/2011/11/07/modern-cucumber-and-rails-no-more-training-wheels/,看http://skillsmatter.com/podcast/home/refuctoring-your-cukes

關於你對「插入表格」的問題,是有可能的,如果你 平均增加額外的排它,事實上,你可以做任何你用它喜歡。 變換塊的結果完全取代原始表格。

Transform /^table:Name,Posts$/ do 
    # transform the table into a list of hashes 
    results = table.hashes.map do |row| 
    user = User.create! :name => row["Name"] 
    posts = (1..row["Posts"]).map { |i| Post.create! :title => "Nr #{i}" } 
    { :user => user, :posts => posts } 
    end 
    # append another hash to the results (e.g. a User "Tim" with 2 Posts) 
    tim = User.create! :name => "Tim" 
    tims_posts = [Post.create! :title => "First", Post.create! :title => "Second"] 
    results << { :user => tim, :posts => tims_posts } 
    results 
end 

Given /^I have Posts of the following Users:$/ do |transformation_results| 
    transformation_results.each do |row| 
    # assing Posts to the corresponding User 
    row[:user].posts = row[:posts] 
    end 
end 

你可以與場景結合本大綱是這樣的:

Scenario Outline: Paginate the post list of an user at 10 
    Given I have Posts of the following Users: 
    | Name | Posts | 
    | Max | 7  | 
    | Tom | 11 | 
    When I visit the post list of <name> 
    Then I should see <count> posts 
Examples: 
    | name | count | 
    | Max |  7 | 
    | Tom | 10 | 
    | Tim |  2 | 

這應該demonstarte爲什麼「加入」行的表,可能不是最好的做法。

請注意,這是不可能擴大示例代碼中的表內:

Scenario Outline: Paginate the post list of an user at 10 
    Given I have Posts of the following Users: 
    | Name | Posts  | 
    | <name> | <existing> | # won't work 
    When I visit the post list of <name> 
    Then I should see <displayed> posts 
Examples: 
    | name | existing | displayed | 
    | Max |  7 |   7 | 
    | Tom | 11 |  10 | 
    | Tim |  2 |   2 | 
+0

在我提供的示例中,即使一條路徑失敗,測試也會繼續。之後,我可以看到測試失敗的參數。所以我的代碼在任何方面都不是這樣。我現在唯一需要的就是生成表格的轉換。 – leifg 2012-01-23 09:04:37

+0

請詳細說明「表的生成位置」。你想生成小黃瓜的源代碼?或者你想在接受表格的步驟中使用Outline標籤(如)?第一個不應該完成,第二個是不可能的。 – mschneider 2012-01-23 16:52:05

+0

我喜歡做的是類似於這個例子(在我的情況下添加數據的表,或插入不可能?):http://www.claytonlz.com/index.php/2010/01/cucumber-table - 轉換/但我想在場景大綱中使用它。 – leifg 2012-01-24 08:59:20

0

我認爲更好的方法是使用不同的工具,只是爲了抓取您的網站,並檢查是否沒有錯誤返回。假設您正在使用Rails

您可能會考慮的工具是:Tarantula

https://github.com/relevance/tarantula

我希望幫助:)

+0

感謝您的建議,但是有幾個原因導致我無法使用Tarantula(集成測試與應用程序分離,應用程序不支持Rails3應用程序)。除此之外,在我不想僅抓取網站的其他場景中,生成數據表格可能很有用。 – leifg 2012-01-16 10:50:13

0

快速黑客是改變的例子收集代碼,並使用紅寶石的EVAL運行自定義紅寶石功能覆蓋默認收集實例數據,這裏是代碼: generate-dynamic-examples-for-cucumber

缺點:需要改變scenario_outline.rb文件。

1

對於動態加載數據的具體情況,這裏有一個建議:

  1. ,A級,比方說PageSets,有方法,例如all_pages_in_the_sitemap_errorcountdeveloping_countries_errorcount

  2. 一步,上面寫着類似

    Given I am on the "Check Stuff" page 
    Then there are 0 errors in the "developing countries" pages 
    

Then there are 0 errors in "all pages in the sitemap" 

Then步驟串"developing countries"轉換成一個方法名developing_countries_errorcount並試圖調用它PageSets類。在這種情況下,步驟期望所有_errorcount方法返回一個整數。返回像地圖這樣的數據結構爲編寫簡潔的動態步驟提供了許多可能性。

對於更多靜態數據,我們發現YAML對於使我們的測試自我記錄和自我驗證非常有用,並且幫助我們去除難以維護的文字,例如「5382739」,我們都忘記了三週後。

YAML格式爲便於閱讀,如有必要可評論(它通常不是。)

不應該這樣寫:

Given I am logged in as "[email protected]" 
And I select the "History" tab 
Then I can see 5 or more "rows of history" 

我們可以改爲寫:

Given I am logged in as "a user with at least 5 items of history" 
When I select the "History" tab 
Then I can see 5 or more "rows of history" 

在文件logins.yaml ....

a member with at least 5 items of history: 
    username: [email protected] 
    password: WalRus 

我們使用YAML來保存與成員,供應商,政策等各種實體相關的數據集...該列表正在不斷增加:列表正在不斷增加:

在文件test_data.yaml ...

a member who has direct debit set up: 
    username: [email protected] 
    password: WalRus 
    policyId: 5382739 
    first name: Jack 
    last name: Robinson 
    partner's first name: Sally 
    partner's last name: Fredericks 

如果您需要驗證文本,那麼也值得看看YAML的multi-line text facilities。雖然這對於自動化測試並不常見,但它有時可能很有用。