grails test-app输出到控制台

时间:2010-02-07 18:57:47

标签: grails

我是来自Django的grails新手。

使用测试驱动开发,我习惯于编写测试,然后编写实际功能。对我来说,编写测试,运行带有一些调试输出的函数以查看变量状态直到单元测试通过,然后移动调试输出,这对我有用。

在grails中,'grails test-app','log.debug'和'println'的输出没有记录到控制台,也不在报告中。

文档指向使用mocklogging,它应该将log.debug调用输出到控制台,但是使用grails 1.2.1,我看不到任何输出。

任何人都可以告诉我如何在控制台中查看'println'或'log.debug'的输出以加快我的发展?

4 个答案:

答案 0 :(得分:78)

将-echoOut开关添加到grails test-app,这是new in the 1.2 release

grails test-app -echoOut

test-app上还有许多其他有用的开关,包括:

echo System.err消息:

grails test-app -echoErr

在运行测试之前强制清理(不要使用grails clean&& grails test-app):

grails test-app -clean

仅运行单元测试:

grails test-app unit:

仅运行集成测试:

grails test-app integration:

在特定环境中运行:

grails -Dgrails.env=production test-app

仅针对特定测试类运行测试(如com.foo.MyControllerTests), 一定要不要使用“测试”后缀:

grails test-app com.foo.MyController

仅重新运行上次运行测试时失败的测试

grails test-app -rerun

答案 1 :(得分:5)

要查看log.debug语句,您需要将以下内容添加到 grails-app / conf / Config.groovy 文件的log4部分:

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

答案 2 :(得分:3)

有一件事可能对你有所帮助:

确保您的Config.groovy设置了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'
    }
}


在测试中,请执行以下操作:


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

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

   ... your tests ...
}

如果这样做,log的行为与在域,服务和控制器对象中的行为几乎相同,其中grails会自动为您注入它。我不知道他们为什么不在测试中自动注入......

答案 3 :(得分:0)

关于在Config.groovy中添加log4j配置的另一个答案很重要

debug 'grails.app'

但也请确保您正在测试的模块已启用日志记录。

例如,如果你从测试中看到这样的一些失败

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

并且没有显示调试日志记录,那么您可能还需要添加包的日志记录

debug 'org.company'
相关问题