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

时间:2020-10-05 11:03:11

标签: android unit-testing kotlin intellij-idea 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的内容。

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 :(得分:0)

此问题是由Kotlin问题跟踪器中已描述的错误引起的,请参见here。如果要运行仅限Android的测试,则应使用名为test<Debug | Release>UnitTest的Gradle任务,而不要使用allTests,后者是您所用的唯一iOS。