TutorialCodeCoveragePlugin

(Difference between revisions)
(Tutorial on Code Coverage Plugin)
m (Demo Project)
Line 18: Line 18:
This tutorial offers a walkthrough that uses a demonstration NetBeans project.  
This tutorial offers a walkthrough that uses a demonstration NetBeans project.  
-
Download and unzip [File:CodeCoverageDemo.zip CodeCoverageDemo] project into a local folder.
+
Download and unzip [[File:CodeCoverageDemo.zip]] project into a local folder.
The tutorial provides screenshots so you can still see how the main  
The tutorial provides screenshots so you can still see how the main  

Revision as of 21:20, 10 May 2010

Outline

In this tutorial you'll follow a short walkthrough for using the NetBeans Code Coverage Plugin.

  • Introduction
  • Activating Coverage
  • Viewing Coverage Results
  • Showing Coverage Statistics
  • Known Issues

Introduction

The NetBeans Code Coverage Plugin provides an interactive way to see testing coverage results within the NetBeans IDE. This enables developers to quickly identify the portions of Java code that aren't covered by their unit tests.

Demo Project

This tutorial offers a walkthrough that uses a demonstration NetBeans project.

Download and unzip File:CodeCoverageDemo.zip project into a local folder.

The tutorial provides screenshots so you can still see how the main features operate even if you choose not to follow the step-by-step walkthrough.

This walkthrough was created with NetBeans 6.5 on Ubuntu.

Activating Coverage

First install the plugin using the Update Center. Once installed, you must "activate" the plugin for the project on which you intend to use it. There is no "global" activation; each project requires a separate activation step.

To activate the plugin, open the CodeCoverageDemo project and right-click on the project node in Projects View.

From the context menu, click on Coverage -> Activate Coverage Collection. (If no Coverage menu item appears, the plugin has not been installed properly).

Expand the project node to see the files in the Source package. In the default package, open PigLatin.java.

File:CodeCoverageDemo1.png

Viewing Coverage Results

From the Run menu, choose "Test Project". (Alt-F6)

The JUnit tests window should say "EMMA: collecting runtime coverage data ..." The tests should pass.

File:CodeCoverageDemo2.png

Observe green highlighting in the source code window. Notice the true branch of the 'if' statement is green, but the false branch is not. Specifically, lines 26 & 28 in the source aren't green. That's because the test didn't cause those lines to be executed.

Show Coverage Statistics

If you are using JDK 6, from the Coverage menu select "Show Project Statistics."

File:CodeCoverageDemo3.png

Notice coverage of PigLatin.java is 82%.

In the Test package, open PigLatinTest.java.

Notice lines 11 and 12 are commented out. Those two lines will cause the false branch of the 'if' statement to execute.

Remove the slashes to uncomment those two lines. Alt-F6 to Test Project.

View the PigLatin editor window and observe the green highlighting now includes line 26 & 28.. (The 'else' statement is not green, apparently because the plugin doesn't consider it an executable statement.)

If you Show Project Statistics again, it says 100%.


Known Issues

  • The plugin doesn't work with Maven-based projects, only Ant-based ones.
  • Test-Single DOES NOT seem to work reliably. Use Test Project instead.
  • Show Project Statistics is only available with JDK 6 or later.
  • If you also have the Checkstyle plugin installed, checkstyle errors will hide the coverage highlighting. So fix the checkstyle errors and then you can see the coverage highlights.
  • If you have a class with only static methods, you can't get 100% coverage unless you also instantiate the class.
  • This plugin is based on the open source EMMA tool. For more info, visit the EMMA home page at http://emma.sourceforge.net/index.html
Not logged in. Log in, Register

By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo