Gradle: Users should be able to configure test logging using Kotlin DSL

Created on 16 May 2018  Â·  3Comments  Â·  Source: gradle/gradle

@eriwen commented on Wed Apr 25 2018

It looks like we don't have Action accepting overloads for AbstractTestTask beforeSuite, afterSuite, beforeTest, afterTest, and onOutput methods.

Expected Behavior

I should be able to configure test logging using the Kotlin DSL without creating extension functions.

Current Behavior

There is no easy equivalent to something like this in Kotlin:

afterSuite { desc, result ->
    if (!desc.parent) {
        println "\nTest result: ${result.resultType}"
        println "Test summary: ${result.testCount} tests, " +
        "${result.successfulTestCount} succeeded, " +
            "${result.failedTestCount} failed, " +
            "${result.skippedTestCount} skipped"
    }
}

The only Kotlin solution I could find is creating something like this in buildSrc.

Context

Looks like over 1000 GitHub projects call afterSuite in their test config. These projects will be harder to migrate to Kotlin DSL.

Your Environment

Kotlin DSL 0.16.3 in Gradle 4.7


@eskatos commented on Wed Apr 25 2018

This also applies to plugins implemented in Java, they would have to import groovy.lang.Closure and fiddle with the untyped Closure parameters.


@eskatos commented on Wed Apr 25 2018

One workaround is to use a typed TestListener instead, it is more verbose because you need to implement its 4 functions though, an adapter would help:

tasks.withType<Test> {
    addTestListener(object : TestListener {
        override fun beforeSuite(suite: TestDescriptor) {}
        override fun beforeTest(testDescriptor: TestDescriptor) {}
        override fun afterTest(testDescriptor: TestDescriptor, result: TestResult) {}
        override fun afterSuite(suite: TestDescriptor, result: TestResult) {}
    })
}

For test output it is simpler given the listener type is a SAM an can be expressed as a lambda:

tasks.withType<Test> {
    addTestOutputListener { testDescriptor, outputEvent -> 
        // ...
    }
}

---

@eskatos commented on [Wed Apr 25 2018](https://github.com/gradle/kotlin-dsl/issues/836#issuecomment-384212274)

For three of these methods (`afterTest()`, `afterSuite()` and `onOutput()`) the passed "closure" takes two parameters. `Action` is out of question in those cases. Note that `BiAction` is currently internal.

---

@eskatos commented on [Wed Apr 25 2018](https://github.com/gradle/kotlin-dsl/issues/836#issuecomment-384333766)

Another potential approach without `BiAction`:
```java
interface TestDescriptorEvent<T> {
    TestDescriptor getDescriptor();
    T getEvent();
}

```java
class AbstractTestTask {
void onOutput(Action> action);
}

```kotlin
tasks.withType<Test> {
    onOutput {
        println("$descriptor -> $event")
    }
}

@eriwen commented on Wed Apr 25 2018

This last solution came to my mind as well, and seems more forward-compatible as it guards against the TestListener interface getting more methods.


@eriwen commented on Wed Apr 25 2018

One last idea that came to my mind is Action<Pair<TestDescriptor, TestResult>> action — is that possible?


@bamboo commented on Thu Apr 26 2018

@eriwen Yes. It would imply usage similar to the following though:

tasks.withType<Test> {
    onOutput { 
        println("$first -> $second")
    }
}

@eskatos commented on Thu Apr 26 2018

Moreover, the changes need to happen in the Gradle Java API where we don't have a public Pair type. So far my preference goes to https://github.com/gradle/kotlin-dsl/issues/836#issuecomment-384333766


@eriwen commented on Fri May 04 2018

Agreed @eskatos. Should we solicit community help to address this?

feature member testing stale

Most helpful comment

any update on this since last year?

All 3 comments

any update on this since last year?

This issue has been automatically marked as stale because it has not had recent activity. Given the limited bandwidth of the team, it will be automatically closed if no further activity occurs. If you're interested in how we try to keep the backlog in a healthy state, please read our blog post on how we refine our backlog. If you feel this is something you could contribute, please have a look at our Contributor Guide. Thank you for your contribution.

This issue has been automatically closed due to inactivity. If you can reproduce this on a recent version of Gradle or if you have a good use case for this feature, please feel free to reopen the issue with steps to reproduce, a quick explanation of your use case or a high-quality pull request.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kevinrushforth picture kevinrushforth  Â·  115Comments

ijuma picture ijuma  Â·  40Comments

bmuschko picture bmuschko  Â·  45Comments

big-guy picture big-guy  Â·  44Comments

lacasseio picture lacasseio  Â·  102Comments