在使用 gradle 运行 junit 测试时,我很难找到有关如何显示日志输出的好信息。为了保持 Logger 独立性,我在项目中使用 Apache commons-logging。
现在我发现了这个,但它并没有真正解释任何事情: 单元测试中的 Gradle java.util.logging.Logger 输出
使用 http://gradle.org/logging 我得到了很好的理解如何从 gradle 任务进行日志记录,但不是如何显示我的测试中的日志消息。
gradle -i 也不是我想要的。尽管这为我提供了一些有关测试的信息,但它没有为我提供日志输出。
我很欣赏任何正确方向的提示。
编辑(在这里做,因为这是对彼得答案的相当长的回复):
添加
test {
testLogging.showStandardStreams = true
}
工作,有点。这是 Gradle 1.0-milestone-6 的功能,所以我首先必须升级 Gradle。
然后,我在 src/test/java 文件夹中的 log4j.properties 找不到。添加
sourceSets.test.runtimeClasspath += files(sourceSets.test.java.srcDirs)
到 build.gradle 文件至少消除了警告,但我没有任何日志输出。我的 log4j.xml 不应该是问题,它在 IDE 中工作:
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p %c{1} - %m%n"/>
</layout>
</appender>
<root>
<priority value="info"/>
<appender-ref ref="console"/>
</root>
</log4j:configuration>
为了完整起见,我在 google 上搜索了 showStandardStream 并找到了我应该找到的信息: http://gradle.org/current/docs/dsl/org.gradle.api.tasks.testing.Test.html
不过,问题仍然存在,因为它似乎不起作用:)
编辑2:
现在gradle test -i
打印日志信息输出。虽然它并不完全是我想要的(因为它与 gradle 日志记录混合在一起),但这已经足够好了。
I have a lot of trouble finding good information on how to get my Logging output displayed when running my junit tests with gradle. To stay Logger-independent, I use Apache commons-logging in my project.
Now I found this, but it doesn't really explain anything: Gradle java.util.logging.Logger output in unit tests
With http://gradle.org/logging I get a good understanding how to log from gradle tasks, but not how to display the logging messages from my tests.
gradle -i
is also not what I am looking for. Although that gives me some information about the tests, it does not give me the logging output.
I appreciate any hints in the right directions.
EDIT (doing it here since it's quite a long reply to Peter's answer):
Adding
test {
testLogging.showStandardStreams = true
}
worked, kinda. This is a Gradle 1.0-milestone-6 feature, so I first had to upgrade Gradle.
Then, my log4j.properties which was in the src/test/java folder could not be found. Adding
sourceSets.test.runtimeClasspath += files(sourceSets.test.java.srcDirs)
to the build.gradle file at least silenced the warning, but I didn't any logging output. My log4j.xml shouldn't be the problem, it worked in the IDE:
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p %c{1} - %m%n"/>
</layout>
</appender>
<root>
<priority value="info"/>
<appender-ref ref="console"/>
</root>
</log4j:configuration>
Just for the sake of completeness, I googled showStandardStream and found the info I should have found to begin with: http://gradle.org/current/docs/dsl/org.gradle.api.tasks.testing.Test.html
Question still open, though, since it didn't seem to work :)
EDIT2:
Now gradle test -i
prints the Logging info output. Although it is not exactly what I was looking for (since it is mixed with the gradle logging), this is good enough.
发布评论
评论(1)
您可以将
test.testLogging.showStandardStreams
设置为true
,然后确保您的测试日志记录打印到标准输出。这将使日志输出同时出现在命令行和测试报告中。You could set
test.testLogging.showStandardStreams
totrue
and then make sure that your test logging prints to std out. This will make the logging output appear both on the command line and in test reports.