2015-12-08 106 views
4

在香草黄瓜,通过在步骤定义puts呼叫输出任何东西被捕获作为“测试输出”,并相应地格式化,如下面的输出例如:如何在Cucumber JVM中捕获标准输出(如Cucumber Ruby的输入)?

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输出中输入了“输出”的条目,因此我想象一下,必须有一种方法来写入该领域。

回答

3

看起来这可以通过写入表示当前正在运行的场景的对象来完成。

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实现那样缩进。尽管如此,这似乎是我能找到的最接近的等价物。

相关问题