2016-05-08 7 views
1

私は、キュウリのBackgroundセクションにデバッグプリントステートメントを入れました。キュウリの背景からの出力が1回だけ表示されるのはなぜですか?

Backgroundはシナリオごとに1回実行されるため、シナリオごとにBackgroundの出力を1回見ると予想されました。ただし、出力は1回だけ表示されます。どうして?ここで

は私の質問を説明する簡単な例です:

電卓/機能/ adding.feature:

Feature: Adding 

Background: 
    Given calculator is ready 

Scenario: Add two numbers 
    Given the input "2" and "2" 
    When the calculator is run 
    Then the output should be "4" 

Scenario: Add another two numbers 
    Given the input "2" and "3" 
    When the calculator is run 
    Then the output should be "5" 

電卓/機能/ step_definitions/calculator_steps.rb:

counter = 0 

Given(/^calculator is ready$/) do 
    puts "*** background ***" 
    counter += 1 
end 

Given(/^the input "([^"]*)" and "([^"]*)"$/) do |x1, x2| 
    @x1 = x1 
    @x2 = x2 
end 

When(/^the calculator is run$/) do 
    @output = `ruby calc.rb #{@x1} #{@x2}` 
end 

Then(/^the output should be "([^"]*)"$/) do |expected_output| 
    expect(@output).to eq(expected_output) 
    puts "counter=#{counter}" 
end 

計算機/ calc.rb:

x1 = ARGV[0].to_i 
x2 = ARGV[1].to_i 

print ("#{x1+x2}") 
シナリオが実行されたときにここで

が出力されます:

$ cucumber 
Feature: Adding 

    Background:     # features/adding.feature:3 
    Given calculator is ready # features/step_definitions/calculator_steps.rb:3 
    *** background *** 

    Scenario: Add two numbers  # features/adding.feature:6 
    Given the input "2" and "2" # features/step_definitions/calculator_steps.rb:8  
    When the calculator is run # features/step_definitions/calculator_steps.rb:13 
    Then the output should be "4" # features/step_definitions/calculator_steps.rb:17 
     counter=1 

    Scenario: Add another two numbers # features/adding.feature:11 
    Given the input "2" and "3"  # features/step_definitions/calculator_steps.rb:8 
    When the calculator is run  # features/step_definitions/calculator_steps.rb:13 
    Then the output should be "5" # features/step_definitions/calculator_steps.rb:17 
     counter=2 

2 scenarios (2 passed) 
8 steps (8 passed) 
0m0.094s 

私は(Backgroundが2回実行されているため)を2回行*** background ***が見込まが、それは一度だけ表示されます。どうして?

答えて

1

キュウリBackgroundのステップで印刷されたメッセージは、キュウバーがステップを実行して標準出力をキャプチャしてキュウリの制御下で印刷するため、1回だけ印刷されます。 Backgroundのステップで印刷されたメッセージは、ステップ名とともに出力されます。出力の最初に1回だけ表示されます。

Backgroundが実行されるたびに印刷されたメッセージを表示する方法は、したがって、Backgroundが実行されるたびにステップ名を確認する方法と同じです。すでに質問と回答がありましたが、現在のバージョンのCucumber(私は2.3.3があります)ではうまくいきませんので、その質問に対する新しい回答をhow to print everything that Background prints before every scenarioと書いています。

+0

あなたがCucumber 2.3.3で説明した解決策は私には当てはまりましたが、これはデバッグのために通常必要となるため、gemをローカルに変更することは問題ありません。 –

関連する問題