Cucumber-JVM ant task - java

How do i tell ant to execute all cucumber tests (features, implementations) in a folder?
I'm stuck using this example
<target name="runcukes" depends="compile-test">
<mkdir dir="target/cucumber-junit-report"/>
<java classname="cucumber.cli.Main" fork="true" failonerror="false" resultproperty="cucumber.exitstatus">
<classpath refid="classpath"/>
<arg value="--format"/>
<arg value="junit:target/cucumber-junit-report/allcukes.xml"/>
<arg value="--format"/>
<arg value="pretty"/>
<arg value="--format"/>
<arg value="html:target/cucumber-html-report"/>
<arg value="--glue"/>
<arg value="cucumber.examples.java.helloworld"/>
<arg value="src/test/resources"/>
</java>
<junitreport todir="target/cucumber-junit-report">
<fileset dir="target/cucumber-junit-report">
<include name="allcukes.xml"/>
</fileset>
<report format="frames" todir="target/cucumber-junit-report"/>
</junitreport>
<fail message="Cucumber failed">
<condition>
<not>
<equals arg1="${cucumber.exitstatus}" arg2="0"/>
</not>
</condition>
</fail>
</target>

Usage: java cucumber.cli.Main [options] [ [FILE|DIR][:LINE[:LINE]*] ]+
Options:
-g, --glue PATH Where glue code (step definitions and hooks) is loaded from.
-f, --format FORMAT[:OUT] How to format results. Goes to STDOUT unless OUT is specified.
Available formats: junit, html, pretty, progress, json, json-pretty.
-t, --tags TAG_EXPRESSION Only run scenarios tagged with tags matching TAG_EXPRESSION.
-n, --name REGEXP Only run scenarios whose names match REGEXP.
-d, --dry-run Skip execution of glue code.
-m, --monochrome Don't colour terminal output.
--dotcucumber Where to write out runtime information.
-v, --version Print version.
-h, --help You're looking at it.
In your case - change the <arg value="src/test/resources"/> to directory you want to run against. Note that you can specify multiple individual files and directories space-separated.

I also faced same problem and solved by declaring top package name like in you case the package name is "cucumber.examples.java.helloworld" Just declare only cucumber like this
<arg value="--glue"/>
<arg value="cucumber"/>
in you ant task. It's working at me end.

Instead of using the build file provided along with the samples from github, you can write your own build file.
Try running the Junit test file from ant using ant's Junit task. Set "format" of "#Cucumber.Options" annotation to point to the directory having your features.
Example
To run the Junit test,add the following target to your build file,
<junit printsummary="yes" failureproperty="junit.failure">
<classpath refid="id_of_the_path_to_find_JARS/.class files"></classpath>
<test name="com.example.YourTestClass" haltonfailure="no" outfile="result" >
<formatter type="plain"/>
<formatter type="xml"/>
</test>
</junit>
The Juint test specified in the <test name="com.example.YourTestClass" haltonfailure="no" outfile="result" > will be executed. The YourTestClass.java will look like this...
import cucumber.api.junit.Cucumber;
import org.junit.runner.RunWith;
#RunWith(Cucumber.class)
#Cucumber.Options(format = {"html:target/cucumber-html","junit:target/cucumber-junit/Webpage.xml"},features = "src/test/features")
public class YourTestClass {
}
All the feature files kept in src/test/features will run while executing the build file

Related

Ant exec command argument dealing with multiple files

I am using ant exec command to implement the less utility to view the source code of a bunch of .java files. (I know that there are other ways to do this like using concat)
So the call ant view works if I specify only one file:
<target name="view">
<exec executable="less" dir=".">
<arg value="Main.java"/>
</exec>
</target>
But if I change my code to <arg value="*.java"/> to view all files, it actually searches for a file named *.java.
Apparently I can put a bunch of arg's for each file, but is there a way to do this with one arg ?
The * glob is expanded by the shell on Unix-likes, that's why less doesn't do it itself.
Apart from <exec> there is <apply> which works on a resource collection:
<apply executable="less" dir="." parallel="true" relative="true">
<fileset dir="." includes="*.java"/>
</apply>
You can use foreach which requires ant-contrib
<target name="view">
<foreach target="call-less" param="file">
<fileset dir="${src}" includes="**/*.java" />
</foreach>
</target>
<target name="call-less">
<exec executable="less">
<arg value="${file}" />
</exec>
</target>

Ant/Grails test-app with cobertura causes: CreateProcess error=206, The filename or extension is too long

