Test coverage for service/UI tests using Jacoco

Though as a QA, using test coverage tool/library might not be the ideal way to figure out your test suit’s (be it junit, cucumber, selenium, etc) coverage, it helps to get a lot of insights from time to time.

When you search for java test coverage you get these 2 on top, Emma and Jacoco. I tried Jacoco, and its a 2 step process.

1 ) Gather execution data :

The idea is to instrument your application when you are running your test. As I said earlier, it can be any test like rest assured api test, selenium UI tests or even manual tests. So you start with instrumenting your application, viz. the jar file. You use the java agent to read class files in your jar file and dump the readings in an execution file. This is the command format :

java -javaagent:[yourpath/]jacocoagent.jar=[option1]=[value1],[option2]=[value2]

An example would be :

java -javaagent:jacocoagent.jar=destfile=jacoco.exec -jar myApp.jar --some.variable=value

In above command we specify the path to the jacocoagent.jar file which is available for download from the above link. jacoco.exec is the path to the file were the execution dump is stored. Followed by path to your application jar file, myApp.jar. And finally any params, some.variable which you would normally give when you start your app in regular fashion.

You run this command right before you start your test use and exit it when your suit stops. Now the .exec file contains all the information. So next step would be extract a readable report from this file.

2) Generate Report :

You use another jar file jacococli.jar to generate the report. This is the format :

java -jar jacococli.jar report [ ...] --classfiles  [--csv ] [--encoding ] [--help] [--html

] [–name ] [–quiet] [–sourcefiles ] [–tabwith ] [–xml ]An example would be :

java -jar jacococli.jar report jacoco.exec --classfiles ./build/classes/ --html ./coverage/report

In the above command we specify path to the jacococli.jar , the execution file jacoco.exec , path to the class files ./build/classes/ , followed by the type of report you need –html finally the path to the directory were reports need to be saved ./coverage/report. After this command you would find the reports in the coverage/report folder.

The report has great details with visual indications of how much of the code is covered and how much is not, you can see at a package level and right through individual classes.

I hope this gives a good start to use the tool. Please comment if you think there are other great/easier/useful tools for coverage.

Glossary

http://www.jacoco.org/jacoco/
http://www.jacoco.org/jacoco/trunk/doc/cli.html
http://www.jacoco.org/jacoco/trunk/doc/agent.html

Advertisements

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