Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

grails test-app to output to console

Tags:

grails

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?

like image 473
Ulrich Enslin Avatar asked Feb 07 '10 18:02

Ulrich Enslin


People also ask

How do I run a Grails test from command line?

You can run your Grails tests using the test-app command: If you want to run only unit tests or integration/functional tests, you can pass in a command line flag to choose one or the other. You can also run a specific test by passing the test class as an argument:

How do integration tests work in Grails?

Grails uses the test environment for integration tests and loads the application prior to the first test run. All tests use the same application state. Integration test methods run inside their own database transaction by default, which is rolled back at the end of each test method.

How do I create a grails app?

Creating a Grails app is about as simple as could be - once you have installed Grails, simply run the create-app command: If you don’t specify a package, the app name will be used as the default package for the application (e.g., myapp ).

How do I use Grails'interactive mode?

You can also use Grails' Interactive Mode to run the Grails runtime, from which you can issue any Grails command without waiting for the runtime to spin up for each task. In this guide we will be preferring the Grails wrapper for most commands.


3 Answers

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
like image 85
Ted Naleid Avatar answered Oct 05 '22 01:10

Ted Naleid


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'
}
like image 29
John Wagenleitner Avatar answered Oct 05 '22 01:10

John Wagenleitner


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

like image 44
jpswain Avatar answered Oct 05 '22 02:10

jpswain