![](/img/trans.png)
[英]Want to execute Background only once in Cucumber feature files for multiple scenarios
[英]Why is the output from a Cucumber Background shown only once?
我在 Cucumber Background
部分放置了一個調試打印語句。
由於Background
對每個場景執行一次,我希望每個場景都能從Background
看到一次輸出。 但是,輸出僅顯示一次。 為什么?
這是一個簡單的例子,說明了我的問題:
計算器/功能/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 ***
兩次(因為Background
被執行了兩次),但它只顯示了一次。 為什么?
在 Cucumber Background
步驟中打印的消息僅打印一次,因為 Cucumber 在執行步驟時捕獲標准輸出並將其打印在 Cucumber 的控制下。 在Background
步驟中打印的消息與步驟名稱一起打印:僅一次,在輸出的開頭。
看到打印的消息每次時間的方式Background
運行是相同的,因此,作為每次都看步驟名稱的方式Background
運行。 已經有一個問題和答案,但它不適用於當前版本的 Cucumber(我有 2.3.3),所以我寫了一個新的答案來說明如何打印Background
在每次之前打印的所有內容場景。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.