繁体   English   中英

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

[英]Error Running Unit Tests for Kotlin Multiplatform Project

我按照本网站的教程使用 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. 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'
        }
    }
}

示例测试如下所示:

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

配置如下所示:

配置

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

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

此问题是由 Kotlin 问题跟踪器中已描述的错误引起的,请参见此处 如果您想运行仅适用于 Android 的测试,您应该使用名为test<Debug | Release>UnitTest Gradle 任务test<Debug | Release>UnitTest test<Debug | Release>UnitTest而不是allTests ,其中包括您案例中唯一的 iOS。

暂无
暂无

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

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