3

I want to generate a report from JBehave that just lists the name of each scenario and a PASS/FAIL status. Bonus points if it gives a PASS/FAIL status for a story based on the the scenario results (if all scenarios pass the story 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: Additional logging JBehave shows how to use a StoryReporter to capture pass/fail for individual steps, but going through the interface I can't see how to capture the final status of a single scenario.

A commenter later in the same post mentions that there are several view genaration examples in the source distribution. If someone can give more specific pointers to which of the examples do this that would help too.

Community
  • 1
  • 1
M. Krajnak
  • 97
  • 1
  • 9

1 Answers1

0

The StoryReporter code below should do what you are looking for. It keeps track of each scenario and the pass/fail status of each step in the scenario. If any step fails, then the scenario is failed. If any scenario fails, then the story is marked as failed. At the end of the story it logs 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;
}
Brian S.
  • 363
  • 1
  • 14