Test Code Coverage Plugin

This plugin will generate code coverage reports using Cobertura.

Please create issues in JIRA if you encounter problems.

To install:

For Grails 1.1
grails install-plugin code-coverage

For Grails versions prior to version 1.1
You need to install version 0.9 of the plugin
grails install-plugin code-coverage 0.9

To run:

grails test-app

Output is written to test/report/cobertura directory. Just open the index.html file.

If you are using a Grails version prior to 1.1, you need to use the provided script:

grails test-app-cobertura

Command line options:

By default, the script will create HTML reports but if you would prefer XML reports (e.g for a Continuous Integration server), specify the -xml flag like this:

grails test-app -xml

The plugin does some post processing on the HTML reports to make the Controller action names appear in human readable form (like Controller.list instead of Controller.$_closure1 ). If you don't require this or it is too resource intensive, you can tell the plugin to skip post processing of HTML files by passing the -nopost flag.

grails test-app -nopost

The default is to run coverage whenever tests are run, but there may be times you would like to bypass this default (for speed of test execution, for example). You can do this by using the -nocoverage flag on the command line:

grails test-app -nocoverage

In some versions the default is reversed so you have to specify this argument to generate the coverage reports.

grails test-app -coverage

Configuration Options:

You can add your own exclusions from the reports by adding an entry in your BuildConfig.groovy file like this:

coverage {
	exclusions = ["**/ExcludedController*", "**/excludedDir/**"]
}

The exclusions are by class name, and package, not path. Here's some more examples, that will filter out jsecurity classes and richui classes, if you are running cobertura with webtest.:

//cobertura exclusions
coverage {
	exclusions = [
	              '**/de/andreasschmitt/richui/taglib/renderer/**',
	              '**/plugins/richui-0.4/src/groovy/de/andreasschmitt/richui/**',
	              '**/de/andreasschmitt/richui/image/**',
	              '**/org/jsecurity/**',
	              '**/org/jsecurity/grails/**',
	              '**/JsecDbRealm*',
	              '**/*TagLib*/**',
	              "**/*Tests*",
	              '**/JsecAuthBase*',
	              '**/JsecurityFilters*']
}

By default, the plugin excludes several items from the coverage results including:

"**/*BootStrap*",
        "Config*",
        "**/*DataSource*",
        "**/*resources*",
        "**/*UrlMappings*",
        "**/*Tests*",
        "**/grails/test/**",
        "**/org/codehaus/groovy/grails/**",
        "**/PreInit*",
        "*GrailsPlugin*"

As of version 1.1.4, you can completely override the default list of exclusions and provide your own.

coverage {
	exclusionListOverride = [
        "**/grails/test/**",
        "*GrailsPlugin*"]
}

In addition, you can add directories to the default list for source files to include in the coverage reports. For example, if you're using the Jsecurity plugin and want to see the source for files in the 'realms' directory in the reports, add the following to your BuildConfig.groovy:

coverage {
     // list of directories to search for source to include in coverage reports
     sourceInclusions = ['grails-app/realms']
}

Release History:

1.1.5
  • Change from using Config.groovy to BuildConfig.groovy to configure exclusions and custom properties
1.1.4
  • support for Grails 1.1 RC2
  • upgraded to Cobertura 1.9
  • Removed TestAppCobertura script, plugin now utilizes the events framework to tie into the main Grails test-app script
  • added ability to override exclusions through config.coverage.exclusionListOverride
  • added ability to bypass generation of code coverage through the -nocoverage flag
1.1.0
  • support for Grails 1.1
0.9
  • added post processing of HTML reports to display human readable names for controller actions (Controller.list instead of Controller.$_closure1)
0.8.5
  • Support for Grails 1.0.4 (deprecated Events.groovy and use _Events.groovy instead)
  • added default exclusions for the Grails Testing Plugin (grails/test/ and org/codehaus/groovy/grails/)
0.8.3
  • add sourceInclusions config option
0.8.2
  • updated the default codeCoverageExclusionList in TestAppCobertura.groovy to exclude 'Config' rather than '*Config*'
0.8:
  • Added scriptEnv = "test" so you no longer need to specify the environment when running the test-app-cobertura script
  • fixed a bug where project specific exclusions were not being loaded correctly

2 Comments

  • Gravatar
    .h3 General
    1. I do not know how to write correct software without testing
    2. I do not know how to write correct tests without code coverage
    So as you can imagine, I'm pretty happy with code coverage for Grails. Thank you!

    .h3 Notes

    1. The default for coverage is off. You say that's "for some versions the default is reversed". Perhaps you'd consider keeping it off by default and requiring "-coverage" from now on. Of course, the devil in me wants to teach the world the value of code coverage so why not make it the default. Ramble, ramble...I think this could be clearer in the doco.
    2. I had to write
    def coverage = {
         // list of directories to search for source to include in coverage reports
         sourceInclusions = ['grails-app/realms']
    }

    in BuildConfig.groovy . Perhaps I'm missing something.

    Jun 30, 2009 14:06 PM NogbadTheBad
  • Gravatar
    Grrr… no preview for wiki text in a comment? Or edit?
    Jun 30, 2009 15:06 PM NogbadTheBad

Post a Comment