我在黄瓜Background
部分放了一个调试print语句。
由于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
执行两次),但它只显示一次。为什么呢?
答案 0 :(得分:1)
以黄瓜enable_if
步骤打印的消息只打印一次,因为Cucumber在执行步骤时捕获标准输出并在Cucumber的控制下打印。以Background
步骤打印的消息与步骤名称一起打印:仅在输出开头处打印一次。
每次Background
运行时查看打印消息的方式都是相同的,因此,每次Background
运行时都可以看到步骤名称。已经有一个问题和答案,但它不适用于当前版本的Cucumber(我有2.3.3),所以我写了一个新问题的答案显示how to print everything that Background
prints before every scenario。