We have an ant build that is creating our production ear. It invokes Grails scripts. We also have the code-coverage plugin added to our Grails project to do instrumentation. Everything works fine when running it using just "grails test-app", but when we invoke it through Ant, we get the following error: CreateProcess error=206, The filename or extension is too long. I've seen this error in the past, and I believe it is usually due to the classpath being too long. The server that this is running on is a 64bit Windows 2008 server.
The ant target looks like this:
<target name="test-cobertura"
description="--> Run a Grails applications unit and integration tests with code coverage">
<!-- <echo message="GRAILS_HOME ${myenv.GRAILS_HOME}"/> -->
<exec executable="${grails}" failonerror="false" resultproperty="resultCode">
<arg value="test-app"/>
<arg value="-xml"/>
<arg value="-Xms512m"/>
<arg value="-Xmx1024m"/>
<arg value="-coverage"/>
</exec>
<junitreport todir="target/test-reports/html">
<fileset dir="target/test-reports/">
<include name="TEST-*.xml"/>
</fileset>
<report format="frames" todir="target/test-reports/html"/>
</junitreport>
<zip basedir="target/test-reports/html" destfile="test/reports/testReport.zip"/>
<fail status="1" message="Some tests failed">
<condition>
<equals arg1="${resultCode}" arg2="1"/>
</condition>
</fail>
<zip basedir="target/test-reports/cobertura" destfile="test/reports/cobertura.zip"/>
</target>
If we turn the coverage piece off, it runs fine, so it's definitely the cobertura piece that is causing the error.
We've been playing around with it but cannot figure anything out at this point. Any help would be greatly appreciated.

Ant execute arbitrary programs and terminate at end of junit task

I am attempting to write unit tests against a series of RMI interfaces / implementations and am relying on Ant + Junit to facilitate these tests. The main problem is that I need to have the rmiregistry start up at the beginning of my Junit task and close down at the end of execution whether that be failure,error, or success. Ant script:
<project>
<target name="test">
<javac srcdir="Test/src" destdir="Test/bin" />
<junit fork="true" printsummary="true">
<batchtest>
<fileset dir="Test/test">
<include name="**/*Test.*"/>
</fileset>
</batchtest>
</junit>
</target>
</project>
Basically, just set haltonerror="false" and failonerror="false" for the junit task.
This way junit won't fail due to tests. Starting and stopping is done via the exec task. I used taskkill here to kill the rmiregistry.exe.
<exec executable="start"> <--your start command here -->
<arg value="rmiregistry"/>
</exec>
<junit fork="true" printsummary="true" haltonerror="false" failonerror="false" errorproperty="juniterrors" failureproperty="junitfailures">
...
</junit>
<exec executable="taskkill"> <--your shutdown command here -->
<arg value="/IM"/>
<arg value="rmiregistry.exe"/>
</exec>
<fail if="juniterrors"/> <!-- ant can fail now, if desired -->
<fail if="junitfailures"/>
Another option is to use try/catch from ant-contrib, but I don't think this is needed here.

Eclipse Equinox, how to configure auto load the bundles in plugin folder

