2015-12-08 35 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實現那樣縮進。儘管如此,這似乎是我能找到的最接近的等價物。

相關問題