简体   繁体   English

Kotlin 多平台项目单元测试问题

[英]Kotlin Multiplatform Project Unit Test Issue

Today I just updated my Android Studio to今天我刚刚将我的 Android Studio 更新为

Android Studio 3.5.2
Build #AI-191.8026.42.35.5977832, built on October 31, 2019
JRE: 1.8.0_202-release-1483-b49-5587405 x86_64
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.15.1

I updated the Android Studio because it prompts error to me when the kotlin version is below 1.3.60.我更新了 Android Studio,因为当 kotlin 版本低于 1.3.60 时,它会向我提示错误。

However I am facing problem with the unit test file.但是我面临单元测试文件的问题。 Previously I can run the unit test without any problem.以前我可以毫无问题地运行单元测试。 Now, when I click the run button besides the function name (on the left), it shows Nothing Here.现在,当我单击 function 名称(左侧)旁边的运行按钮时,它显示 Nothing Here。

在此处输入图像描述

Below is all my dependencies以下是我所有的依赖项

// build.gradle

buildscript {
ext.kotlin_version = '1.3.60'
repositories {
    google()
    jcenter()
    maven { url "https://kotlin.bintray.com/kotlinx" }
}

dependencies {
    // classpath 'com.android.tools.build:gradle:3.4.1'
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    classpath "org.jetbrains.kotlin:kotlin-serialization:$kotlin_version"
}
}

allprojects {
  repositories {
    google()
    jcenter()
  }
}

task clean(type: Delete) {
   delete rootProject.buildDir
}


 apply plugin: 'kotlin-multiplatform'
 apply plugin: 'kotlinx-serialization'

kotlin {
targets {
    final def iOSTarget = presets.iosX64 // Simulator

    fromPreset(iOSTarget, 'iOS') {
        binaries {
            framework('SharedCode')
        }
    }
    fromPreset(presets.jvm, 'android')
}

sourceSets {
    commonMain {
        dependencies {
            api 'org.jetbrains.kotlin:kotlin-stdlib-common'
            implementation "org.jetbrains.kotlin:kotlin-test-common:$kotlin_version"
            implementation "org.jetbrains.kotlin:kotlin-test-annotations-common:$kotlin_version"
            implementation "org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version"
            implementation "org.jetbrains.kotlin:kotlin-test:$kotlin_version"
        }
    }

    iOSMain {
        dependsOn commonMain
    }

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

    commonTest {
        dependsOn commonMain
    }
}
}

Is there something that I missed out in this new Android Studio and kotlin version.在这个新的 Android Studio 和 kotlin 版本中,我是否遗漏了什么。 I have been struggling with this for hours.我已经为此苦苦挣扎了几个小时。 Browsed web but seems couldn't find any help.浏览了 web 但似乎找不到任何帮助。

Thanks谢谢

In my case, this issues happened when I updated from就我而言,当我从

  • Android Studio 3.5.3 and Kotlin plugin version v1.3.50-release-Studio3.5-1 Android Studio 3.5.3和 Kotlin 插件版本v1.3.50-release-Studio3.5-1

to

  • Android Studio 3.6.3 with Kotlin plugin 1.3.71-release-Studio3.6-1带有 Kotlin 插件的Android Studio 3.6.3 1.3.71-release-Studio3.6-1

All of my common Kotlin MPP tests were not running from the expected IDE side function "green" run and displayed the Nothing here message too.我所有常见的 Kotlin MPP 测试都没有从预期的 IDE 侧函数“绿色”运行中运行,并且也显示了Nothing here消息。

I tried a few things, one of them being a full uninstall and then install but nothing seemed to work.我尝试了几件事,其中之一是完全卸载然后安装,但似乎没有任何效果。

What ended up working for me was (with some caveats):最终对我有用的是(有一些警告):

  • created a new test (works with existing tests too, but just as an example)创建了一个新测试(也适用于现有测试,但仅作为示例)

图1

to make sure the Nothing Here shows.以确保此处无显示。

Then:然后:

  • open the dropdown next to the Emulator selected one and click Edit Configurations打开所选模拟器旁边的下拉菜单,然后单击编辑配置
  • under Android JUnit click the + button to add a new configurationAndroid JUnit 下单击+按钮添加新配置
  • it should look like this:它应该是这样的:

图片2

make sure the test kind is Class and that you have the correct path to the file.确保测试类型是Class并且您有正确的文件路径。 For example:例如:

com.my.path.is.correct.MyNewTestCase

Also, make sure you have the correct module for the common Kotlin;另外,请确保您拥有适用于常见 Kotlin 的正确模块; in my case it was common (not commonTest).就我而言,它很常见(不是 commonTest)。

After the above you should be able to run the test class from the main Run button (again, next to the Emulator drop down list) and even run it as Debug .完成上述操作后,您应该能够从主Run按钮(再次,在 Emulator 下拉列表旁边)运行测试类,甚至将其作为Debug运行。

The "green" run button next to the function will still say Nothing here but adding a breakpoint and running the new configuration from the main Debug button does work for me:该函数旁边的“绿色”运行按钮仍然会在此处显示 Nothing但添加断点并从主调试按钮运行新配置对我有用:

在此处输入图片说明

This is obviously not perfect, but it was the only way to have it working from the IDE that I could find.这显然并不完美,但这是我能找到的让它在 IDE 中工作的唯一方法。

Hope it helps!希望能帮助到你!

Clean install Android Studio fixed this issue.全新安装 Android Studio 修复了此问题。 Never thought this problem will occurr.没想到会出现这个问题。 Updated Android Studio several times in the past, and this is my first such issue encounter.过去多次更新 Android Studio,这是我第一次遇到此类问题。

I have tried all the solutions that provided above, NONE worked for me.我已经尝试了上面提供的所有解决方案,没有一个对我有用。 Finally I kept only plugins that are bundled with Android studio and disabled every other (restart studio).最后,我只保留了与 Android studio 捆绑在一起的插件,并禁用了所有其他插件(重启 studio)。 I started seeing the menu on gutter to run the tests.我开始在 gutter 上看到运行测试的菜单。 Once this is fixed, I started enabling those disabled plugins ONE-BY-ONE.解决此问题后,我开始逐个启用那些禁用的插件。 If the plugin which cause this issue identified, then decide what to do with it based on your environment and dev requirement.如果确定导致此问题的插件,则根据您的环境和开发要求决定如何处理它。

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

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