2014-01-06 45 views
1

我們正試圖將CSV到黃瓜獲得的CSV文件的好處,但網上調查,當我看到這個功能,但只有「Import scenario outline 'examples' from CSV?」沒有正式的文件。導入CSV如黃瓜測試數據?

我不知道這是否涉及到任何的結論呢。它目前是否在Cucumber中使用CSV的內置方式?

如果當前沒有內置的方式來導入CSV,並且我必須編寫自己的解析方法,我想我的問題是,在步驟定義中,如何將變量與定義場景?例如:

Scenario: do something 
    Given I eat number as <number> cucumbers 
    and the cucumber is produced at date as <date> 
    When the cucumber is expired 
    Then I should have diarrhea 

data.csv

number,date 
1,2012-01-01 
1,2012-11-03 

在steps.rb,如果我這樣做:

CSV.foreach("path/to/data.csv") do |row| 
    ... 

如何我row.number`row.date`映射到數\日期在功能文件?

回答

3

爲了在黃瓜使用實例功能,我認爲你必須做一些顯著元編程,可能會降低你的特點可維護性和可讀性。我可能會通過包裝你當前步驟一個步驟的定義,像這樣實現:

Scenario: do something 
    Given I load some data 
    Then expired cucumbers should give me diarrhea 

和定義步驟定義

Then 'I load some data' do 
    @data = [] 
    CSV.foreach("path/to/data.csv", headers: true) do |row| 
    @data << row 
    end 
end 

Then 'expired cucumbers should give me diarrhea' do 
    @data.each do |row| 
    step %Q|I eat number as #{row[:number]} cucumbers| 
    step %Q|the cucumber is produced at date as #{row[:date]}| 
    step %Q|the cucumber is expired| 
    setp %Q|I should have diarrhea| 
    end 
end 

與此唯一的問題是,如果一個方案失敗,可能需要額外的調試步驟來確定哪一個失敗。由於這些步驟或多或少都是在引擎蓋下運行的。您可以通過從步驟定義打印行STDOUT做到這一點很容易地:

Then 'expired cucumbers should give me diarrhea' do 
    @data.each do |row| 
    puts "row", row.inspect 
    step %Q|I eat number as #{row[:number]} cucumbers| 
    step %Q|the cucumber is produced at date as #{row[:date]}| 
    step %Q|the cucumber is expired| 
    setp %Q|I should have diarrhea| 
    end 
end 

這應該給你一些提示,其中行給你的麻煩。

Erata:我理解您希望能夠保留與功能分開的數據表,以便像項目經理這樣的人可以提出邊緣案例,然後讓這些案例違反代碼的行爲。但我幾乎更願意允許他們編輯github上的功能,並允許CI運行他們添加到示例表中的示例,而不是執行此方法。在任何一種情況下,它都可能適合迴歸,但可能真的很難開發代碼。這種測試類型的目標是http://fitnesse.org/,這可能是您的靈感項目。