Junit5: Gradle test runner does not display names for parameterized tests

Created on 4 Oct 2019  路  11Comments  路  Source: junit-team/junit5

The full name of @ParameterizedTest is never printed in Gradle output.

Reproducer: https://github.com/junit-team/junit5-samples/pull/116

That really hurts.
I can add relevant information to the Assertions.assert...(..., message=...), however, it won't be printed in case the test fails with NPE in the middle :-/

Gradle invalid

Most helpful comment

As a temporary workaround you could do this:

test {
    useJUnitPlatform()
    afterTest { descriptor, result ->
        println "\n$descriptor.className [$descriptor.classDisplayName] > $descriptor.name [$descriptor.displayName]: $result.resultType"
    }
}

All 11 comments

I know this isn't great and by all means should be improved in Gradle. For now, I can only say please vote for gradle/gradle#5975 to be resolved. FWIW the display names are shown in the HTML report.

Screenshot 2019-10-05 at 11 02 52

I see, thank you.

However the key problem for me is analyzing CI failures, and html is not always available there

As a temporary workaround you could do this:

test {
    useJUnitPlatform()
    afterTest { descriptor, result ->
        println "\n$descriptor.className [$descriptor.classDisplayName] > $descriptor.name [$descriptor.displayName]: $result.resultType"
    }
}

Just in case, it is a bit harder in Kotlin DSL: https://github.com/gradle/gradle/issues/5431

Yes, for Kotlin you'll have to do it like this or use a TestListener:

tasks.test {
    useJUnitPlatform()
    afterTest(KotlinClosure2<TestDescriptor, TestResult, Any>({ descriptor, result ->
        val test = descriptor as TestDescriptorInternal
        println("\n${test.className} [${test.classDisplayName}] > ${test.name} [${test.displayName}]: ${result.resultType}")
    }))
}

What should be the imports then?

I guess jupiter's TestDescriptor is not on the classpath(need to
doublecheck), and Gradle's one does not have getDisplayName()

Vladimir

>

@vlsi The only import that was required for me was import org.gradle.api.internal.tasks.testing.TestDescriptorInternal

My complete snippet is:

tasks.withType<Test> {
  group = "verification"
  useJUnitPlatform()
  afterTest(KotlinClosure2<TestDescriptor, TestResult, Any>({ descriptor, result ->
    val test = descriptor as TestDescriptorInternal
    val classDisplayName = if(test.className ==test.classDisplayName) test.classDisplayName else "${test.className} [${test.classDisplayName}]"
    val testDisplayName = if(test.name ==test.displayName) test.displayName else "${test.name} [${test.displayName}]"
    println("\n$classDisplayName > $testDisplayName: ${result.resultType}")
  }))
}

@checketts , it is often the case that test.className is full class name (package + class), while test.classDisplayName is just simple class name.

So I used just full class name when test.classDisplayName adds nothing:

val classDisplayName = test.className?.let {
    if (it.endsWith(test.classDisplayName)) it else "${test.className} [${test.classDisplayName}]"
} ?: test.classDisplayName

if the workaround can do it that shows that the data is actually there. why can't the gradle test plugin do the same?

I could work on this if this is something that would get merged and someone could point me to the right place to start looking.

The fix has already been merged: https://github.com/gradle/gradle/pull/11026

Was this page helpful?
0 / 5 - 0 ratings