2015-03-03 60 views
2

中播放Logger V2.3测试不起作用:如何在Play framework v2.3测试中启用记录器?

"Test example" should { 
    "Test 1" in { 
    import play.api.Logger 

    Logger.debug("hello world") // doesn't work, no output on screen 
    } 
} 

但如果我WithApplication包装测试,它的工作原理:

"Test example" should { 
    "Test 1" in new WithApplication { 
    import play.api.Logger 

    Logger.debug("hello world") 
    } 
} 

但它给管理费,如果我必须包装使用WithApplication进行每项测试只是为了让Logger工作。

因此,任何人都知道如何让记录器在没有WithApplication的情况下工作?

+0

你从'application.conf启用记录器的例子'? – silentprogrammer 2015-03-03 11:36:13

+0

你在使用['scalatestplus'](http://www.scalatest.org/plus/play)吗?如果是的话,你可以使用['OneServerPerSuite'](http://doc.scalatest.org/plus-play/1.0.0/index.html#org.scalatestplus.play.OneServerPerSuite)或'OneServerPerTest'特性,假应用程序给你的上下文。如果你已经在使用['scalatest'](http://www.scalatest.org),这是一个下降的选择。 – 2015-03-03 11:52:09

+0

@singhakash:是的,如果没有,使用'WithApplication'的第二次测试将不会成功。 @Nader:我使用使用specs2的内置游戏测试库。 – null 2015-03-03 16:36:11

回答

0

如果您正在使用scalatestplus应覆盖FakeApplication额外的配置记录程序,在这里你有工作

import org.scalatest._ 
import play.api.test._ 
import play.api.test.Helpers._ 
import org.scalatestplus.play._ 

import play.api.Logger 

class TestExampleLogger extends PlaySpec with OneAppPerSuite { 

    // Override app if you need a FakeApplication with other than 
    // default parameters. 
    implicit override lazy val app: FakeApplication = FakeApplication(additionalConfiguration = Map(
    "logger.application" -> "DEBUG" 
)) 

    "Test example" should { 
    "Test 1" in { 
     import play.api.Logger 

     Logger.debug("hello world") // doesn't work, no output on screen 
    } 
    } 

} 

希望它可以帮助

+0

不起作用... – Ixx 2015-12-03 18:26:52

相关问题