Test Coverage with EclEmma


Laurie Williams, Ben Smith and Sarah Heckman [Contact Authors]
CSC 326 - Software Engineering
Department of Computer Science
North Carolina State University

Back to Software Engineering Tutorials


0.0 Contents
1.0 Introduction to EclEmma
2.0 Running EclEmma
3.0 EclEmma Coverage Report
4.0 Exporting a Coverage Report
5.0 Exercise
6.0 Resources

1.0 Introduction to EclEmma

EclEmma is an Eclipse plug-in that generates code coverage reports and provide simple trace information about test cases. We will be using EclEmma primarily as a coverage reporting tool.

Coverage is defined as a measure of the completeness of the set of test cases. This definition means that the more source code that is executed by your test cases the better. There are four types of coverage: There are four types of coverage:

  • Method coverage: how many of your methods have been called by your test cases?
  • Statement coverage: how many of your statements have been run by your test cases?
  • Decision/Branch coverage: how many decision points (if statements) have evaluated both true and false?
  • Condition coverage: how many Boolean sub-expressions have been evaluated both true and false at a decision point?

For small programs, coverage is relatively easy to calculate by hand; however, for larger programs, the task is much more difficult. Coverage tools, like EclEmma, help by automating the coverage process and providing readable reports.

EclEmma may be used on top of JUnit. EclEmma runs the JUnit test cases and generates the coverage report from the execution. EclEmma provides overall, package, and file level information on statement.

EclEmma can be downloaded for a manual install from its SourceForge Project Page. Additionally, EclEmma can be installed via Eclipse Update Site. For more information on installing eclipse plugins, consult the tutorial on the subject. EclEmma's update site is http://update.eclemma.org.

Top | Contents

2.0 Running EclEmma

Eclipse allows running Java programs directly from the workbench. Programs can be launched in different so called launch modes. In a standard Eclipse installation you can launch your programs either in Run or in Debug mode. EclEmma adds a new launch mode Coverage which is available from the Run menu and the toolbar:

Note: If the Coverage drop-down toolbar button is not visible in your current workbench perspective, open the Customize Perspective... dialog and enable the Coverage command group on the Commands tab.

To gather the coverage report for a JUnit test suite, right-Click the test suite you want to run and go to Coverage As -> JUnit Test as shown in Figure 3.


Figure 3. Run Coverage As...

If the test you want to execute has already been run at least once, you can find it in the Coverage Menu as shown in Figure 4.


Figure 4. Coverage Drowndown Menu

Top | Contents

3.0 EclEmma Coverage Report

The coverage view should appear as soon as your test case has finished executing. The EclEmma coverage report shows coverage statistics for statement coverage (as calculated by number of executed instructions) as shown in Figure 5. To pull it up manually, go to Window -> Show View -> Other -> Java -> Coverage.


Figure 5. Coverage Report

If you double click a file name in the coverage report, the source code shown in the editor with the lines that were covered in green and the lines that were not covered in red. The lines in white are non-executable as shown in Figure 6.


Figure 6. Executed Lines

Top | Contents

4.0 Exporting a Coverage Report

To export an HTML version of your coverage report, right click anywhere in the coverage report and go to Export as shown in Figure 7.


Figure 7. Exporting a Coverage Report

A dialog box will appear demonstrating all the JUnit (and other) runtime configurations you have executed recently. Select the one which contains the information you want to export. Then, click Browse and select a destination folder as shown in Figure 8.


Figure 8. Selecting a Session

Top | Contents

5.0 Exercise

For this exercise we will be using the CoffeeMaker project. Download the CoffeeMaker project from here or import the CoffeeMaker project that you used in the JUnit tutorial. For more information on CoffeeMaker, consult the CoffeeMaker User Stories and Black Box Test Cases. Import the project into Eclipse. Please see the Eclipse Import/Export Guide for instructions on how to import a project into Eclipse.

We all know that most computer scientists love caffeine, so the Computer Science department is looking to put a coffee kiosk in the new building. The coffee kiosk must be able to make coffee for students to purchase.

The CoffeeMaker code is complete; however, we need you to create and run unit tests until you have 90% coverage and a green bar on the CoffeeMaker, Inventory, and Recipe classes. Run your JUnit tests using EclEmma and generate a coverage report. (Hint: Run EclEmma on the initial project to see what coverage you already have, and what lines you need to test.)

Deliverables to the TA

  • Run all of your JUnit unit tests, which give a green bar.
  • A coverage report with 90% statement coverage or higher for CoffeeMaker, Inventory, and Recipe classes.
Top | Contents

6.0 Resources
Top | Contents

Back to Software Engineering Tutorials
Test Coverage with EclEmma ©2003-2008. North Carolina State University, Laurie Williams, Ben Smith and Sarah Heckman
Email the authors with any questions or comments about this tutorial.
Last Updated: Monday, March 30, 2009 3:24 PM