Running jacoco programmatically

891 views
Skip to first unread message

rahul...@gmail.com

unread,
Jul 26, 2016, 6:04:47 AM7/26/16
to JaCoCo and EclEmma Users
Hi
I'm currently working on a project which executes test cases on a remote device's built-in API's.

Now I have got a requirement to do a code coverage and the firs thing came to my mind was JaCoCo.

But when I checked the test framework I found these issues

1. Test cases are compiled with ant build and created a bundle which will be installed on the remote device via telnet.
so the test cases are ran when telnet starts the bundle instead of regular java -jar xxx.jar format
so I'm unable to add javvagent during this part.

2. The alternative way I found was to run javaagent at device bootup while device calls built-in API's for initialization.
but now the boot directory is flagged as read-only and and I'm unable to add javaagent in boot file.

is there any other way to add javaagents? can I call jacoco programmatically to get coverage results?

Evgeny Mandrikov

unread,
Jul 26, 2016, 7:40:17 AM7/26/16
to JaCoCo and EclEmma Users, rahul...@gmail.com
Hi,

Probably you can simply use offline instrumentation instead of on-the-fly instrumentation with agent - see http://www.eclemma.org/jacoco/trunk/doc/offline.html

rahul...@gmail.com

unread,
Jul 26, 2016, 9:36:57 AM7/26/16
to JaCoCo and EclEmma Users, rahul...@gmail.com
On Tuesday, July 26, 2016 at 5:10:17 PM UTC+5:30, Evgeny Mandrikov wrote:
> Hi,
>
>
> Probably you can simply use offline instrumentation instead of on-the-fly instrumentation with agent - see http://www.eclemma.org/jacoco/trunk/doc/offline.html
>

Hi,

Thank you for the quick reply
I did try this but unable to get the coverage report and couldn't find any example of how this is done (all I could find is for maven build)

is there any tutorial/example (non maven)projects available?

Evgeny Mandrikov

unread,
Jul 26, 2016, 9:39:07 AM7/26/16
to jac...@googlegroups.com, rahul...@gmail.com
--
You received this message because you are subscribed to a topic in the Google Groups "JaCoCo and EclEmma Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/jacoco/2GDpJXWsTQQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to jacoco+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jacoco/e9117db5-2dff-4693-9cf0-a619a1562bbc%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

rahul...@gmail.com

unread,
Jul 26, 2016, 9:51:05 AM7/26/16
to JaCoCo and EclEmma Users, rahul...@gmail.com
tried that already
and Im stuck on this part.

<target name="test" depends="instrument">
<!-- Step 3: Run tests with instrumented classes -->
<java classname="org.jacoco.examples.parser.Main" fork="true">
<!-- jacocoagent.jar must be on the classpath -->
<classpath><pathelement path="../../../lib/jacocoagent.jar"/>
<pathelement path="${result.classes.instr.dir}"/></classpath>
.
.
.
the problem is, my ant build does not execute the test cases, it just compiles the class and create a jar file
this jar file is installed to device with our test framework and run from there.

is there any other workaround to run this?

Evgeny Mandrikov

unread,
Jul 26, 2016, 11:10:56 AM7/26/16
to JaCoCo and EclEmma Users, rahul...@gmail.com
Instrument classes locally, create jar file with instrumented classes, install it on the device and run tests with your framework as before without JaCoCo, download produced "jacoco.exec" from the device and create report based on it.

rahul...@gmail.com

unread,
Jul 28, 2016, 7:51:11 AM7/28/16
to JaCoCo and EclEmma Users, rahul...@gmail.com
so if that is the case
if I did the following I should get the jacoco.exec file generated

<target name="TestSuite.Build">
<property name="src.testsuite" value="com/xxxxxxx/xxx/test/testcase/template/TestSuite.*" />
<property name="path.mf.testsuite" value="com/xxxxxxx/xxx/test/testcase/template/MANIFEST.MF" />
<property name="bundlename.testsuite" value="${ant.project.name}" />
<echo>Compiling TestSuite.Build</echo>
<javac destdir="${jacoco.dir}" source="1.5" target="1.5" debug="on">
<src path="${src.dir}" />
<classpath refid="master-classpath" />
<include name="${src.testsuite}" />
</javac>
<copy file="${src.dir}/${path.mf.testsuite}" tofile="${build.dir}/${path.mf.testsuite}" />
</target>


<taskdef uri="antlib:org.jacoco.ant" resource="org/jacoco/ant/antlib.xml">
<classpath path="../bin/jacoco/jacocoant.jar" />
</taskdef>

