【问题标题】:Class not found, Empty test suite in androidTest using Android Studio 3.0.1, Room, Kotlin找不到类,使用 Android Studio 3.0.1、Room、Kotlin 的 androidTest 中的空测试套件
【发布时间】:2018-06-06 18:53:19
【问题描述】:

我在运行我的 androidTest 时遇到问题。

这是我在 gradle 中的设置:

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'

android {
compileSdkVersion 26
defaultConfig {
    applicationId "com.blabla.shoppinglistapp"
    minSdkVersion 17
    targetSdkVersion 26
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
 }
}

ext.daggerVersion = '2.11'
ext.roomVersion = '1.0.0'
ext.mockitoVersion = '2.11.0'

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
implementation 'com.android.support:design:26.1.0'
implementation 'com.android.support:recyclerview-v7:26.1.0'

testImplementation 'junit:junit:4.12'
testImplementation 'org.mockito:mockito-android:2.8.47'

androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'

// ViewModel and LiveData
implementation "android.arch.lifecycle:extensions:1.0.0"
annotationProcessor "android.arch.lifecycle:compiler:1.0.0"

// RxJava
implementation 'io.reactivex.rxjava2:rxjava:2.1.5'
implementation 'io.reactivex.rxjava2:rxandroid:2.0.1'
implementation 'com.jakewharton.rxbinding2:rxbinding:2.0.0'

// Room
implementation "android.arch.persistence.room:runtime:$roomVersion"
implementation "android.arch.persistence.room:rxjava2:$roomVersion"
kapt "android.arch.persistence.room:compiler:$roomVersion"
implementation "org.jetbrains.kotlin:kotlin-stdlib:1.2.10"
androidTestImplementation "android.arch.persistence.room:testing:$roomVersion"

// Gson
implementation 'com.google.code.gson:gson:2.8.2'
}

这是测试(这里几乎没有):

@RunWith(AndroidJUnit4::class)
class ShoppingListDaoTest {
@Test
fun useAppContext() {
    // Context of the app under test.
    val appContext = InstrumentationRegistry.getTargetContext()
    assertEquals("com.blabla.shoppinglistapp", appContext.packageName)
}

private lateinit var database: ShoppingListDatabase

@Before
fun initDb() {
    // using an in-memory database because the information stored here disappears after test
    database = Room.inMemoryDatabaseBuilder(InstrumentationRegistry.getContext(),
            ShoppingListDatabase::class.java)
            // allowing main thread queries, just for testing
            .allowMainThreadQueries()
            .build()
}

@After
fun closeDb() {
    database.close()
}


@Test fun testGetActiveShoppingListWhenNoActiveShoppingListInserted() {
    database.shoppingListDao().getActiveShoppingLists()
            .test()
            .assertNoValues()
}


}

当我尝试运行测试时,我得到了这个:

进程以退出代码 1 结束 找不到类:“com.blabla.shoppinglistapp.ShoppingListDaoTest”空测试套件。

它没有开始

更新

我注意到一些非常糟糕的事情。 当我在 Android Studio 中启动新项目并尝试运行默认的 androidTest 时,它给了我已知的错误:

错误:Gradle:java.util.concurrent.ExecutionException:java.util.concurrent.ExecutionException:com.android.tools.aapt2.Aapt2Exception:AAPT2 错误:查看日志了解详情

解决方法是将此行添加到 gradle.properties:

android.enableAapt2=false

这是我之前在原始项目中完成的步骤(我也遇到了那个错误)。 不幸的是,它给了我问题中的错误。 所以真正的问题是如何用 aapt2 解决这个问题来完成这项工作。 参见这里:AAPT2 error: check logs for details, after workround the androidTest not working

有什么想法吗?

【问题讨论】:

    标签: android android-studio gradle kotlin android-room


    【解决方案1】:

    我也有同样的问题。每当我尝试运行单个测试或测试类时,我都会不断收到“Empty Test Suite”错误。这似乎是 Android Studio 中的一个错误。

    相反,右键单击androidTest 包并单击“在...中运行'测试”。这是我让它工作的唯一方法。

    礼貌:The codelab guys

    更新

    这是在Android Studio 3.1 版本中修复的。甚至 gradlew connectedDebugAndroidTest issue 现在也已修复。

    【讨论】:

    • 这有点荒谬(感谢您的回答)...您无法右键单击测试并运行它,但您可以右键单击父文件夹并运行它....我是 android 新手,但测试似乎很糟糕....
    • @jacoballenwood 是的,它在 kotlin 中被破坏了。如果您使用 Java 编写测试,它可以完美运行。但是现在写,该工具并不完全支持 Kotlin。希望他们尽快解决。
    • @jacoballenwood 检查我更新的答案。这已在最新的 Android Studio 中得到修复。
    【解决方案2】:

    我遇到了同样的问题。就我而言,解决问题的方法是将我的测试类(DaoTest 类)移动到与 Android Studio 创建的默认 ExampleInstrumentedTest 相同的包中。

    我之前有这个项目结构:

      • androidTest
        • Java
          • [我的应用程序包]
            • ExampleInstrumentedTest
            • itemList.dataSource
              • 道测

    又改为:

      • androidTest
        • Java
          • [我的应用程序包]
            • ExampleInstrumentedTest
            • 道测

    完成此操作后,我能够一次运行班级中的所有测试而没有任何问题。

    【讨论】:

      【解决方案3】:

      我猜你运行测试文件的方式不正确。

      首先,使缓存无效/重新启动,然后,从编号栏运行测试。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-10-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多