Jbehave Ant任务

时间:2014-07-29 15:59:50

标签: ant jenkins jbehave

我想在我的项目中介绍Jbehave,我正准备一个简单的POC。 使用:jbehave 3.9.3,ant 1.9.2,IDE eclipse kepler。 我可以在Eclipse中成功运行测试(我还使用@RunWith(JUnitReportingRunner.class)注释了我的测试类。)

但是,当我尝试通过ant运行相同的操作时,我遇到了一些问题。

这是我正在使用的ant文件:

    <property name="src.dir" value="${basedir}/bdd/jbtest"/>
    <property name="jbehave.version"  value="3.9.3"/>

    <target name="clean">
        <delete dir="target" />
    </target>

    <target name="setup">
        <artifact:dependencies filesetId="dependency.fileset" useScope="test">
            <dependency groupId="org.jbehave" artifactId="jbehave-ant" version="${jbehave.version}"/>           
            <dependency groupId="org.jbehave" artifactId="jbehave-core" version="${jbehave.version}" classifier="resources" type="zip"/>           
            <dependency groupId="org.jbehave.site" artifactId="jbehave-site-resources" version="3.1.1" type="zip"/>           
        </artifact:dependencies>

        <mkdir dir="target" />
        <mkdir dir="target/classes" />
        <mkdir dir="target/lib" />
        <copy todir="target/lib">
            <fileset refid="dependency.fileset" />
            <mapper type="flatten" />
        </copy>
        <!--  copy todir="${src.dir}">
            <fileset dir="../core/src/main/java">       
            </fileset>
        </copy> -->
        <copy todir="target/classes">
            <fileset dir="${src.dir}">
                <include name="**/*.story" />
                <include name="**/*.properties" />
                <include name="**/*.xml" />
            </fileset>
        </copy>



        <path id="story.classpath">
            <fileset dir="${basedir}/lib" includes="**/*.jar" />
            <pathelement location="${basedir}/bin" />
        </path>
        <classloader classpathref="story.classpath" />

        <pathconvert targetos="unix" property="story.classpath.unix" refid="story.classpath">
        </pathconvert>
        <echo>Using classpath: ${story.classpath.unix}</echo>

    </target>

    <target name="compile" depends="setup">
        <javac includeantruntime="false" srcdir="${src.dir}" destdir="bin" debug="on" debuglevel="lines,source" includes="**/*.java,**/*.xml">
            <classpath refid="story.classpath" />
        </javac>
    </target>

    <target name="reports-resources" depends="setup">
        <unzip src="${org.jbehave:jbehave-core:zip:resources}" dest="${basedir}/target/jbehave/view/" />
        <unzip src="${org.jbehave.site:jbehave-site-resources:zip}" dest="${basedir}/target/jbehave/view/" />
    </target>


    <target name="run-stories-as-embeddables" depends="compile, reports-resources">
        <taskdef name="runStoriesAsEmbeddables" classname="org.jbehave.ant.RunStoriesAsEmbeddables" classpathref="story.classpath" />
        <runStoriesAsEmbeddables sourceDirectory="${src.dir}" includes="**/Myjb.java" excludes="**/examples*" batch="false" ignoreFailureInStories="true" ignoreFailureInView="true" generateViewAfterStories="true" 
            systemproperties="java.awt.headless=true,project.dir=${basedir}" />
    </target>


    <target name="run-stories-as-paths" depends="compile, reports-resources" >
        <taskdef name="runStoriesAsPaths" classname="org.jbehave.ant.RunStoriesAsPaths" classpathref="story.classpath" />
        <runStoriesAsPaths sourceDirectory="${src.dir}" 
                includes="**/*.story" batch="false" ignoreFailureInStories="true" ignoreFailureInView="true" generateViewAfterStories="true" 
                systemproperties="java.awt.headless=true,project.dir=${basedir}" 
        >    
        </runStoriesAsPaths>                

    </target>

    <target name="stepdoc" depends="compile">
        <taskdef name="reportStepdocs" classname="org.jbehave.ant.ReportStepdocs" classpathref="story.classpath" />
        <reportStepdocs embedderClass="org.jbehave.examples.core.CoreEmbedder" />

                 <taskdef name="reportRenderer" classname="org.jbehave.ant.ReportRendererTask"  classpathref="story.classpath" />
    <reportRenderer outputDirectory="${basedir}/target/jbehave"
      formats="txt,html" templateProperties="defaultFormats=stats" 
      ignoreFailure="true"/>
    </target>

    <target name="build" depends="run-stories-as-paths,stepdoc" />

