在vanillaCucumber中,步骤定义中调用puts
输出的任何内容都被捕获为"测试输出",并相应地格式化,如以下输出示例所示:
Feature: A simple thing
Scenario: A simple scenario # features/simple.feature:3
Given I do a step # features/steps/step.rb:1
This text is output with puts
正如您在上面看到的,它以"漂亮"的输出格式缩进非常有用。在JSON格式中,它甚至以结构化的方式捕获:
"keyword": "Scenario",
"name": "A simple scenario",
"line": 3,
"description": "",
"id": "a-simple-thing;a-simple-scenario",
"type": "scenario",
"steps": [
{
"keyword": "Given ",
"name": "I do a step",
"line": 4,
"output": [
"This text is output with puts"
],
}
],
上面是用一个琐碎的特征文件和如下步骤定义生成的:
Given(/^I do a step$/) do
puts 'This text is output with puts'
end
在Java中实现Cucumber步骤时,是否有一个等效的函数可以用来以相同的方式捕获输出?打印到System.out
会导致绕过捕获机制,就像在Ruby中使用STDOUT.puts
一样。
与Ruby Cucumber的许多示例不同,我还没有看到任何使用此功能的Cucumber JVM示例,但Cucumber JVM的JSON输出中显然有一个"output"条目,因此我认为必须有一种方法来写入该字段。
这似乎可以通过写入表示当前运行场景的对象来实现。
public class StepDefs {
private Scenario scenario;
/* Need to capture the scenario object in the instance to access it
* in the step definition methods. */
@Before
public void before(Scenario scenario) {
this.scenario = scenario;
}
@Given("^I do a step$")
public void iDoAStep() {
scenario.write("This text is output with scenario.write");
}
}
与Ruby中的puts
调用一样,这会在JSON输出文件中捕获。它也在"漂亮"的输出中着色,尽管它不像Ruby实现那样缩进。尽管如此,这似乎是我能找到的最接近的等价物。