Straight from the Heart

EMMA: a free Java code coverage tool

Posted on: July 28, 2008


How to Use EMMA-

 

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

Tags:

43 Responses to "EMMA: a free Java code coverage tool"

Thanks Rohit.

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 Trang,
May i Know, what kind of exeptions you are getting?

Also while creating coverage report(coverage.ec) the state of class files should be same as the state while instrumentation. Otherwise when you will create coverage.ec, metadata information won’t match with the current classes.

I don’t think this problem is because of different OS as i have tried on the same Win XP.

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 Trang,
Sorry for the late reply. I have just checked your comment as it was blocked by Aksimet, my anti-spammer s/w. I think some issue is there.

Hope your problem would be solved by now.

If not then, please check the metadata file. Is it there in EmmaDemo.war.

.ec File will be created in APP_SERVER_HOME\bin folder. Also after command get executed, check the respective error message. Sometimes it’s not created just after testing the application and gets reflected after stopping the server, it is not a sure behavior but I encountered such problem.

I would like to refer ant for all these things, just create a build file by following instructions given in the post. It will save your time from setting explicitly class path and all.

Thanks,
Manu

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 Rajiv,

In Current version, I think its not possible to get report in .xls format. Coverage data can be exported only as a *.ec file or in XML or HTML format.

Thanks,
Manu

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 Trang,

The answer of your question is already given in the post.

Anyway, i think there is problem with the order of your ant targets.
First compile, then instrument, then create war.

Export the .EAR/.WAR 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 the application. This will create coverage.ec in the JBOSS_HOME\bin folder. Now Copy this file into the PROJECT_HOME/build/emma folder and run emma.report ANT target. This will generate code coverage report in the PROJECT_HOME/build/emma folder.

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. Do you have any information about how to set up an ant script to instrument a jar file?

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 Naresh,
smtimes this file gets created after stop jboss action, idealy it should created w/o stopping jboss. just stop jboss and coverage.ec file will get created in the bin folder.

Cheers,
Manish

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 Sujai,

I will be happy to help you out, tell me what exactly your project is.

before creating build target, read some stuff regarding junit and ant (prefer their official sites). emma is easy to learn but you need to cover unit and functional test cases for your project.

Cheers,
manish

Thanks Ram.

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😦

What i got from the above write-up that you have a web application which take some data through GUI and perform some operations on that data, which eventually give results.

Some confusions-

want to import test data for JUnit from external source i.e., I need to maintain test data alone in a separate file.

what is this external source. are u providing data through UI and storing into a file.

what operation you are doing on that test data. Some methods are involve in thses operations. you need to use those methods in your unit tests (junit). junit is used for writing unit test cases to test java code, for functional (UI) testcases selenium is good choice.

after the completeion of these test cases, emma will come in picture for code coverage.

refer these link..

http://www.thekirschners.com/articles/how-to-write-test-cases.html

http://blog.taragana.com/index.php/archive/9-important-tips-for-selenium-remote-control-java-client-test-tool/

Let me know if it helps.

Cheers,
Manish

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🙂

I need to use emma for web applications. For normal java applications,it is ok to use emma. But I have no idea hoe to use emma for java servlet web applications.
Plz help me to solve this

Hi Manu,

I am facing the same issue as Naresh in post no 16:

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.

I also tried stopping JBOSS, even then i could not see any coverage.ec file in JBOSS/bin folder.

I have another Question related to this that may give you an idea about the problem:
After running the emma.instrument target, it created a metadata.emma file under emma folder, now when i pack it in my JAR File back i packed only the file under emma folder ( i mean not the new instrumented class files ) Now my question which is very basic q that what do we mean by instrumneted classes, do they also get changed after rumming emma.instrumnet target and do they also need to pack again in JAR file.

So many Questions i think.. oops😛

Please help me. I would be very thankful to you Manu.

Thanks,
Ashish

Hello,
what should be content of emma_ant.properties file?
I’m getting error below. Thanks.

Buildfile: C:\projects\someproject\build.xml
[taskdef] Could not load definitions from resource emma_ant.properties. It could not be found.

emma.instrument:

BUILD FAILED
C:\projects\someproject\build.xml:45: Problem: failed to create task or type emma
Cause: The name is undefined.

this issue is resolved.

hi
I am facing the same problem when trying to execute the following xml.

The error i get is

BUILD FAILED
C:\Users\Aswathy\workspace\EmmaRunTest\EmmaRun\ANT\2_emma-instument.xml:47: Problem: failed to create task or type emma
Cause: The name is undefined.
Action: Check the spelling.
Action: Check that any custom tasks/types have been declared.
Action: Check that any / declarations have taken place.

