Kotlin 多平台:kotlinx.coroutines.test 库在 commonTest 模块中不可见
Kotlin multiplatform: kotlinx.coroutines.test library not visible in commonTest module
我很难将协程的 kotlin 测试框架 kotlinx.coroutines.test
与面向 Android(以及未来的 ios)的多平台项目相集成
我的常用代码严重依赖协程,但我无法测试它们(看起来测试库不在类路径中)
build.gradle
通用模块:
plugins {
id 'org.jetbrains.kotlin.multiplatform'
id 'com.android.library'
id 'kotlin-kapt'
}
android {
defaultConfig {
compileSdkVersion 28
javaCompileOptions.annotationProcessorOptions.includeCompileClasspath = true
}
lintOptions {
checkAllWarnings true
}
}
def coroutinesVersion = "1.3.0-M2"
def mockKVersion = "1.9.3"
kotlin {
targets {
fromPreset(presets.android, 'android')
}
sourceSets {
commonMain.dependencies {
//Kotlin
implementation "org.jetbrains.kotlin:kotlin-stdlib-common:$kotlin_version"
implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version" //used in Log implementation
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core-common:$coroutinesVersion"
}
commonTest.dependencies {
//Kotlin
implementation "org.jetbrains.kotlin:kotlin-test-common:$kotlin_version"
implementation "org.jetbrains.kotlin:kotlin-test-annotations-common:$kotlin_version"
//Coroutines testing
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core-common:$coroutinesVersion"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:$coroutinesVersion"
implementation "io.mockk:mockk-common:$mockKVersion"
}
androidMain.dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutinesVersion"
}
androidTest.dependencies {
implementation "org.jetbrains.kotlin:kotlin-test:$kotlin_version"
implementation "org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutinesVersion"
implementation "io.mockk:mockk:$mockKVersion"
}
}
}
/*
* Due to the current limitations, it requires that the Android target is created before the kapt dependencies are configured,
* which needs to be done in a top-level dependencies { ... } block rather than within Kotlin source sets dependencies.
*/
dependencies {
implementation 'javax.annotation:javax.annotation-api:1.3.2'
kapt 'com.google.auto.factory:auto-factory:1.0-beta6@jar'
compileOnly "com.google.auto.factory:auto-factory:1.0-beta6"
}
commonTest
源集中的示例测试:
package some.package.common.test
import kotlin.test.Test
import kotlin.test.assertTrue
class SomeTest {
@Test
fun `should pass`() {
//none of the kotlinx.coroutines.test content is available here (eg. runBlockingTest)
assertTrue { 2 + 2 == 4 }
}
}
原来kotlinx.coroutines.test
是JVM库,不能用在common模块中
可能的解决方案:
- 编写测试并使用 Android(或其他 JVM 兼容)模块中的库
- 定义您自己的常用函数,该函数将使用 JVM/Native runBlocking 的实现(参见 https://youtrack.jetbrains.com/issue/KT-22228)
//TestUtil.kt in commonTest source set
expect fun runBlocking(block: suspend () -> Unit)
//TestUtil.kt in androidTest source set
actual fun runBlocking(block: suspend () -> Unit) = kotlinx.coroutines.runBlocking { block() }
//example usage in commonTest source set
class Test {
@Test
fun shouldPass() = runBlocking {
assertTrue { 2 + 2 == 4 }
}
}
实际上,看起来他们 fixed it。现在,您应该可以在 commonTest 中使用 kotlinx.coroutines.test。
据我所知,随着新的 Kotlin 1.6 版本的发布,您应该能够在共享的 KMM 代码
中使用 this 到 运行 测试
我很难将协程的 kotlin 测试框架 kotlinx.coroutines.test
与面向 Android(以及未来的 ios)的多平台项目相集成
我的常用代码严重依赖协程,但我无法测试它们(看起来测试库不在类路径中)
build.gradle
通用模块:
plugins {
id 'org.jetbrains.kotlin.multiplatform'
id 'com.android.library'
id 'kotlin-kapt'
}
android {
defaultConfig {
compileSdkVersion 28
javaCompileOptions.annotationProcessorOptions.includeCompileClasspath = true
}
lintOptions {
checkAllWarnings true
}
}
def coroutinesVersion = "1.3.0-M2"
def mockKVersion = "1.9.3"
kotlin {
targets {
fromPreset(presets.android, 'android')
}
sourceSets {
commonMain.dependencies {
//Kotlin
implementation "org.jetbrains.kotlin:kotlin-stdlib-common:$kotlin_version"
implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version" //used in Log implementation
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core-common:$coroutinesVersion"
}
commonTest.dependencies {
//Kotlin
implementation "org.jetbrains.kotlin:kotlin-test-common:$kotlin_version"
implementation "org.jetbrains.kotlin:kotlin-test-annotations-common:$kotlin_version"
//Coroutines testing
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core-common:$coroutinesVersion"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:$coroutinesVersion"
implementation "io.mockk:mockk-common:$mockKVersion"
}
androidMain.dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutinesVersion"
}
androidTest.dependencies {
implementation "org.jetbrains.kotlin:kotlin-test:$kotlin_version"
implementation "org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutinesVersion"
implementation "io.mockk:mockk:$mockKVersion"
}
}
}
/*
* Due to the current limitations, it requires that the Android target is created before the kapt dependencies are configured,
* which needs to be done in a top-level dependencies { ... } block rather than within Kotlin source sets dependencies.
*/
dependencies {
implementation 'javax.annotation:javax.annotation-api:1.3.2'
kapt 'com.google.auto.factory:auto-factory:1.0-beta6@jar'
compileOnly "com.google.auto.factory:auto-factory:1.0-beta6"
}
commonTest
源集中的示例测试:
package some.package.common.test
import kotlin.test.Test
import kotlin.test.assertTrue
class SomeTest {
@Test
fun `should pass`() {
//none of the kotlinx.coroutines.test content is available here (eg. runBlockingTest)
assertTrue { 2 + 2 == 4 }
}
}
原来kotlinx.coroutines.test
是JVM库,不能用在common模块中
可能的解决方案:
- 编写测试并使用 Android(或其他 JVM 兼容)模块中的库
- 定义您自己的常用函数,该函数将使用 JVM/Native runBlocking 的实现(参见 https://youtrack.jetbrains.com/issue/KT-22228)
//TestUtil.kt in commonTest source set
expect fun runBlocking(block: suspend () -> Unit)
//TestUtil.kt in androidTest source set
actual fun runBlocking(block: suspend () -> Unit) = kotlinx.coroutines.runBlocking { block() }
//example usage in commonTest source set
class Test {
@Test
fun shouldPass() = runBlocking {
assertTrue { 2 + 2 == 4 }
}
}
实际上,看起来他们 fixed it。现在,您应该可以在 commonTest 中使用 kotlinx.coroutines.test。 据我所知,随着新的 Kotlin 1.6 版本的发布,您应该能够在共享的 KMM 代码
中使用 this 到 运行 测试