Cucumber入门之_argument
a)?Multi-line Text
在feature文件中,我們可以嵌入多行文本(multi-line string)作為參數,我們需要用一對三個雙引號把我們的文本括起來。《The RSpec Book》一書中的示例如下:?
?feature文件:
1 Scenario: pending implementation2 Given a file named "example_without_block_spec.rb" with:
3 """
4 describe "an example" do
5 it "has not yet been implemented"
6 end
7 """
8 When I run "spec example_without_block_spec.rb"
9 Then the exit code should be 0
10 And the stdout should include
11 """
12 Pending:
13 an example has not yet been implemented \(Not Yet Implemented\)
14 .\/example_without_block_spec.rb:2
15 Finished in ([\d\.]*) seconds
16 1 example, 0 failures, 1 pending
17 """
在這個scenario中,Given和And(Then)兩個步驟中都使用了多行文本字符串作為它們的參數。這可以帶給我們很多靈活性,因為我們可以更精確的描述輸入或輸出數據在一個文件中的顯示情況。
關于字符串的縮進,取決于第一個雙引號的位置,所以第四行的describle和第6行的end是左對齊,第5行的it會縮進兩個空格。??
step_definitions:
1 Given /^a file named "([^"]*)" with:$/ do |filename, text|2 puts "Given:\n#{filename}"
3 puts "Given:\n#{text}"
4 end
5 When /^I run "([^"]*)"$/ do |filename|
6 puts "When:\n#{filename}"
7 end
8 Then /^the exit code should be (\d+)$/ do |number|
9 puts "Then:\n#{number}"
10 end
11 And /^the stdout should include$/ do |text|
12 puts "And:\n#{text}"
13 end
在這個step definitions文件中,我只是簡單的把參數打印出來,當然我們也可以在這里做一些復雜的操作或處理。匹配step的正則表達式不關心這些多行文本字符串,它會在在step語句的最后一個字符處結束。
Cucumber會把多行文本作為最后一個塊參數傳遞給step definition。如在上面的step definition文件的第1行中,filename這個塊參數的值會由正則式捕捉到,而text會得到多行文本的值。
?results:
1 Given:2 example_without_block_spec.rb
3
4 Given:
5 describe "an example" do
6 it "has not yet been implemented"
7 end
8
9 When:
10 spec example_without_block_spec.rb
11
12 Then:
13 0
14
15 And:
16 Pending:
17 an example has not yet been implemented \(Not Yet Implemented\)
18 .\/example_without_block_spec.rb:2
19 Finished in ([\d\.]*) seconds
20 1 example, 0 failures, 1 pending
這里只是簡單的輸出了多行文本,大家注意下縮進的問題,只有第6行的it空了兩個空格,其它都是頂格。因為其它的每行開頭都和第一個雙引號在同一垂直位置。
?
另外一個例子:http://asymmetrical-view.com/2011/06/02/cucumber-gherkin-and-multiline-arguments.html?
?
b)?Tables in Steps
? Cucumber支持在steps中傳遞表格數據,這種方式常用于Given和Then這兩個步驟中。看《The RSpec Book》中的示例:
feature文件:
1 Feature: test table in steps2
3 Scenario: three of a kind beats two pair
4 Given a hand with the following cards:
5 | rank | suit |
6 | 2 | H |
7 | 2 | S |
8 | 2 | C |
9 | 4 | D |
10 | A | H |
11 And another hand with the following cards:
12 | rank | suit |
13 | 2 | H |
14 | 2 | S |
15 | 4 | C |
16 | 4 | D |
17 | A | H |
18 Then the first hand should beat the second hand
在這個feature文件中的Given和And(Given)步驟中,定義了兩個表格。當cucumber碰到一個step(Given or Then)的下一行是以“|”開頭時,cucumber會解析它和它之后的所有以"|"開頭行,并把這些單元格里面的數據存儲到Cucumber::Ast::Table 對象里面。我們可以使用hashes()方法得到一個哈希數組。
數組里的每一個哈希都使用表格中的第一行作為key,如:
1 [2 { :rank => '2', :suit => 'H' },
3 { :rank => '2', :suit => 'S' },
4 { :rank => '4', :suit => 'C' },
5 { :rank => '4', :suit => 'D' },
6 { :rank => 'A', :suit => 'H'}
7 ]
和多文本參數一樣,cucumber會把Cucumber::Ast::Table 作為最后一個塊參數傳遞給step definition。
step definitions文件:
2 # table is a Cucumber::Ast::Table
3 p table
4 p table.hashes
5 end
6 And /^another hand with the following cards:$/ do |table|
7 # table is a Cucumber::Ast::Table
8 table.hashes.each do |value|
9 puts "#{value}"
10 end
11 end
12 Then /^the first hand should beat the second hand$/ do
13 #do nothing
14 end
在這個文件里,做了些簡單的操作。在Given中使用P來監視參數table和table.hashes。在And(Given)中則會用each方法輸出參數。
?
results:
1 | rank | suit |2 | 2 | H |
3 | 2 | S |
4 | 2 | C |
5 | 4 | D |
6 | A | H |
7
8 [{"rank"=>"2", "suit"=>"H"}, {"rank"=>"2", "suit"=>"S"}, {"rank"=>"2", "suit"=>"C"}, {"rank"=>"4", "suit"=>"D"}, {"rank"=>"A", "suit"=>"H"}]
9
10 {"rank"=>"2", "suit"=>"H"},
11
12 {"rank"=>"2", "suit"=>"S"},
13
14 {"rank"=>"4", "suit"=>"C"},
15
16 {"rank"=>"4", "suit"=>"D"},
17
18 {"rank"=>"A", "suit"=>"H"},
看上面的結果:
1.第1-6行是p table的輸出結果,跟定義的一樣,是一個表格形式的數據集合。
2.第8行是p table.hashes的輸出結果,是一個哈希數組。
3.第9-18行是迭代輸出哈希數組。
c)?Scenario Outlines
這個Scenario Outlines挺有用的。當我們一個feature文件里有多個scenario的時候,而且每個scenario的步驟都差不多,只是測試的數據不同。在這種情況下我們就可以使用Scenario Outlines,定義一個scenario,然后把測試數據參數化就可以了。繼續看《The RSpec Book》中的示例:
feature 文件:
1 Feature: test scenario outline2
3 Scenario: test one
4 Given the secret code is 1234
5 When I guess 1234
6 Then the mark should be bbbb
7
8 Scenario: test two
9 Given the secret code is 1234
10 When I guess 1235
11 Then the mark should be bbb
12
13 Scenario: test three
14 Given the secret code is 1234
15 When I guess 1236
16 Then the mark should be bbb
在這個feature文件里面,我們有三個scenario(可能會有更多),它們的區別僅僅是測試數據不同。這樣寫有很多缺點,比如不易讀,違反DRY原則等等。
Scenario outliness可以幫我們解決這個問題,讓我們只定義一次scenario,然后使用占位符來代替可能不斷在改就的值。然后我們可以把那些變化的值以一樣表格的形式組織起來。
改進后的feature文件
2 Given the secret code is "<code>"
3 When I guess "<guess>"
4 Then the mark should be "<mark>"
5
6 Scenarios: all numbers correct
7 | code | guess | mark |
8 | 1234 | 1234 | ++++ |
9 | 1234 | 1243 | ++-- |
10 | 1234 | 1423 | +--- |
11 | 1234 | 4321 | ----|
這樣編寫feature文件,使得我們很容易閱讀,同時也給我們了一個大綱,讓我們清晰的知道這個scenario的什么用。
關鍵字Scenarios(Examples)定義了一個輸入數據的table,這樣cucumber會處理除開第一行(在這是第7行)的每第行數據,
此例中總共執行四次, 所以實際上會有四個scenario。
?
這種參數化的方式也可以使用在multi-line text 和 table in steps中,繼續看示例:
feature文件:
1 Feature: Test scenario outline2
3 Scenario Outline:
4 Given a discount of <discount>
5 When I order the following book:
6 | title | price |
7 | Healthy eating for programmers | <price> |
8 Then the statement should read:
9 """
10 Statement for David
11 Total due: <total>
12 """
13
14 Scenarios:
15 | discount | price | total |
16 | 10% | $29.99 | $26.99 |
17 #| 15% | $29.99 | $25.49|
在這個文件中,Given,When和Then分別使用了三種不同的方式傳遞參數,但都使用的參數化:
Given:參數化
When:table in steps + 參數化
Then: ??multi-line text + 參數化
?
step definition文件:
1 Given /^a discount of (\d+)%$/ do |discount|2 p discount
3 end
4
5 When /^I order the following book:$/ do |table|
6 # table is a Cucumber::Ast::Table
7 p table
8 end
9 Then /^the statement should read:$/ do |string|
10 p string
11 end
在該文件中,只是簡單的使用p輸出了參數,結果如下。
?
results:
1 "10"2
3 | title | price |
4 | Healthy eating for programmers | $29.99 |
5
6 "Statement for David\nTotal due: $26.99"
占位符的地方被實際值代替后,得到上面的結果。
?
?
注:本文大部分內容來自《The RSpec Book》一書,僅共學習之用,如需要了解更多內容,請看原文。
轉載于:https://www.cnblogs.com/puresoul/archive/2012/03/06/2382135.html
總結
以上是生活随笔為你收集整理的Cucumber入门之_argument的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: TroubleshootingGuide
- 下一篇: 分布式版本控制系统Mercurial(一