38

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?

4 Answers 4

81

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
3
  • 1
    Do you know how to output the actuall error as reported by junit? I.e. "expected foo, but got bar". Even with -testOut and -testErr, it's not displayed and I have to look in target/test-reports/plain/*.txt files.
    – c089
    Apr 13, 2011 at 17:52
  • Same issue here. I have to do something like this: $ grails test-app -echoOut -echoErr Something.testMain ; cat ./target/test-reports/plain/TEST-integration-integration-SomethingTests.txt Does anybody know a way to actually get it to output everything? Thxx!
    – jpswain
    Apr 26, 2011 at 21:48
  • Thanks for posting these. I only wish I could figure out a way to have the console output print the test name it is running instead of "1 of xx". These -echoOut prints out some helpful info, but not the test name.
    – franzcatch
    Feb 10, 2017 at 21:34
5

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'
}
3

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...

0

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'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.