Get a script skipped / failed report by script using JBehave

I want to generate a JBehave report that simply displays the name of each script and the PASS / FAIL status. Bonus points if it gives PASS / FAIL status for a story based on scenario results (if all scenarios go through history passes). Something like:

PASS:  Story: Build a widget.
    PASS:  Scenario:  Build a normal widget.
    PASS:  Scenario:  Build a custom widget.
FAIL:  Story: Test a widget.
    PASS:  Scenario:  Test a normal widget.
    FAIL:  Scenario:  Test a custom widget.

      

Text is preferred, but I can work with other formats.

This post: JBehave's optional log shows how to use the StoryReporter to capture pass / fail for individual steps, but going through the interface I don't see how to capture the final status of a single script.

It is mentioned in the comments later in the same post that there are some examples of decomposition examples in the source distribution. If anyone can provide more specific pointers which of the examples will do this, that would help too.

+3


source to share


1 answer


The StoryReporter code below should do what you are looking for. It tracks every scenario and the pass / fail status of every step in the scenario. If any step fails, the script is not executed. If any script fails, the story is marked as unsuccessful. At the end of the story, he records the results.



public class MyStoryReporter implements org.jbehave.core.reporters.StoryReporter {
private Story runningStory;
private boolean runningStoryStatus;
private String runningScenarioTitle;
private boolean runningScenarioStatus;
private List<ScenarioResult> scenarioList;
private Log log = LogFactory.getLog(this.getClass());

private class ScenarioResult {
    public String title;
    public boolean result;

    public ScenarioResult(String title, boolean result) {
        this.title = title;
        this.result = result;
    }
}

public void beforeStory(Story story, boolean b) {
    runningStory = story;
    runningStoryStatus = true;
    scenarioList = new ArrayList<>();
}

public void afterStory(boolean b) {
    String storyPrefix = runningStoryStatus ? "PASS: STORY: " : "FAIL: STORY: ";
    log.info(storyPrefix + runningStory.getName() + ".");

    String scenarioPrefix;
    for (ScenarioResult scenario : scenarioList) {
        scenarioPrefix = scenario.result ? "  PASS: SCENARIO: " : "  FAIL: SCENARIO: ";
        log.info(scenarioPrefix + scenario.title + ".");
    }
}

public void beforeScenario(String s) {
    runningScenarioTitle = s;
    runningScenarioStatus = true;
}

public void afterScenario() {
    scenarioList.add(new ScenarioResult(runningScenarioTitle, runningScenarioStatus));
    runningStoryStatus = runningStoryStatus && runningScenarioStatus;
}

public void failed(String s, Throwable throwable) {
    runningScenarioStatus = false;
}

      

0


source







All Articles