【问题标题】:grails test-app to output to consolegrails test-app 输出到控制台
【发布时间】:2011-01-14 03:26:00
【问题描述】:

我是来自 Django 的 grails 新手。

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

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

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

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

【问题讨论】:

    标签: grails


    【解决方案1】:

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

    grails test-app -echoOut
    

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

    回显 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), 一定要去掉“Tests”后缀:

    grails test-app com.foo.MyController
    

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

    grails test-app -rerun
    

    【讨论】:

    • 你知道如何输出junit报告的实际错误吗? IE。 “预期 foo,但得到 bar”。即使使用 -testOut 和 -testErr,它也不会显示,我必须查看 target/test-reports/plain/*.txt 文件。
    • 这里有同样的问题。我必须这样做: $ grails test-app -echoOut -echoErr Something.testMain ; cat ./target/test-reports/plain/TEST-integration-integration-SomethingTests.txt 有没有人知道一种方法可以真正让它输出所有内容?谢谢!
    • 感谢您发布这些内容。我只希望我能找到一种方法让控制台输出打印它正在运行的测试名称而不是“1 of xx”。这些 -echoOut 打印出一些有用的信息,但不是测试名称。
    【解决方案2】:

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

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

    【讨论】:

      【解决方案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 ... }

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

      【讨论】:

        【解决方案4】:

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

        debug 'grails.app'
        

        但还要确保您正在测试的模块已打开日志记录。

        例如,如果您在测试中看到这样的失败

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

        调试日志没有显示出来,那么你可能还需要为你的包添加日志

        debug 'org.company'
        

        【讨论】:

          猜你喜欢
          • 2012-09-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-03-18
          • 2011-05-22
          • 1970-01-01
          相关资源
          最近更新 更多