<target name="instrumentation">
<jacoco:instrument destdir="${build.dir}" xmlns:jacoco="antlib:org.jacoco.ant">
<fileset dir="${jacoco.dir}" includes="**/*.class" />
</jacoco:instrument>
</target>

<!-- TestSuite.Bundle -->
<target name="TestSuite.Bundle" depends="TestSuite.Build, tbc.Bundle, instrumentation">
<echo> *********************************</echo>
<echo> TestSuite Compiling _ Making Bundle </echo>
<echo> *********************************</echo>
<echo>TestSuite.Bundle</echo>
<antcall target="tbc.Build" />
<mkdir dir="${jars.dir}" />
<jar destfile="${jars.dir}/${bundlename.testsuite}.jar" manifest="${build.dir}/${path.mf.testsuite}">
<fileset dir="${build.dir}">
<include name="${src.testsuite}" />
</fileset>
<fileset dir="${jars.dir}">
<include name="${bundlename.tbc}.jar" />
</fileset>
</jar>
</target>

<!-- tbc.Build-->
<target name="tbc.Build">
<property name="src.tbc" value="com/xxxxxxx/xxx/test/testcase/template/tbc/*.*" />
<property name="path.mf.tbc" value="com/xxxxxxx/xxx/test/testcase/template/tbc/MANIFEST.MF" />
<property name="bundlename.tbc" value="tbc" />
<echo> Compiling tbc</echo>
<javac destdir="${build.dir}" source="1.5" target="1.5" debug="on">
<src path="${src.dir}" />
<classpath refid="master-classpath" />
<include name="${src.tbc}" />
</javac>
<copy file="META-INF/MANIFEST.MF" tofile="${build.dir}/${path.mf.tbc}" />
</target>


<!-- tbc.Bundle-->
<target name="tbc.Bundle" depends="tbc.Build">
<echo> *********************************</echo>
<echo> tbc Compiling _ Making Bundle </echo>
<echo> *********************************</echo>
<echo>tbc.Bundle</echo>
<mkdir dir="${jars.dir}" />
<jar destfile="${jars.dir}/${bundlename.tbc}.jar" manifest="${build.dir}/${path.mf.tbc}">
<fileset dir="${build.dir}">
<include name="${src.tbc}" />
</fileset>
<fileset dir="." includes="testFiles/**" />
</jar>
</target>


jacoco-agent.properties(added in class path )-> jacoco-agent.destfile:\home\test\tmp\jacoco.exec

but no reports are generated

Evgeny Mandrikov

unread,
Jul 28, 2016, 8:39:44 AM7/28/16
to JaCoCo and EclEmma Users, rahul...@gmail.com
As stated on page http://www.eclemma.org/jacoco/trunk/doc/offline.html :
if you supply system properties, then they must be prefixed, but not in a "jacoco-agent.properties" file

I guess that in your current configuration jacoco.exec was generated in a working directory of your test runner.

Rahul Sivananda

unread,
Jul 28, 2016, 9:28:52 AM7/28/16
to Evgeny Mandrikov, JaCoCo and EclEmma Users
no its not, i checked every posible locations
and properties file I have prefixed all args like jacoco-agent.xxx no results.
Is there a way to check if the agent is running or not?

Evgeny Mandrikov

unread,
Jul 28, 2016, 10:35:46 AM7/28/16
to JaCoCo and EclEmma Users, mand...@gmail.com, rahul...@gmail.com
Just to be sure that we are talking about the same thing:
  • properties in file "jacoco-agent.properties" MUST NOT have prefix
  • this file must be in "Java properties file format", where backslash is used for escaping
And to be sure let's explicitly specify output mode, so that "jacoco-agent.properties" will be:
destfile=/home/test/tmp/jacoco.exec
output=file

If all correct, then on a first execution of any class instrumented in offline mode JaCoCo will try to create an empty output file. And this is an indication that it works.

Another indication that classes instrumented and executed correctly: classes instrumented in offline mode have a direct dependency on "jacocoagent.jar" requiring it to be on classpath, in other words - remove it from classpath and if classes instrumented, then their execution should fail.

Also I'd like to propose you to play a bit with a simple "hello world" example and JaCoCo Offline instrumentation locally on your machine independently from the targeted project, magical "device" and its testing framework to see on practice that offline instrumentation works and how it works.
And only after that move forward with integration into build of your project and execution on "device".
Reply all
Reply to author
Forward
This conversation is locked
You cannot reply and perform actions on locked conversations.
0 new messages