为 Kotlin 多平台项目运行单元测试时出错

gre*_*qrl 5 android unit-testing intellij-idea kotlin kotlin-multiplatform

我按照本网站的教程使用 Intelli-j IDE(社区版)创建了一个 Kotlin 多平台项目:

https://medium.com/@cafonsomota/set-up-your-first-kotlin-multiplatform-project-for-android-and-ios-april-2020-258e2b1d9ef4

我没有遵循的是教程的 xCode 部分,因为此时虽然我希望这个项目是多平台的,但我的主要兴趣是 Android。

当我运行common示例测试时,我看到错误: e: org.jetbrains.kotlin.konan.MissingXcodeException: An error occurred during an xcrun execution. Make sure that Xcode and its command line tools are properly installed.

我还可以看到,对于相关配置,任务详细说明如下: cleanIosTest iosTest

这就是我收到错误的原因。

我不知道如何更改Sample Test以不运行该配置。我尝试删除这些任务、应用并保存,但当我运行它们时它们不断重新出现。我在 build.gradle 文件中看不到任何表明 iOS 特定于测试的内容。

构建.gradle.app

    plugins {
    id 'org.jetbrains.kotlin.multiplatform' version '1.3.72'
}
repositories {
    google()
    jcenter()
    mavenCentral()
}
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId 'org.jetbrains.kotlin.mpp_app_android'
        minSdkVersion 15
        targetSdkVersion 28
        versionCode 1
        versionName '1.0'
        testInstrumentationRunner 'android.support.test.runner.AndroidJUnitRunner'
    }
    buildTypes {
        release {
            minifyEnabled false
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
}

kotlin {
    android("android")
    // This is for iPhone emulator
    // Switch here to iosArm64 (or iosArm32) to build library for iPhone device
    iosX64("ios") {
        binaries {
            framework()
        }
    }
    sourceSets {
        commonMain {
            dependencies {
                implementation kotlin('stdlib-common')
            }
        }
        commonTest {
            dependencies {
                implementation kotlin('test-common')
                implementation kotlin('test-annotations-common')
            }
        }
        androidMain {
            dependencies {
                implementation kotlin('stdlib')
            }
        }
        androidTest {
            dependencies {
                implementation kotlin('test')
                implementation kotlin('test-junit')
            }
        }
        iosMain {
        }
        iosTest {
        }
    }
}

// This task attaches native framework built from ios module to Xcode project
// (see iosApp directory). Don't run this task directly,
// Xcode runs this task itself during its build process.
// Before opening the project from iosApp directory in Xcode,
// make sure all Gradle infrastructure exists (gradle.wrapper, gradlew).
task copyFramework {
    def buildType = project.findProperty('kotlin.build.type') ?: 'DEBUG'
    def target = project.findProperty('kotlin.target') ?: 'ios'
    dependsOn kotlin.targets."$target".binaries.getFramework(buildType).linkTask

    doLast {
        def srcFile = kotlin.targets."$target".binaries.getFramework(buildType).outputFile
        def targetDir = getProperty('configuration.build.dir')
        copy {
            from srcFile.parent
            into targetDir
            include 'app.framework/**'
            include 'app.framework.dSYM'
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

示例测试如下所示:

package sample

import kotlin.test.Test
import kotlin.test.assertTrue

class SampleTests {
    @Test
    fun testMe() {
        assertTrue(Sample().checkMe() > 0)
    }

    @Test
    fun testProxy() {
        assertTrue(Proxy().proxyHello().isNotEmpty())
    }
}
Run Code Online (Sandbox Code Playgroud)

配置如下:

配置

有谁知道如何解决这个问题而不需要下载xCode?我很高兴分享任何其他信息,但不确定我应该为此分享什么。

顺便说一句,我确实创建了另一个没有该行的配置,但是当我在第一次测试中按下绿色的 PLAY 按钮时,它始终默认为Sample Test包含 iOS 任务的配置。

Art*_*rev 0

此问题是由 bug 引起的,已在 Kotlin 问题跟踪器中进行了描述,请参阅此处。如果您想运行仅限 Android 的测试,您应该使用名为的 Gradle 任务test<Debug | Release>UnitTest而不是allTests,其中包括您的情况中唯一的 iOS。

  • 我最终做的是创建一个新项目,但使其成为一个“移动多平台”库,而不是“Android\iOS”库。这样就不会在每个阶段强制执行 iOS,而是为我提供了选择“jvm”或“iOS”的选项 (2认同)