问题
When I print output straight from the Given/When/Then statements of cucumber, it prints in a nice blue text, however if I call into any other function and print within there, it prints in red (and not indented)
Sample feature:
Feature: Foo
Scenario: Foo2
Given something
Then something else
Sample step_def:
Given /^something$/ do
puts "Pretty blue"
end
class AnyClass
def doSomething
puts "Scary red"
end
end
Then /^something else$/ do
AnyClass.new.doSomething
end
In the below sample output, "Scary red" is red, not indented at all, and actually printed before the cucumber step (The "Then" clause). "Pretty blue" is blue, indented appropriately, and outputted after the cucumber step.
Feature: Foo
Scenario: Foo2
Given something
Pretty blue
Scary red
Then something else
1 scenario (1 passed)
2 steps (2 passed)
0m0.001s
I am new to Ruby & cucumber so I am not even positive those are the appropriate tags for this question. Is there a way to make output from methods called within other classes will match the pretty formatting?
Edit: Some more searching has shown that puts from within steps are handled differently (as in, if you format it differently, they are included) than puts from other functions (which get excluded from other formatters). So perhaps the question is, how does Ruby intercept the puts from the steps? I suspect they're implementing their own puts which is part of the step's class then.
回答1:
When writing the grammar for cucumber steps, cucumber has overridden (the grammars) puts to be colored and buffered. The color depends on whether the step passes or not, and the buffering happens because it won't color the output until everything has run (so it knows which color to print).
Passing self from within a grammar step and calling that objects puts gets the behavior I was after.
The red was a side effect of my default font color.
ie:
After /Some Condition/ do
some_object.test_something(self)
end
class SomeObject
def test_something(obj)
obj.puts "SomeText"
end
end
来源:https://stackoverflow.com/questions/15034767/cucumber-colored-output-changes-depending-on-where-i-print-text