</project>

问题#1:我无法指定格式

run-stories-as-paths:
[runStoriesAsPaths] Running stories as paths using embedder Embedder[storyMapper=StoryMapper,storyRunner=StoryRunner,embedderMonitor=AntEmbedderMonitor,classLoader=EmbedderClassLoader[urls=[],parent=java.net.URLClassLoader@1a8fa0f0],embedderControls=UnmodifiableEmbedderControls[EmbedderControls[batch=false,skip=false,generateViewAfterStories=true,ignoreFailureInStories=true,ignoreFailureInView=true,verboseFailures=false,verboseFiltering=false,storyTimeoutInSecs=300,failOnStoryTimeout=false,threads=1]],embedderFailureStrategy=<null>,configuration=org.jbehave.core.configuration.MostUsefulConfiguration@5556d74f,candidateSteps=<null>,stepsFactory=<null>,metaFilters=<null>,systemProperties={java.awt.headless=true, project.dir=D:danielewsjbtest},executorService=<null>,executorServiceCreated=false,storyManager=<null>]
[runStoriesAsPaths] Found story paths: [Example.story, Sample.story]
[runStoriesAsPaths] Processing system properties {java.awt.headless=true, project.dir=D:danielewsjbtest}
[runStoriesAsPaths] System property 'java.awt.headless' set to 'true'
[runStoriesAsPaths] System property 'project.dir' set to 'D:danielewsjbtest'
[runStoriesAsPaths] Using controls UnmodifiableEmbedderControls[EmbedderControls[batch=false,skip=false,generateViewAfterStories=true,ignoreFailureInStories=true,ignoreFailureInView=true,verboseFailures=false,verboseFiltering=false,storyTimeoutInSecs=300,failOnStoryTimeout=false,threads=1]]
[runStoriesAsPaths] Generating reports view to 'D:\daniele\ws\jbtest\target\jbehave' using formats '[]' and view properties '{navigator=ftl/jbehave-navigator.ftl, views=ftl/jbehave-views.ftl, reports=ftl/jbehave-reports-with-totals.ftl, nonDecorated=ftl/jbehave-report-non-decorated.ftl, decorated=ftl/jbehave-report-decorated.ftl, maps=ftl/jbehave-maps.ftl}'
[runStoriesAsPaths] Reports view generated with 0 stories (of which 0 pending) containing 0 scenarios (of which 0 pending)

我没有找到传递格式的方法,就像我在java类中所做的那样,并且忽略了它,因此它不会生成任何报告。

问题#2故事发现异常

当我跑

ant -f jb_ant.xml -lib lib run-stories-as-paths

在上面显示的输出之后,我得到一个异常

