10

我遵循了https://kotlinlang.org/docs/tutorials/native/mpp-ios-android.html的教程,我想对我在 SharedModule 中创建的 commonTest 进行单元测试。

在此处输入图像描述

我尝试过的事情:

我曾尝试在公共文件中使用 kotlin.test。我在网上研究你可以使用JUnit5,但是当我在commonTest中导入依赖项时,我无法访问kotlin.test的库。现在我已经在 commonTest 中成功实现了 kotlin.test,但是我该如何运行呢?

我试过 gradlew commonTest.kt 但它不起作用。请帮忙谢谢!

我的 SharedModule gradle 代码:

    apply plugin: 'kotlin-multiplatform'

kotlin {
    targets {
        final def iOSTarget = System.getenv('SDK_NAME')?.startsWith("iphoneos") \
                              ? presets.iosArm64 : presets.iosX64

        fromPreset(iOSTarget, 'iOS') {
            binaries {
                framework('SharedCode')
            }
        }

        fromPreset(presets.jvm, 'android')
    }

    sourceSets {
        commonMain.dependencies {
            api 'org.jetbrains.kotlin:kotlin-stdlib-common'
            api 'org.jetbrains.kotlin:kotlin-stdlib'
            //implementation("com.ionspin.kotlin:bignum:0.0.8")

            //Testing
            //implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core-common:$coroutines_version"
            //implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime-common:$serialization_version"
/*
            implementation "io.ktor:ktor-client-core:$ktor_version"
            implementation "io.ktor:ktor-client-json:$ktor_version"
            implementation "io.ktor:ktor-client-logging:$ktor_version"*/

        }

        commonTest.dependencies{
            //implementation ("org.junit.jupiter:junit-jupiter-api:5.2.0")
            //implementation ("org.junit.jupiter:junit-jupiter-engine:5.0.3")
            //implementation 'junit:junit:4.12'
            implementation 'org.jetbrains.kotlin:kotlin-test-junit5:1.3.31'
            implementation 'org.jetbrains.kotlin:kotlin-test:1.3.31'
            //implementation "io.mockk:mockk-common:1.9.3"
            //implementation 'io.kotlintest:kotlintest-runner-junit5:3.3.0'

            //Testing
            //implementation "org.jetbrains.kotlin:kotlin-test-common"
            //implementation "org.jetbrains.kotlin:kotlin-test-annotations-common"

            //implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutines_version"
            implementation "io.mockk:mockk:1.8.13.kotlin13"
            implementation "io.mockk:mockk-common:1.8.13.kotlin13"
            implementation 'org.amshove.kluent:kluent:1.42'
        }

        androidMain.dependencies {
            api 'org.jetbrains.kotlin:kotlin-stdlib'
        }
    }
}

// workaround for https://youtrack.jetbrains.com/issue/KT-27170
configurations {
    compileClasspath
}

task packForXCode(type: Sync) {
    final File frameworkDir = new File(buildDir, "xcode-frameworks")
    final String mode = project.findProperty("XCODE_CONFIGURATION")?.toUpperCase() ?: 'DEBUG'
    final def framework = kotlin.targets.iOS.binaries.getFramework("SharedCode", mode)

    inputs.property "mode", mode
    dependsOn framework.linkTask

    from { framework.outputFile.parentFile }
    into frameworkDir

    doLast {
        new File(frameworkDir, 'gradlew').with {
            text = "#!/bin/bash\nexport 'JAVA_HOME=${System.getProperty("java.home")}'\ncd '${rootProject.rootDir}'\n./gradlew \$@\n"
            setExecutable(true)
        }
    }
}
tasks.build.dependsOn packForXCode
4

1 回答 1

5

我能够根据mpp-example git repo 让它工作。我的 gradle 文件看起来像:

kotlin {
    sourceSets {
        commonTest {
            dependencies {
                implementation kotlin('test-common')
                implementation kotlin('test-annotations-common')
            }
        }
        iosTest {
            dependsOn commonTest //possibly not needed
        }
    }
}
task iosTest {
    def device = project.findProperty("iosDevice")?.toString() ?: "iPhone 8"
    dependsOn 'linkTestDebugExecutableIos'
    group = JavaBasePlugin.VERIFICATION_GROUP
    description = "Runs tests for target 'ios' on an iOS simulator"
    
    doLast {
        def binary = kotlin.targets.ios.binaries.getExecutable('test', 'DEBUG').outputFile
        exec {
            commandLine 'xcrun', 'simctl', 'spawn', device, binary.absolutePath
        }
    }
}

主要缺点是测试必须在终端中运行,之后它们不会输出漂亮的 html 文件。所有结果都显示在终端中。

于 2019-05-23T18:19:54.347 回答