简体   繁体   English

grails test-app输出到控制台

[英]grails test-app to output to console

I am new to grails, coming from Django. 来自Django的我是Grails的新手。

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. 在grails中,'grails test-app','log.debug'和'println'的输出不会记录到控制台,也不在报告中。

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

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? 任何人都可以告诉我如何在控制台中查看'println'或'log.debug'的输出以加快我的开发速度?

Add the -echoOut switch to grails test-app, this is new in the 1.2 release : 将-echoOut开关添加到grails test-app,这是1.2版本中的新功能

grails test-app -echoOut

There are a number of other useful switches on test-app as well, including: test-app上还有许多其他有用的开关,包括:

echo System.err messages: echo System.err消息:

grails test-app -echoErr

force a clean before running tests (without doing grails clean && grails test-app): 在运行测试之前强制清理(不用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: 仅针对特定测试类运行测试(如com.foo.MyControllerTests),请务必不要使用“Tests”后缀:

grails test-app com.foo.MyController

rerun only the tests that failed the last time you ran your tests 仅重新运行上次运行测试时失败的测试

grails test-app -rerun

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

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

One thing that might also help you is doing this: 有一件事可能对你有所帮助:

Make sure your Config.groovy sets up log4j: 确保您的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'
    }
}

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. 如果这样做,log的行为与在域,服务和控制器对象中的行为几乎相同,其中grails会自动为您注入它。 I have no idea why they don't auto-inject in tests... 我不知道他们为什么不在测试中自动注入......

The other answer about adding the log4j config in Config.groovy is important 关于在Config.groovy中添加log4j配置的另一个答案很重要

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'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM