将grails测试应用程序输出到控制台

将grails测试应用程序输出到控制台,grails,Grails,我是来自Django的grails新手 使用测试驱动开发,我习惯于编写测试,然后编写实际的功能。对我来说,最有效的方法是编写测试,使用一些调试输出运行函数以查看变量的状态,直到单元测试通过,然后移动调试输出 在grails“grails测试应用程序”中,“log.debug”和“println”的输出不会记录到控制台,也不会记录在报告中 文档指向使用mocklogging,它应该将log.debug调用输出到控制台,但是使用Grails1.2.1,我看不到任何输出 任何人都可以告诉我如何在控制台

我是来自Django的grails新手

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

在grails“grails测试应用程序”中,“log.debug”和“println”的输出不会记录到控制台,也不会记录在报告中

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


任何人都可以告诉我如何在控制台中查看“println”或“log.debug”的输出以加快我的开发速度吗?

将-echoot开关添加到grails测试应用程序,这是:

测试应用程序上还有许多其他有用的开关,包括:

echo System.err消息:

grails test-app -echoErr
在运行测试之前强制执行清理(不执行grails clean&&grails测试应用程序):

仅运行单元测试:

grails test-app unit:
仅运行集成测试:

grails test-app integration:
在特定环境中运行:

grails -Dgrails.env=production test-app
仅为特定测试类运行测试(如com.foo.MyControllerTests), 确保去掉“测试”后缀:

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

grails test-app -rerun

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

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

有一件事也可能对你有所帮助,那就是:

确保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 ...
}

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

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

debug 'grails.app'
但也要确保您正在测试的模块已打开日志记录

例如,如果您在测试中看到类似的失败

| Failure:  write a GFF3 of a simple gene model(org.company.YourAppIntegrationSpec)
并且调试日志没有显示,那么您可能还需要为包添加日志

debug 'org.company'

您知道如何输出junit报告的实际错误吗?也就是说,“预期的foo,但得到了bar”。即使使用-testOut和-testErr,它也不会显示,我必须查看target/testreports/plain/*.txt文件。这里也有同样的问题。我必须这样做:$grails测试应用程序-echoOut-Echorr something.testMain;cat./target/test reports/plain/test-integration-integration-SomethingTests.txt有人知道如何让它输出所有内容吗?谢谢!谢谢你发布这些。我只希望我能想出一种方法,让控制台输出打印它正在运行的测试名称,而不是“xx中的1”。这些-echoot打印出一些有用的信息,但不是测试名称。
debug 'grails.app'
| Failure:  write a GFF3 of a simple gene model(org.company.YourAppIntegrationSpec)
debug 'org.company'