BUILD FAILED
D:\daniele\ws\jbtest\jb_ant.xml:74: org.jbehave.core.io.StoryResourceNotFound: Story path 'Example.story' not found by class loader EmbedderClassLoader[urls=[],parent=java.net.URLClassLoader@1a8fa0f0]
    at org.jbehave.core.io.LoadFromClasspath.resourceAsStream(LoadFromClasspath.java:44)
    at org.jbehave.core.io.LoadFromClasspath.loadResourceAsText(LoadFromClasspath.java:29)
    at org.jbehave.core.io.LoadFromClasspath.loadStoryAsText(LoadFromClasspath.java:38)
    at org.jbehave.core.embedder.StoryRunner.storyOfPath(StoryRunner.java:192)
    at org.jbehave.core.embedder.StoryManager.storyOfPath(StoryManager.java:49)
    at org.jbehave.core.embedder.StoryManager.runningStoriesAsPaths(StoryManager.java:101)
    at org.jbehave.core.embedder.StoryManager.runStories(StoryManager.java:78)
    at org.jbehave.core.embedder.Embedder.runStoriesAsPaths(Embedder.java:203)
    at org.jbehave.ant.RunStoriesAsPaths.execute(RunStoriesAsPaths.java:16)
    at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:292)
    at sun.reflect.GeneratedMethodAccessor8.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
    at org.apache.tools.ant.Task.perform(Task.java:348)
    at org.apache.tools.ant.Target.execute(Target.java:435)
    at org.apache.tools.ant.Target.performTasks(Target.java:456)
    at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1393)
    at org.apache.tools.ant.Project.executeTarget(Project.java:1364)
    at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
    at org.apache.tools.ant.Project.executeTargets(Project.java:1248)
    at org.apache.tools.ant.Main.runBuild(Main.java:851)
    at org.apache.tools.ant.Main.startAnt(Main.java:235)
    at org.apache.tools.ant.launch.Launcher.run(Launcher.java:280)
    at org.apache.tools.ant.launch.Launcher.main(Launcher.java:109)

令我感到困惑,因为jbehave刚刚在执行任务时列出了发现的故事。

如果这有助于诊断问题,我可以发布Java类和故事。

任何想法我做错了什么?

1 个答案:

答案 0 :(得分:0)

过了一会儿,我找到了这些问题的答案:

  • 通过实现StoryFinder类(下面的代码)解决了org.jbehave.core.io.StoryResourceNotFound异常
  • 要生成报告,我必须实现一个Embedder类(下面的代码)并在那里指定格式(显然没有办法将它直接传递给Ant任务。

希望这可以帮助其他有类似问题的人。

<强> StoryFinder

package jbtest;


import org.jbehave.core.io.StoryFinder;
import java.util.*;

public class MyStoryFinder extends StoryFinder {
    @Override
    protected List<String> scan(String basedir, List<String> includes,
            List<String> excludes) {
        //List<String> defaultStories = super.scan(basedir, includes, excludes);
        //String myStories = System.getProperty("com.sarang.stories");
        return Arrays.asList("jbtest/Example.story,jbtest/Sample.story".split(","));
    }
}

<强>嵌入器

package jbtest;

import java.text.SimpleDateFormat;
import java.util.Properties;
import org.jbehave.core.configuration.Configuration;
import org.jbehave.core.configuration.MostUsefulConfiguration;
import org.jbehave.core.embedder.Embedder;
import org.jbehave.core.embedder.EmbedderControls;
import org.jbehave.core.io.CodeLocations;
import org.jbehave.core.io.LoadFromClasspath;
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.jbehave.core.steps.SilentStepMonitor;

public class MyEmbedder extends Embedder {

    @Override
    public EmbedderControls embedderControls() {
        return new EmbedderControls().doIgnoreFailureInStories(true).doIgnoreFailureInView(true);
    }

    @Override
    public Configuration configuration() {
        Class<? extends MyEmbedder> embedderClass = this.getClass();      
           Properties viewResources = new Properties();
            viewResources.put("decorateNonHtml", "true");

           return new MostUsefulConfiguration()
             .useStoryReporterBuilder(  
                 new StoryReporterBuilder()
                 .withDefaultFormats()
                .withViewResources(viewResources).withFormats(Format.CONSOLE, Format.HTML, Format.XML)
                .withFailureTrace(true)
                ) ;
    }

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

}

然后我按如下方式修改了Ant任务:

    <target name="run-stories-as-paths" depends="compile, reports-resources" >
    <taskdef name="runStoriesAsPaths" classname="org.jbehave.ant.RunStoriesAsPaths" classpathref="story.classpath" />
    <runStoriesAsPaths 
            sourceDirectory="${src.dir}" 
            includes="**/*.story" 
            ignoreFailureInStories="true" 
            ignoreFailureInView="true" 
            generateViewAfterStories="true" 
            storyfinderclass="jbtest.MyStoryFinder"
        verbosefailures="true"
        embedderclass="jbtest.MyEmbedder"
            >    
    </runStoriesAsPaths>
    </target>