CodeNarc - CodeNarc - Run as a Test

##### General

- Home
- Downloads
- Javadocs
- Mailing Lists
- Bug Tracker
- SourceForge Project
- GitHub Project

##### Running

- Ant Task Usage
- Command-Line
- Run as a Test
- Other Tools/Frameworks

##### Using

- Creating a RuleSet
- Creating a Rule
- Configuring Rules
- Starter RuleSet (All)

##### Report Types

- HTML Report
- XML Report
- Text and IDE Reports

##### Sample Reports

- Sample HTML Report
- Sample XML Report

##### Rules

- Rule Index
- Basic Rules
- Braces Rules
- Concurrency Rules
- Convention Rules
- Design Rules
- DRY Rules
- Enhanced Rules
- Exceptions Rules
- Formatting Rules
- Generic Rules
- Grails Rules
- Groovyism Rules
- Imports Rules
- JDBC Rules
- JUnit Rules
- Logging Rules
- Naming Rules
- Size/Complexity Rules
- Security Rules
- Serialization Rules
- Unnecessary Rules
- Unused Rules

##### Developing

- Developer Guide

##### Project Documentation

- Project Information
- Project Reports

## 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 part of your "regular" Ant build, especially if you have a classpath defined for your Ant 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 test (JUnit 3.x) that runs CodeNarc.

        private static final GROOVY_FILES = '**/*.groovy'
        private static final RULESET_FILES = [
                'rulesets/basic.xml',
                'rulesets/imports.xml'].join(',')

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

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

            ant.codenarc(ruleSetFiles:RULESET_FILES,
               maxPriority1Violations:0, maxPriority2Violations:0) {

               fileset(dir:'src/main/groovy') {
                   include(name:GROOVY_FILES)
               }
               fileset(dir:'src/test/groovy') {
                   include(name:GROOVY_FILES)
               }

               report(type:'ide')
            }
        }

Things to note:

- This approach uses the `AntBuilder` class provided with Groovy.
- The CodeNarc jar must be on the classpath. Any external resources that you reference must also be on the classpath, including any custom RuleSet files.
- The maxPriority1Violations and maxPriority2Violations properties are configured to fail the test if any priority 1 or 2 violations are found.
- An ide report is configured. This will to write (only) to standard out, and will include hyperlinks to the lines within the source code that contained the violations . The report shows the total number of violations (at each priority) and also list the violations by file.

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