我已经按照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
答案 0 :(得分:1)
我能够根据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文件。所有结果都显示在终端中。