Ulrich Enslin
Ulrich Enslin

Reputation: 383

grails test-app to output to console

I am new to grails, coming from Django.

Using test driven development, I am used to writing tests and then actual functionality. What works well for me it to write the test, run the function with some debug output to see that state of variables until the unit test passes, and then moving the debug output.

In grails the 'grails test-app', the output of 'log.debug' and 'println' is not recorded to the console, and it is not in the reporting either.

The documentation point to using mocklogging, which should output the log.debug calls to the console, but using grails 1.2.1, I can not see any output.

Can any one please let me know how to see the output of 'println' or 'log.debug' in the console to speed up my developement?

Upvotes: 38

Views: 21497

Answers (4)

Colin D
Colin D

Reputation: 3109

The other answer about adding the log4j config in Config.groovy is important

debug 'grails.app'

But also make sure the modules you are testing have the logging turned on.

For example, if you saw some failure like this from your test

| Failure:  write a GFF3 of a simple gene model(org.company.YourAppIntegrationSpec)

and the debug logging wasn't showing up, then you might also need to add the logging for your package

debug 'org.company'

Upvotes: 0

jpswain
jpswain

Reputation: 14732

One thing that might also help you is doing this:

Make sure your Config.groovy sets up log4j:


import grails.util.GrailsUtil

if (GrailsUtil.environment == 'test') {
    log4j = {

        appenders {
            // %d{yyyyMMdd.HHmmss.SSS} %r [%t] %-5p %c %x - %m%n
            console name:'a1', layout:pattern(conversionPattern: '%d{yyyyMMdd.HHmmss.SSS} %r [%t] %-5p %c %x - %m%n')
        }

        root {
            info    'a1'
            additivity = true
        }

        // debug  'org.springframework.security'

        error   'org.codehaus.groovy.grails.web.servlet',  //  controllers
                'org.codehaus.groovy.grails.web.pages', //  GSP
                'org.codehaus.groovy.grails.web.sitemesh', //  layouts
                'org.codehaus.groovy.grails.web.mapping.filter', // URL mapping
                'org.codehaus.groovy.grails.web.mapping', // URL mapping
                'org.codehaus.groovy.grails.commons', // core / classloading
                'org.codehaus.groovy.grails.plugins', // plugins
                'org.codehaus.groovy.grails.orm.hibernate', // hibernate integration
                'org.springframework',
                'org.hibernate',
                'org.apache',
                'grails.util.GrailsUtil',
                'grails.app.service.NavigationService',
                'org.quartz',
                'net.sf.ehcache'

        fatal   'NotificationJob'


        warn    'org.mortbay.log',
                'groovyx.net.ws',                    // Web services too noisy with INFO statements
                'org.apache.cxf.endpoint.dynamic'    // Web services too noisy with INFO statements

        info    'org.springframework.security'

        debug   'grails.app.controller.TroublesomeController'
    }
}


In your test, do this:


import org.slf4j.Logger
import org.slf4j.LoggerFactory

class HandoffTests extends GroovyTestCase {
    Logger log = LoggerFactory.getLogger(HandoffTests)

   ... your tests ...
}

If you do this, log will behave the pretty much the same as it does in the domain, service, and controller objects, where grails auto-injects it for you. I have no idea why they don't auto-inject in tests...

Upvotes: 3

John Wagenleitner
John Wagenleitner

Reputation: 11035

In order to see log.debug statements you need to add the following to the log4 section of the grails-app/conf/Config.groovy file:

log4j = {
   ...
   ...
   ...
   debug 'grails.app'
}

Upvotes: 5

Ted Naleid
Ted Naleid

Reputation: 26801

Add the -echoOut switch to grails test-app, this is new in the 1.2 release:

grails test-app -echoOut

There are a number of other useful switches on test-app as well, including:

echo System.err messages:

grails test-app -echoErr

force a clean before running tests (without doing grails clean && grails test-app):

grails test-app -clean

only run unit tests:

grails test-app unit:

only run integration tests:

grails test-app integration:

run in a particular environment:

grails -Dgrails.env=production test-app

run tests only for a particular test class (like com.foo.MyControllerTests), be sure to leave off the "Tests" suffix:

grails test-app com.foo.MyController

rerun only the tests that failed the last time you ran your tests

grails test-app -rerun

Upvotes: 81

Related Questions