I am also facing the same issue.
How did you resolve this?

@Arpan Can you please put some light on it- what kind of same issue your are facing.

Now I’ve exported the war and deployed on app server, when accessing the index.jsp page, content is not showing as before when emma instrumentation is not involved. any ideas where thing might have gone wrong? thanks.

C:\projects\proj1>ant emma.instrument
Buildfile: build.xml

emma.instrument:
[instr] processing instrumentation path …
[instr] package [com/mycomp/pkgname/appman] contains classes [Application] without full debug info
[instr] package [com/mycomp/pkgname/aaa] contains classes [AppCredential] without full debug info
[instr] package [com/mycomp/pkgname/aaa/common] contains classes [Constants] without full debug info
[instr] package [com/mycomp/pkgname/bbb] contains classes [CloudFileTransfer] without full debug info
[instr] package [com/mycomp/pkgname/session] contains classes [SessionData] without full debug info
[instr] instrumentation path processed in 282 ms
[instr] [16 class(es) instrumented, 0 resource(s) copied]
[instr] metadata merged into [C:\projects\proj1\coverage\metadata.emma] {in 15 ms}

BUILD SUCCESSFUL
Total time: 1 second
C:\projects\proj1>ant export-war
Buildfile: build.xml

BUILD SUCCESSFUL

Hi Manu,

If I have only an EAR file for an application with me then is possible to apply EMMA on it? If yes then please let me know the steps for the same.

Thanks,
Vikas

Hi Vikas,
yes, you can apply emma on it but as I said- your ear file should have metadata.emma file.

your answer is in the post itself-

“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.”

cheers,
Manish

Can you please tell me the steps for having metadata.emma file in my EAR? I am trying to do it from scratch on a sample EAR file which doesn’t have any emma thing in it. Kindly provide me steps for testing emma on a sample EAR file. I have only a sample EAR file nothing else.

Hi Vikas,

Please look into this ant target. this target is doing instrumenting the class files, meta data file will be created at ”${emma.dir}/metadata.emma, you can provide this emma.dir value.

so change this target as per your project and run this target.

txs a lot to this site. it helped a lot..The comments posted and content posted in the site helped me a lot

(Sry for weak english)

thank you

txs the explantion of emma

i want to know how to use ctl tool.i updatd the emma jar and follwed trhe steps which are done for normal build..when i start tomcat ctl tooll on port 45763 running
when i click the port it is showing java heap size memory

can u help me out
plz

Hi manu,

i am not getting correct emma coverage report. what i am doing in build file ,

1) i started server,after this 2) compiled my junit testcases ,3) jar files instrumentation 4)emma report.

but i am getting coverage.ec and metadata.emma files.

but this is not showing correct coverage report.

please suggest me how to solve this problem.

Thank you…..

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Translator

a

The Calendar..तारीख

July 2008
M T W T F S S
« Jun   Sep »
 123456
78910111213
14151617181920
21222324252627
28293031  

गुफ्तुगू ….

Sindhu on How to integrate Sonar with…
Naga Anji on EMMA: a free Java code coverag…
Manu on Hiring
jarvarm on Hiring
Manu on Hiring

पन्ना

हिसाब-किताब

  • 37,617 hits

Tashveerein.......तसवीरें

Ceannabeinne

Cuckoo, Cuckoo

All the Small Mistakes

The Old Teahouse in Pengzhen Town, Chengdu

Kestrel      (Falco tinnunculus)

American Crow

Sony A7 MII + Zeiss Sonnar 50/1.5 ZM

Einsam

sparkles

Tunnel View Sunset

More Photos

Thanks for the Visit…आने का शुक्रिया

website statistics

Rumbling of Mind

  • Sushil Kumar has won his second Olympic medal, become the first Indian to win two individual Olympic medals for India. Respect. 4 years ago
  • Satyamev Jayate-A great initiative by Aamir Khan & company....must watch on star plus/DD National every Sunday 11:00 AM 4 years ago
  • @sujeetkant number is changed..sent on your linkedin.. 4 years ago
  • @sujeetkant dude I m fine..howz life at you end ..long time 4 years ago
  • In real bad mood.. :( Banged my car today.. 4 years ago

Aaate-Jaate..आते-जाते

Top Clicks..पसंदीदा

  • None

The Selected One’s..चुनिन्दा

U come from..

%d bloggers like this: