java 通过gradle运行junit测试未显示预期结果

9wbgstp7  于 2023-06-28  发布在  Java
关注(0)|答案(2)|浏览(111)

运行:./gradlew test触发试运行成功:
任务:测试失败

PerfectNumberTest > Test calcPerfectNumbers() method FAILED
    org.opentest4j.AssertionFailedError at PerfectNumberTest.java:51

5 tests completed, 1 failed

然而,我没有得到输出,这将表明到底是什么在测试中出错,
例如:应为% 1,而不是% 2
这些是我使用的依赖项:

dependencies {
    testImplementation platform('org.junit:junit-bom:5.9.1')
    testImplementation 'org.junit.jupiter:junit-jupiter'
}
mefy6pfw

mefy6pfw1#

build.gradle文件使用以下配置:

import org.gradle.api.tasks.testing.logging.TestExceptionFormat
import org.gradle.api.tasks.testing.logging.TestLogEvent

// ...

tasks.withType(Test) {
    testLogging {
        // set options for log level LIFECYCLE
        events TestLogEvent.STARTED,
                   TestLogEvent.FAILED,
                   TestLogEvent.PASSED,
                   TestLogEvent.SKIPPED,
                   TestLogEvent.STANDARD_ERROR,
                   TestLogEvent.STANDARD_OUT
        exceptionFormat TestExceptionFormat.FULL
        showExceptions true
        showCauses true
        showStackTraces true
        info.events = debug.events
        info.exceptionFormat = debug.exceptionFormat

        afterSuite { desc, result ->
            if (!desc.parent) { // will match the outermost suite
                def duration = String.format("%.3f sec", (result.endTime - result.startTime) / 1000)
                def output = "Results: ${result.resultType} (${result.testCount} tests, ${result.successfulTestCount} passed, ${result.failedTestCount} failed, ${result.skippedTestCount} skipped) ${duration}"
                def startItem = '|  ', endItem = '  |'
                def repeatLength = startItem.length() + output.length() + endItem.length()
                println('\n' + ('-' * repeatLength) + '\n' + startItem + output + endItem + '\n' + ('-' * repeatLength))
            }
        }
    }
}

此配置将显示所有内容,甚至控制台输出
请参阅此帖子问题了解更多详细信息Gradle: How to Display Test Results in the Console in Real Time?

u2nhd7ah

u2nhd7ah2#

以下是Youans分享的配置的修改版本:

import org.gradle.api.tasks.testing.logging.TestExceptionFormat
import org.gradle.api.tasks.testing.logging.TestLogEvent

// ...

tasks.withType(Test) {
    testLogging {
        // set options for log level LIFECYCLE
        events TestLogEvent.FAILED,
                   TestLogEvent.SKIPPED,
                   TestLogEvent.STANDARD_ERROR,
                   TestLogEvent.STANDARD_OUT
        exceptionFormat TestExceptionFormat.FULL
        showExceptions true
        showCauses true
        showStackTraces false
        info.events = debug.events
        info.exceptionFormat = debug.exceptionFormat

        beforeSuite { desc ->
            if (!desc.name.startsWith("Gradle Test")) { 
                println "\nRunning ${desc.name}"
            }
        }

        afterSuite { desc, result ->
            def duration = String.format("%.3f sec", (result.endTime - result.startTime) / 1000)

            if (desc.name.startsWith("Gradle Test Executor")) {
                def output = "Result: ${result.resultType} (${result.testCount} tests, ${result.successfulTestCount} passed, ${result.failedTestCount} failed, ${result.skippedTestCount} skipped) ${duration}"
                def startItem = '|  ', endItem = '  |'
                def repeatLength = startItem.length() + output.length() + endItem.length()
                println('\n' + ('-' * repeatLength) + '\n' + startItem + output + endItem + '\n' + ('-' * repeatLength))
                return
            }

            if (!desc.name.startsWith("Gradle Test")) { 
                println "Tests run: ${result.testCount}, Failures: ${result.failedTestCount}, Errors: 0, Skipped: ${result.skippedTestCount} Time elapsed: ${duration}"
            }
        }
    }
}

修改:

  • 已更改套件检测以说明name模式
  • events中删除了TestLogEvent.STARTEDTestLogEvent.PASSED
  • showStackTraces更改为false

它打印以下最小输出:

Running gradle.example.FooTest

FooTest > someLibraryMethod() FAILED
    org.opentest4j.AssertionFailedError: expected: <1> but was: <2>
Tests run: 2, Failures: 1, Errors: 0, Skipped: 0 Time elapsed: 0.045 sec

Running gradle.example.LibraryTest

LibraryTest > someLibraryMethodReturnsTrue() FAILED
    org.opentest4j.AssertionFailedError: expected: <true> but was: <false>
Tests run: 1, Failures: 1, Errors: 0, Skipped: 0 Time elapsed: 0.006 sec

------------------------------------------------------------------------
|  Result: FAILURE (3 tests, 1 passed, 2 failed, 0 skipped) 0.331 sec  |
------------------------------------------------------------------------

3 tests completed, 2 failed

当然,可能应该保留堆栈跟踪,但这至少会显示预期/实际值。

相关问题