我正在使用JUnit 4编写Android测试用例(这些测试没有使用仿真程序,而是作为本地测试运行)。在我的代码中,我使用SL4J进行日志记录,但是当我运行单元测试时,我无法看到任何日志记录输出。例如,以下语句:
private static final Logger logger = LoggerFactory.getLogger(AClass.class);
logger.warn("log output not visible in unit test");对于是否可以在单元测试中访问记录器输出,有什么想法吗?
致以敬意,
发布于 2015-11-22 06:03:40
我以前已经回答过这个问题,但我现在找不到答案的链接。
这里是Android插件解决方案:
android {
// ...
testOptions.unitTests.all {
testLogging {
events 'passed', 'skipped', 'failed', 'standardOut', 'standardError'
}
}
}这里是我用于任何分级测试的解决方案:
tasks.withType(Test) {
testLogging {
exceptionFormat 'full'
showCauses true
showExceptions true
showStackTraces true
showStandardStreams true
}
}发布于 2022-11-04 03:51:26
@JaredBurrow答案的更新Kotlin DSL版本
参考文献:1
在项目 build.gradle.kts中
import org.gradle.api.tasks.testing.logging.TestExceptionFormat
import org.gradle.api.tasks.testing.logging.TestLogEvent
buildscript {
repositories {
google()
mavenCentral()
}
dependencies {...}
}
allprojects {
repositories {
google()
mavenCentral()
maven(url ="https://jitpack.io")
}
tasks.withType<Test>().configureEach {
testLogging {
events = setOf(
TestLogEvent.STARTED,
TestLogEvent.PASSED,
TestLogEvent.FAILED,
TestLogEvent.SKIPPED,
TestLogEvent.STANDARD_OUT,
TestLogEvent.STANDARD_ERROR
)
exceptionFormat = TestExceptionFormat.FULL
showExceptions = true
showCauses = true
showStackTraces = true
}
ignoreFailures = false
}
}https://stackoverflow.com/questions/33850781
复制相似问题