3

I'm trying to set up JBehave for testing web services.

Template story is running well, but I can see in JUnit Panel only Acceptance suite class execution result. What I want is to see execution result for each story in suite and for each step in story like it is shown in simple JUnit tests or in Thucydides framework.

Here is my acceptance suite class: so maybe I Haven't configured something, or either I have to notate my step methods some other way, but I didn't find an answer yet.

package ***.qa_webservices_testing.jbehave;

import java.util.Arrays;
import java.util.List;
import java.util.Properties;

import org.jbehave.core.Embeddable;
import org.jbehave.core.configuration.Configuration;
import org.jbehave.core.configuration.MostUsefulConfiguration;
import org.jbehave.core.io.CodeLocations;
import org.jbehave.core.io.LoadFromClasspath;
import org.jbehave.core.io.StoryFinder;
import org.jbehave.core.junit.JUnitStories;
import org.jbehave.core.parsers.RegexPrefixCapturingPatternParser;
import org.jbehave.core.reporters.CrossReference;
import org.jbehave.core.reporters.Format;
import org.jbehave.core.reporters.StoryReporterBuilder;
import org.jbehave.core.steps.InjectableStepsFactory;
import org.jbehave.core.steps.InstanceStepsFactory;
import org.jbehave.core.steps.ParameterConverters;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ***.qa_webservices_testing.jbehave.steps.actions.TestAction;

/**
 * suite class.
 */
public class AcceptanceTestSuite extends JUnitStories {
    private static final String CTC_STORIES_PATTERN = "ctc.stories";
    private static final String STORY_BASE = "src/test/resources";
    private static final String DEFAULT_STORY_NAME = "stories/**/*.story";
    private static final Logger LOGGER = LoggerFactory.getLogger(AcceptanceTestSuite.class);

    private final CrossReference xref = new CrossReference();

    public AcceptanceTestSuite() {
        configuredEmbedder()
            .embedderControls()
            .doGenerateViewAfterStories(true)
            .doIgnoreFailureInStories(false)
            .doIgnoreFailureInView(true)
            .doVerboseFailures(true)
            .useThreads(2)
            .useStoryTimeoutInSecs(60);
    } 

    @Override
    public Configuration configuration() {
        Class<? extends Embeddable> embeddableClass = this.getClass();
        Properties viewResources = new Properties();
        viewResources.put("decorateNonHtml", "true");
        viewResources.put("reports", "ftl/jbehave-reports-with-totals.ftl");
        // Start from default ParameterConverters instance
        ParameterConverters parameterConverters = new ParameterConverters();
        return new MostUsefulConfiguration()
            .useStoryLoader(new LoadFromClasspath(embeddableClass))
            .useStoryReporterBuilder(new StoryReporterBuilder()
                .withCodeLocation(CodeLocations.codeLocationFromClass(embeddableClass))
                .withDefaultFormats()
                .withViewResources(viewResources)
                .withFormats(Format.CONSOLE, Format.TXT, Format.HTML_TEMPLATE, Format.XML_TEMPLATE)
                .withFailureTrace(true)
                .withFailureTraceCompression(false)
                .withMultiThreading(false)               
                .withCrossReference(xref)) 
            .useParameterConverters(parameterConverters)                     
            // use '%' instead of '$' to identify parameters
            .useStepPatternParser(new RegexPrefixCapturingPatternParser(
                            "%")) 
            .useStepMonitor(xref.getStepMonitor());
    }

    @Override
    protected List<String> storyPaths() {
        String storiesPattern = System.getProperty(CTC_STORIES_PATTERN);
        if (storiesPattern == null) {
            storiesPattern = DEFAULT_STORY_NAME;
        } else {
            storiesPattern = "**/" + storiesPattern;
        }
        LOGGER.info("will search stories by pattern {}", storiesPattern);
        List<String> result = new StoryFinder().findPaths(STORY_BASE, Arrays.asList(storiesPattern), Arrays.asList(""));
        for (String item : result) {
            LOGGER.info("story to be used: {}", item);
        }
        return result;
    }

    @Override
    public InjectableStepsFactory stepsFactory() {
        return new InstanceStepsFactory(configuration(), new TestAction());
    }
}

my test methods look like:

Customer customer = new cutomer();

@Given ("I have Access to Server")
public void givenIHaveAccesToServer() {
    customer.haveAccesToServer();
}

So they are notated only by JBehave notations.

The result returned in Junit panel is only like here (I yet have no rights to post images):

enter image description here

RichardTheKiwi
  • 105,798
  • 26
  • 196
  • 262
  • Found a solution of problem myself. I need to extend Runner class from jbehave and then notate my suite with @RunWith(ReportingRunner.class) – Dmytro Grablov Apr 04 '13 at 11:39

2 Answers2

2

You should try this open source library:

https://github.com/codecentric/jbehave-junit-runner

It does exactly what you ask for :)

AndreasEK
  • 363
  • 1
  • 10
2

Yes, the codecentric runner works very nicely. https://github.com/codecentric/jbehave-junit-runner Eclipse Juno and the CodeCentric Runner.

Lance Kind
  • 949
  • 12
  • 32
  • I was wondering if the plug in execute tests and creating junit report only from IDE , or I also can run it by maven ? – Igal Jul 08 '15 at 08:24
  • 1
    When talking about Maven, it's assumed you want headless (no ui) execution & a report that can be shown via a build server like Jenkins, etc. So there are two workflows for test execution: one for the IDE so the developers can quickly produce tests and features, & one for maven so automatic test execution is asynchronously from the development team. Most teams I see use the Surefire plugin to execute their junit/BDD tests. https://maven.apache.org/surefire/maven-surefire-plugin/ – Lance Kind Jul 10 '15 at 18:34
  • thanks for your input, you are right , I do want a headless execution ... I've tried to work with surefire plug in as well , but from some reason it didn't work for me , probably due to wrong plug in set up – Igal Jul 12 '15 at 08:57
  • I've tried the surefire and the failsafe plugin, the thing is that I cannot configure them properly to work with Jbehave plug in , for example , to set up story time out... any chance and you have an example of pom file where you configure both , the jbehave plug in and the surefire ? Thanks – Igal Jul 14 '15 at 06:53