Observe stdout from multiplatform kotlin commonTest code
Asked Answered
N

4

6

I just want to see the output from a few simple println(...) in my Kotlin mulitplatform commonTest code. My build.gradle.kts looks a little like:

import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget

plugins {
    kotlin("multiplatform") version "1.3.61"
    kotlin("plugin.serialization") version "1.3.61"
}


kotlin {

   sourceSets {

        val commonMain by getting { ... }

        val commonTest by getting {
           dependencies {
              implementation("org.jetbrains.kotlin:kotlin-test-common")
              implementation("org.jetbrains.kotlin:kotlin-test-annotations-common")
           }
        }

        val jvmMain by getting { ... }

        val jvmTest by getting {
           dependencies {
              implementation(kotlin("test-junit"))
           }
        }

        // and so on ...

   }

}

Meanwhile in ~/src/commonTest/kotlin/my/company/library/CommonTest.kt:

package my.company.library

import kotlin.test.*

class CommonTest() {

   @Test
   fun testTrue() {
      println("Hello, test!")
      assertTrue(true)
   }

}

For the time being I'm running tests like this

./gradlew jvmTest

I want to see Hello, test! show up in the terminal. I don't mind typing a little extra on the command line.

Various answers around SO involving testLogging.showStandardStreams refer to the "standard" gradle test target, and I'm not sure how or if it actually interacts with the multiplatform test targets.

Nichollenicholls answered 19/2, 2020 at 22:1 Comment(1)
I've got the same issue but with Kotlin/Native, I've managed to see the result by opening the "output.bin" file located at "build/test-results/nativeTest/binary", but it might not be present in JVM tests. It's not the ideal solution, but at least I could see it. Hope you can too!Rida
A
1

You can make it work by adding this to your build.gradle.kts:

tasks.withType<Test> {
    testLogging {
        showStandardStreams = true
    }
}
Alcaic answered 3/3, 2021 at 20:41 Comment(0)
I
0

Add the following to your gradle config:

iosTest {
    testLogging {
        events("PASSED", "FAILED", "SKIPPED")
        exceptionFormat = "full"
        showStandardStreams = true
        showStackTraces = true
    }
}

for JVM it would be similar but then under the block jvmTest.

Ironist answered 7/1, 2021 at 21:24 Comment(0)
I
0

In my case, this worked:

afterEvaluate {
    tasks.withType<AbstractTestTask> {
        testLogging {
            showStandardStreams = true
        }
    }
}
Interpolate answered 28/4, 2022 at 10:32 Comment(0)
F
0

To enable standard output in your Kotlin Multi Platform tests you need to enable STANDARD_OUT events in your testLogging configuration.

// kotlin dsl
tasks.withType<AbstractTestTask>().configureEach {
    testLogging {
        events = setOf(
            TestLogEvent.STANDARD_OUT,
        )
    }
}
Febrifugal answered 11/9, 2024 at 14:58 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.