use*_*471 5 java cucumber cucumber-jvm
在vanilla Cucumber中,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
Run Code Online (Sandbox Code Playgroud)
如上所见,它以“漂亮”输出格式缩进。在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"
],
}
],
Run Code Online (Sandbox Code Playgroud)
上面的代码是用一个简单的特征文件和一个步骤定义生成的,如下所示:
Given(/^I do a step$/) do
puts 'This text is output with puts'
end
Run Code Online (Sandbox Code Playgroud)
在Java中实现Cucumber步骤时,是否可以使用相同的功能来捕获此输出?打印到System.out结果会绕过捕获机制,就像STDOUT.puts在Ruby中使用一样。
与许多Ruby Cucumber的示例不同,我还没有看到任何使用此功能的Cucumber-JVM示例,但是显然Cucumber-JVM的JSON输出中有一个用于“输出”的条目,因此我想一定有一种写入该字段的方法。
看起来可以通过写入代表当前正在运行的方案的对象来完成。
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");
}
}
Run Code Online (Sandbox Code Playgroud)
与putsRuby中的调用一样,它会在JSON输出文件中捕获。尽管它不像Ruby实现那样缩进,但它还在“漂亮”输出中着色。但是,这似乎是我能找到的最接近的等效项。