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

5
我使用Intelli-j IDE (community edition)创建了一个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特定的测试相关内容。
build.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'
        }
    }
}

示例测试看起来像这样:

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())
    }
}

配置如下:

Configuration

请问有没有人知道我怎样才能解决这个问题而不需要下载xCode?我很乐意分享其他信息,但对于这个问题,我并不确定应该分享什么信息。
顺便提一下,我创建了另一个没有那行代码的配置,但每当我按下第一个测试的绿色PLAY按钮时,它总是默认使用Sample Test配置并带有iOS任务。
1个回答

0

这个问题是由一个漏洞引起的,已经在 Kotlin 问题跟踪器中进行了描述,详见 here。如果您想运行仅适用于 Android 的测试,则应使用 Gradle 任务命名为test<Debug | Release>UnitTest,而不是包括您的情况下唯一的 iOS 的allTests


如果我理解 OP 所询问的是如何避免在按下“绿色播放”▶️按钮或其关联的 Gradle 任务时运行 iOS 测试。运行 test<Debug | Realease>UnitTest 是一个解决方法,它可以让您运行所有测试,并且不像“播放按钮”那样可以选择单个测试运行。 - shadowsheep
哦,我明白了。这更多是关于这个问题(https://youtrack.jetbrains.com/issue/KT-34535),而不是我提到的那个。但无论如何,我现在不确定是否有更好的解决方法来解决它。 - Artyom Degtyarev
2
我最终做的是创建一个新项目,但将其设置为“移动多平台”库,而不是“Android\iOS”库。这样就不会在每个阶段强制使用iOS,并且可以本地选择jvmiOS选项。 - greysqrl
什么是解决方案? - IgorGanapolsky

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接