Posted by: Manu on: July 28, 2008
ANT and Emma can be used together to automate the generation of code coverage report. Following Steps are solving the same purpose-
First define properties-
<!– EMMA BEGIN –>
<property name=”emma.dir” location=”bin/emma” />
<property name=”emma.thresholds” value=”class:100,method:100,block:100,line:100″ />
<path id=”emma.libraryclasspath”>
<pathelement location=”lib/emma.jar” />
<pathelement location=”lib/emma_ant.jar” />
</path>
<taskdef resource=”emma_ant.properties” classpathref=”emma.libraryclasspath” />
<!– EMMA END –>
1) Create ant target for the instrumentation of java classes-
<!– ============================================ –>
<!– Instrument the classes –>
<!– ============================================ –>
<target name=”emma.instrument”>
<!– Instrument the class files. –>
<emma enabled=”true”>
<instr instrpath=”bin” destdir=”bin” metadatafile=”${emma.dir}/metadata.emma” merge=”true” mode=”overwrite”>
<filter excludes=”com.XXX.somefolder.SomeJavaFile” />
</instr>
</emma>
</target>
2) Create ant target for the generation of code coverage report.
<!– ============================================ –>
<!– Create coverage report –>
<!– ============================================ –>
<target name=”emma.report”>
<emma enabled=”true”>
<report sourcepath=”src” sort=”+name” metrics=”${emma.thresholds}”>
<fileset dir=”${emma.dir}”>
<include name=”*.emma” />
<include name=”*.ec” />
</fileset>
<xml outfile=”${emma.dir}/coverage.xml” depth=”method” />
<html outfile=”${emma.dir}/index.html” depth=”method” columns=”name,class,method,block,line” />
</report>
</emma>
</target>
3) Create ant target for the generation of Junit report.
<target name=”junitreport”>
<junitreport todir=”${junit.output.dir}”>
<fileset dir=”${junit.output.dir}”>
<include name=”TEST-*.xml”/>
</fileset>
<report format=”frames” todir=”${junit.output.dir}”/>
</junitreport>
</target>
After the creation of above ant targets, first of all run emma.instrument target, it will create metadata file of instrumented classes at PROJECT_HOME/bin/emma folder and all classes will get instrumented.
Now there are two ways to create converage.ec file, depend on the nature of project-
I) If project is a simple java project, run your junit test cases and coverage.ec file will create in your PROJECT_HOME folder. Copy this file into the PROJECT_HOME/bin/emma folder and run emma.report ant target. This will generate code coverage report in the PROJECT_HOME/bin/emma folder.
II) If project is an enterprise application project, export the .EAR file of your project into the concern folder of your application server. For example if you are using JBOSS, export .EAR into the JBOSS_HOME\server\default\deploy folder. This .EAR file should contain metadata.emma file. Now put the URL into the browser and play with your (read as launch/run) application. This will create coverage.ec in the JBOSS_HOME\bin folder. Now Copy this file into the PROJECT_HOME/bin/emma folder and run emma.report ANT target. This will generate code coverage report in the PROJECT_HOME/bin/emma folder.
Finally Run jnitreport ANT target for junit report. So this is a simple way to generate code coverage and junit report through ant, Hope it helps. Good luck and please contact me if you have any difficulties or leave a comment below to help out other users.
Thanks,
Manu
I’m a new bee of emma.
I’m trying some example in EMMA: Quickstart (http://emma.sourceforge.net/intro.html) but I have problem with Offline Instrumentation on the step creating coverage.ec file, at the line
>java -cp SwingSet2.jar:emma.jar SwingSet2
It cannot start the SwingSet2 but throwing out some exceptions.
The SwingSet2 is already instrumented in D:\
Structure:
D:\SwingSet2.jar,
D:\emma.jar
I don’t understand exactly what that command line means.
I’m using OS Window XP. Is there anything different between Win and Unix based OS in this case?
Please help!
Thanks in advance
Hi Manu!
Thanks for your support.
I tried to modify the command line into:
>java -cp SwingSet2.jar;emma.jar SwingSet2
And it ran well. The different is “;” between 2 jar files. Maybe the guidance of EMMA is wrong or the command line in Linux differs a bit with WinXP.
I’m now getting another problem when creating .ec file by offline-instrumentation with a web application.
I based on steps in 3.11 item in FAQ of EMMA:
http://emma.sourceforge.net/faq.html#q.runtime.appservers
Step 1: >java -cp emma.jar emma instr -m overwrite -cp .\testemma\EmmaDemo\build\web
\ -d .\
Step 2: build my web application into EmmaDemo.war
Step 3: copy emma.jar into \lib, and set server classpath (I also copy emma.jar into its /lib/ext directory)
Step 4: deploy .war file and test
I cannot get the .ec file from this step.
Please help again.
Thanks very much
Hi Manu!
Thanks for your support.
I tried to modify the command line into:
>java -cp SwingSet2.jar;emma.jar SwingSet2
And it ran well. The different is “;” between 2 jar files. Maybe the guidance of EMMA is wrong or the command line in Linux differs a bit with WinXP.
I’m now getting another problem when creating .ec file by offline-instrumentation with a web application.
I based on steps in 3.11 item in FAQ of EMMA:
http://emma.sourceforge.net/faq.html#q.runtime.appservers
Step 1: >java -cp emma.jar emma instr -m overwrite -cp .\testemma\EmmaDemo\build\web
\ -d .\
Step 2: build my web application into EmmaDemo.war
Step 3: copy emma.jar into \lib, and set server classpath (I also copy emma.jar into its /lib/ext directory)
Step 4: deploy .war file and test
I cannot get the .ec file from this step.
Please help again.
Thanks very much
Hi Manu,
I thought there’s problem with this site after not seeing my replies.
I’ll check again.
Thank you so much for your answer.
can we get emma coverage report in .xls format
Hi Manu,
Q1: I have a web application EmmaDemo.war file with the structure as below:
EmmaDemo
|- build // javac class files
|- dist // EmmaDemo.war file
|- lib
|- src // source code (java files)
|- web // .jsp, . css, … files
|- build.xml
After run ANT with build.xml, I receive an EmmaDemo.war. How can I run EMMA coverage report with this .war file?
Q2: Based on the source code and structure described above. I run EMMA report directly by compile, instrument and report. But after getting instrumented class, I don’t know how to modify the ANT file to get .ec file when running the application from server.
Note: run a web app from server is different from a desktop app. ‘Cause web app doesn’t have static class main() as in desktop app.
EMMA example just describes how to gen report from desktop app with a class containing main method.
Please help on these question. If you need more information (souce code,… ) please let me know.
Thank you very much,
Hi Manu,
Thank you so much! I’ve done it.
I did confusingly understand the order of the ant targets. Now it’s quite smooth.
By the way, I tried the “ctl tool” to dump runtime coverage data in EMMA version 2.1.5 but it throws some exceptions. Maybe this version is a testing one and there’re still some problems. Have you ever tried it? How do you feel?
Hi Manu,
nice tutorial!!
I have a set of jar files in some location
say “/home/aaa/jars/*.jar”
so using ant script and emma i have to instrument all the jar files in some other location but I am not getting how to write ant script for it.
Please help.
Thanks,
Yagnesh
HI Manu,
Can you provide an example of EMMA with JUnit with source code.
Thanks,
Ponds
Hi Manu,
i am working on enterprise application. i created a metadata.emma and add it to the my ear fille and copy this ear in deploy folder of jboss.
i run my application and navigate to all the pages but i didnt see any file like coverage.ec in Jboss/bin.
Please help me on this. Thanks in advance…
Hi Manu,
thats really a cool one. I am a novice to JUnit, Emma and Ant. I have chose to integrate these three technologies as my project. It will be helpful to me if you help me to start from the scratch.
Quite good and simple knowledge transfer for effective results.
Hi Manu,
Thanks for replying. Actually my project is to automate the testing process for a web application. I was able to run a basic build file which uses the money sample package from JUnit.
I want to import test data for JUnit from external source i.e., I need to maintain test data alone in a separate file. Whenever JUnit is executed, actual and expected results should be imported from the test data.
All these processes should be done through a GUI
Hi Manu,
Sorry for the late reply..
I’ll elloborate my requirements:
* A GUI should be designed which prompts the user to select a package.
* It will then display all the methods in the classes of the selected package.
* The user needs to select a method to test.
* The user needs to enter the expected and actual results for the JUnit test of the selected method.
* The JUnit test is now executed and the result states that whether the test has passed or not.
(upto this stage, I have completed). The problem starts from here:
* The actual and expected results which are fed for the test should be extracted and stored in a file (say a text document).
(this should be done for all the tests which are encountered by the method)
* When a method is tested again, the actual and expected results should be imported from these files and also we need to feed them through the GUI.
* In between, the test (the input we feed through the GUI) which we do through the GUI should undergo code coverage analysis
I talked to my guide about the usage of Selenium RC, but he insists me to concentrate on this GUI first
The links you gave are very much useful. They are very much basic and I too explored them through the net
September 19, 2008 at 6:02 AM
Nice Triveni’s Manu..
New Post :
I don’t want to love you… but I do….