简体   繁体   English

Kotlin 多平台 Gradle 单元测试无法解析 kotlin.test 参考

[英]Kotlin Multiplatform Gradle unit test not resolving kotlin.test reference

I am trying to test a Kotlin class in my common library for my Kotlin Multiplatform project in Android Studio.我正在尝试为 Android Studio 中的 Kotlin Multiplatform 项目测试公共库中的 Kotlin 类。

I have had to reconfigure the build.gradle file several times and managed to fix most of the unresolved references, but Gradle still can't find the reference for the @Test annotation, while the editor recognizes that it is from the kotlin.test library.我不得不多次重新配置build.gradle文件并设法修复了大部分未解析的引用,但是 Gradle 仍然找不到@Test注释的引用,而编辑器识别出它来自kotlin.test库.

Here is my test class:这是我的测试课:

import kotlin.test.*
import kotlinx.serialization.json.*
import Recipe

class RecipeTest {
    @Test
    fun serializeTest() {
        val keys = arrayOf("Dessert", "Cookies", "Cute")
        val ingredients = arrayOf("12 cups sugar", "2 cups flour", "1 bottle warm love")
        val instructions = arrayOf("Sift together in bowl", "Cook however else you see fit!")
        val recipe = Recipe(
            "Macaroons",
            "Morgan",
            "Today",
            "small cookies",
            "1 hour",
            keys,
            "1 dozen macaroons",
            "Dessert",
            "French",
            false,
            ingredients,
            instructions,
            true
        )
        val jsonString = JSON.stringify(Recipe.serializer(), recipe)
        val obj = JSON.parse(Recipe.serializer(), jsonString)
        assertEquals(Recipe.toString(), jsonString)
        assertEquals(Recipe.toString(), obj.toString())
    }
}

And my module build.gradle file:我的模块 build.gradle 文件:

plugins {
    id("com.android.library")
}

apply plugin: 'kotlin-multiplatform'
apply plugin: 'kotlinx-serialization'

android {
    compileSdkVersion = 28
    buildToolsVersion = '28.0.3'
    defaultConfig {
        minSdkVersion 21
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
    }
    sourceSets {
        main {
            manifest.srcFile 'src/androidMain/AndroidManifest.xml'
        }
    }
}

kotlin {
    android {

    }
    iosArm64 {
        binaries {
            executable()
        }
    }
    iosX64 {
        binaries {
            executable()
        }
    }

    sourceSets {
        commonMain {
            dependencies {
                implementation 'org.jetbrains.kotlin:kotlin-stdlib-common'
                implementation 'org.jetbrains.kotlinx:kotlinx-serialization-runtime:0.9.1'
            }
        }

        commonTest {
            dependencies {
                implementation 'org.jetbrains.kotlin:kotlin-stdlib-common'
                implementation 'org.jetbrains.kotlinx:kotlinx-serialization-runtime:0.9.1'
                implementation kotlin('test')
                implementation kotlin('test-common')
                implementation kotlin('test-annotations-common')
            }
        }
        androidMain {
            dependencies {
                implementation 'org.jetbrains.kotlin:kotlin-stdlib'
                implementation kotlin('test-common')
                implementation kotlin('test-annotations-common')
            }
        }

        iosMain {

        }
    }
}

configurations {
    compileClasspath
}

When the test in run from command line, the build fails with an exception, saying当从命令行运行测试时,构建失败并出现异常,说

Unresolved reference: test未解决的参考:测试

at the line with the @Test annotation.在带有@Test注释的那一行。

EDIT: I changed the accepted answer to the one that appears to be most helpful to others, but I'm leaving my old one just in case.编辑:我将已接受的答案更改为对其他人最有帮助的答案,但为了以防万一,我将保留旧答案。

I had a similar issue and found that I needed to explicitly add the platform specific Kotlin test dependencies:我有一个类似的问题,发现我需要显式添加特定于平台的 Kotlin 测试依赖项:

kotlin {

  // ...

  sourceSets {
    commonTest {
      dependencies {
        implementation "org.jetbrains.kotlin:kotlin-test-annotations-common"
        implementation "org.jetbrains.kotlin:kotlin-test-common"
      }
    }

    jvmTest {
      dependencies {
        implementation "org.jetbrains.kotlin:kotlin-test-junit"
      }
    }

    jsTest {
      dependencies {
        implementation "org.jetbrains.kotlin:kotlin-test-js"
      }
    }
  }
}

With only the commonTest dependencies I received the "Unresolved reference: test" error.仅使用commonTest dependencies我收到了“未解析的参考:测试”错误。 Once I added the jvmTest and jsTest blocks it fixed the error.添加jvmTestjsTest块后,它修复了错误。

As it turns out, I had some conflicting dependencies in my commonTest source set after all. 事实证明,我的commonTest源集中毕竟有一些冲突的依赖关系。 The 'test' dependency was conflicting with the 'test-common', which led to problems that were buried in some build logs. “测试”依赖项与“常见测试”冲突,这导致一些构建日志中隐藏的问题。 After deleting the extra dependencies, the build succeeded and the test ran. 删除额外的依赖项后,构建成功运行了测试。 ( and passed!) 通过!)

sourceSets {
   ...
   commonTest {
      dependencies {
         //only these are needed
         implementation kotlin('test-common')
         implementation kotlin('test-annotations-common')
      }
   }
   ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM