3

我正在尝试设置 JBehave 来测试 Web 服务。

模板故事运行良好,但我只能在 JUnit 面板中看到验收套件类执行结果。我想要的是查看套件中每个故事的执行结果以及故事中每个步骤的执行结果,就像它在简单的 JUnit 测试或 Thucydides 框架中显示的那样。

这是我的验收套件类:所以也许我还没有配置一些东西,或者我必须以其他方式注释我的步骤方法,但我还没有找到答案。

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());
    }
}

我的测试方法如下:

Customer customer = new cutomer();

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

因此它们仅由 JBehave 符号表示。

Junit 面板返回的结果只有这里(我还没有发布图像的权利):

在此处输入图像描述

4

2 回答 2

2

你应该试试这个开源库:

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

它完全符合您的要求:)

于 2013-04-16T09:30:43.263 回答
2

是的,以代码为中心的运行程序非常好用。 https://github.com/codecentric/jbehave-junit-runner Eclipse Juno 和 CodeCentric Runner。

于 2013-05-06T04:40:49.567 回答