I've followed http://www.eclipse.org/equinox/documents/quickstart-framework.php but it seems to be old and not valid.
There is no such bundles as described org.eclipse.update.configurator_3.2.100.jar
I tried with the org.eclipse.equinox.simpleconfigurator_1.0.200.v20100503, but doesn't work.
Anyone can tell me how to make Equinox auto load the bundles inside plugins folder?
Simplest approach would be to use Apache Felix File Install. It works just fine with Equinox, you only need to put File Install configuration parameters into the configuration/config.ini. Note though that if you launch Equinox via launcher JAR or via binary, the working directory would be parent of configuration/ or plugins/ directory.
Excerpt from our project config.ini:
# Start File Install itself
osgi.bundles=reference\:file\:org.apache.felix.fileinstall_3.1.0.jar#1\:start
# The name of the directory to watch
felix.fileinstall.dir=./plugins
# A regular expression to be used to filter file names
# We have all bundles in plugins/ directory, this regexp
# forbids monitoring bundles that are started via osgi.bundles property
felix.fileinstall.filter=^(?!org.apache.felix.fileinstall|org.eclipse.osgi).*
# Determines if File Install waits felix.fileinstall.poll milliseconds before doing an initial scan or not.
felix.fileinstall.noInitialDelay=true
# Not sure why we have this...
felix.fileinstall.start.level=2
Other possible solution would be to use Eclipse P2. It is much more advanced and powerful, though I find it quite difficult to use.
Good thing is that if your application is agnostic to the way bundles are provisioned (and it should be this way), you can always change your mind later.
Here is the fragment from my automated eclipse installer written in ant.
This installs all features from the custom update site. The code is 'as is', but I sure would have liked to have something like this to guide me when I wrote it.
This script also uses antcontrib extension to ant. Antcontrib tasks are have 'ac:' namespace prefix
Hope this helps.
<property name="real.eclipse.home" location="${eclipse.home}/eclipse"/>
<property file="${real.eclipse.home}/configuration/config.ini" prefix="ECLIPSE_CONFIG"/>
<property name="eclipse-plugins.dir" location="${real.eclipse.home}/plugins"/>
<path id="newest.equinox.launcher-library.path.id">
<dirset dir="${eclipse-plugins.dir}">
<include name="org.eclipse.equinox.launcher.*"/>
</dirset>
</path>
<property name="equinox.launcher-library.full-path" refid="newest.equinox.launcher-library.path.id"/>
<basename property="equinox.launcher-library.dir" file="${equinox.launcher-library.full-path}"/>
<echo message="equinox.launcher-library.dir='${equinox.launcher-library.dir}'"/>
<path id="newest.equinox.launcher.path.id">
<fileset dir="${eclipse-plugins.dir}">
<include name="org.eclipse.equinox.launcher_*.jar"/>
</fileset>
</path>
<property name="equinox.launcher.jar" refid="newest.equinox.launcher.path.id"/>
<basename property="equinox.launcher.jar.basename" file="${equinox.launcher.jar}"/>
<echo message="equinox.launcher.jar='${equinox.launcher.jar}'"/>
<java jar="${equinox.launcher.jar}"
fork="true"
failonerror="true"
>
<arg value="-consolelog"/>
<arg value="-application"/>
<arg value="org.eclipse.equinox.p2.director"/>
<arg value="-repository"/>
<arg value="http://${repository.server}/custom-update-site"/>
<arg value="-list"/>
<redirector
logError="true"
outputproperty="features.list"
>
<outputfilterchain>
<linecontains>
<contains value="feature.group="/>
</linecontains>
<replaceregex pattern="(.*feature\.group)=.*$" replace="\1"/>
</outputfilterchain>
</redirector>
</java>
<ac:for list="${features.list}" delimiter="${line.separator}" trim="true" param="feature">
<sequential>
<ac:if>
<isset property="feature.comma.list"/>
<then>
<ac:var name="feature.comma.list" value="${feature.comma.list},#{feature}"/>
</then>
<else>
<property name="feature.comma.list" value="#{feature}"/>
</else>
</ac:if>
</sequential>
</ac:for>
<echo message="Found following features to install"/>
<echo message="${features.list}"/>
<java jar="${equinox.launcher.jar}"
fork="true"
failonerror="true"
>
<arg value="-consolelog"/>
<arg value="-application"/>
<arg value="org.eclipse.equinox.p2.director"/>
<arg value="-repository"/>
<arg value="http://${repository.server}/custom-update-site"/>
<arg value="-destination"/>
<arg file="${real.eclipse.home}"/>
<arg value="-installIU"/>
<arg value="${feature.comma.list}"/>
<arg value="-profile"/>
<arg value="${ECLIPSE_CONFIG.eclipse.p2.profile}"/>
</java>
P.S. For its usefulness and complexity Eclipse P2 is surely one of the most underdocumented features.
In your eclipse installation folder you have the file bundles.info, for example:
eclipse-3.6.1/configuration/org.eclipse.equinox.simpleconfigurator/bundles.info
You can modify the file to add any bundle you want, and also the start level. But the simplest method of adding bundles to an eclipse installation is to add them to the "dropins" folder. This will lead to an automatic modification of the bundle.info file.

Ant macrodef compilation task

I have a compiler (and language) I am building that is normally invoked thus:
java -jar nc.jar \
-p some/referenced/package.nc \
-p framework.nc \
source1.ns source2.ns sourceN.ns \
-o output/package.nc
I'd like to include a task in my ANT build file that invokes the compiler to compile the standard library and all test cases, but specifying each separate compiler invocation as a <java> task is painful:
<target name="framework" depends="compiler" description="Build the n framework">
<!-- More compile steps -->
<java jar="nc.jar" fork="true">
<arg value="-p"/>
<arg path="../nframework/build/n.core.nc"/>
<arg path="../nframework/n/debug/DebugPrint.ns"/>
<arg path="../nframework/n/debug/Trace.ns"/>
<arg value="-o"/>
<arg path="../nframework/build/n.debug.nc"/>
</java>
<!-- More compile steps -->
</target>
I would like to create an ANT task of some sort that can simplify this into something like:
<target name="framework" depends="compiler" description="Build the n framework">
<!-- More compile steps -->
<nc output="../nframework/build/n.debug.nc">
<link-package path="../nframework/build/n.core.nc"/>
<src>
<fileset dir="../nframework/n/debug" includes="**/*.ns"/>
</src>
</nc>
<!-- More compile steps -->
</target>
To this end, I tried macrodef:
<macrodef name="nc">
<attribute name="output"/>
<element name="link-package"/>
<element name="src"/>
<sequential>
<java jar="nc.jar" fork="true">
<arg value="-p"/>
<!-- This doesn't do what I want -->
<link-package/>
<!-- Neither does this -->
<src/>
<arg value="-o"/>
<arg path="#{output}"/>
</java>
</sequential>
</macrodef>
I've tried several variations on the above, but each errors out with something like:
/home/jwarner/code/nlang/nc/build.xml:55: java doesn't support nested "fileset" element.
Is there a way to do this without extending ANT itself? Alternatively, would it be fairly easy to add an ant task to my compiler? I'm not terribly picky about the syntax of the final <nc> task.
I have had a similar problem in the past where the out-of-the-box Ant tasks didn't quite do what I wanted them to do. I found that it was very easy to write my own Ant task.
The documentation is concise but does a good job of explaining what you need to do.
http://ant.apache.org/manual/develop.html#writingowntask

Categories

Resources