Code Coverage with Emma
This example is based on the Car_Rental_Example. If you want work through this example, you need to follow these instructions first.
In this example, we add code coverage to the Car Rental Application. We then examine the results, make some changes and finish with a few conclusions.
Setup and Configuration
Emma does not currently come with a plugin, so in this example we’ll use Ant from within Eclipse. I’ll warn you that I’m not much of an Ant user, so if you’d like to make recommendations, please feel free: schuchert@yahoo.com.
Let’s get started:
Download
- Download Emma from here. Note I downloaded emma-2.0.5312.zip for this example.
- Extract the jar somewhere. I used c:\libs\, which created the directory C:\libs\emma-2.0.5312.
- Start Eclipse using the Car Rental workspace. If you used my directory names, it is C:\workspaces\CarRentalExample.
suite() method
Ant 1.6.5 does not play nice with JUnit 4.x, so I added a suite() method to each of my classes. Depending on when you downloaded this file, you already have those suite methods.
Ant Script
When you followed these instructions, you should have created a workspace with three projects:
- CarRental
- LoggingUtils
- ToolConfiguration
Under CarRental there is an xml directory that contains two ant build files:
- cobertura.xml
- emma.xml
(By the way, let me give credit where it’s due, I got most of this from Getting Started with Emma using Ant.
- Here’s the file so we can discuss it and configure it for your situation:
emma.xml
Interesting Lines
N/A.
Update Tests
Execution
Assuming you’ve updated the emma.xml file and set all of the relevant properties for your environment, then do the following:
- Right-click on CarRental/xml/emma.xml
- Select Run As:Ant Build
- Wait for a few seconds (about 5 - 10 in my case)
- Note that this ant file adds a directory called emma_results under the CarRental directory with the execution results. To see this (if you don’t automatically see it after executing this ant task), select CarRental, right-click and select refresh (or hit F5).
I considered adding the following line to emma.xml:
This would force a refresh but it also requires that you run the ant script in the same VM as Eclipse.
Preliminary Analysis
With your directory refreshed, you’re ready to have a look at the output:
- Expand the CarRental project.
- Expand the emma_results directory.
- Double click on coverage.html.
- Because this is just the top-level file from my machine, it is missing some links.
- Here’s a summary of my numbers by package:
name | class, % | method, % | block, % | line, % |
all classes | 94% (78/83) | 83% (527/636) | 80% (6040/7578) | 81% (1579.7/1956) |
vehicle.type | 83% (10/12) | 68% (56/82) | 63% (437/692) | 67% (104.4/155) |
vehicle.configuration | 100%(2/2) | 80% (8/10) | 67% (73/109) | 68% (21/31) |
vehicle.integration | 100%(4/4) | 84% (37/44) | 71% (470/662) | 72% (118.7/164) |
vehicle.component.vehicletype | 100%(2/2) | 77% (23/30) | 73% (320/438) | 71% (82.5/116) |
vehicle.component.rateplan | 100%(2/2) | 87% (46/53) | 76% (804/1061) | 74% (219.1/295) |
vehicle.reference | 100%(1/1) | 62% (5/8) | 78% (84/108) | 81% (22/27) |
vehicle.component.vehicle | 100%(2/2) | 87% (20/23) | 83% (216/259) | 81% (63.4/78) |
vehicle.domain | 100%(26/26) | 86% (199/231) | 84% (1908/2275) | 87% (502/575) |
vehicle.validation | 88% (14/16) | 85% (58/68) | 84% (669/793) | 85% (198/234) |
vehicle.util | 100%(4/4) | 80% (12/15) | 85% (109/128) | 84% (32/38) |
vehicle.exception | 80% (4/5) | 67% (4/6) | 89% (70/79) | 69% (9/13) |
vehicle.component.rentalagreement | 100%(2/2) | 95% (21/22) | 89% (230/257) | 90% (68.2/76) |
vehicle.integration.inmemory | 100%(5/5) | 86% (38/44) | 91% (650/717) | 90% (139.4/154) |
So I have roughly 81% code coverage with 106 unit tests and I have a few problem areas:
- vehicle.type
- vehicle.configuration
- vehicle.integration
- …
Over the next several sections, we will look at each package with less than 80% code coverage, assess each class and plan out our changes. For you to follow along, you’ll need to execute the Emma ant script and then look at the classes to understand the assessments.
Comments