CodeNarc - Run as an Automated Test

CodeNarc can be run as part of an automated test suite, for instance using JUnit or TestNG. This approach uses the org.codenarc.ant.CodeNarcTask class and Groovy’s built-in support for Apache Ant.

NOTE: This approach may not play well with having CodeNarc also configured to run as a separate task aspart of your “regular” build, especially if you have a classpath defined for your build which is different that your runtime classpath for running your tests (which is likely).

Automated Test That Runs CodeNarc

This is an example JUnit (4.x) test that runs CodeNarc.

    private static final GROOVY_FILES = '**/*.groovy'
    private static final RULESET_FILES = [

    void testRunCodeNarc() {
        def ant = new AntBuilder()

        ant.taskdef(name:'codenarc', classname:'org.codenarc.ant.CodeNarcTask')

           maxPriority1Violations:0, maxPriority2Violations:0) {

           fileset(dir:'src/main/groovy') {
           fileset(dir:'src/test/groovy') {


Things to note:

See the CodeNarc Ant Task for more information on configuring the Ant task.