本文介绍了类未找到,androidTest中使用Android Studio 3.0.1的空测试套件,Room,Kotlin的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这是我在gradle中的设置:



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

android {
compileSdkVersion 26
defaultConfig {
applicationIdcom.blabla.shoppinglistapp
minSdkVersion 17
targetSdkVersion 26
versionCode 1
versionName1.0
testInstrumentationRunnerandroid.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'

依赖项{
implementation fileTree (dir:'libs',in clude:['* .jar'])
implementationorg.jetbrains.kotlin:kotlin-stdlib-jre7:$ kotlin_version
implementationcom.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和LiveData
实现android.arch.lifecycle:extensions:1.0.0
annotationProcessorandroid.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'

//房间
实现android.arch.persistence.room:runtime:$roomVersion
implementationandroid.arch .persistence.room:rxjava2:$ roomVersion
kaptandroid.arch.persistence.room:compiler:$roomVersion
implementationorg.jetbrains.kotlin:kotlin-stdlib:1.2.10
androidTestImplementationandroid.arch.persistence.room:testing:$roomVersion

// Gson
实现'com.google.code.gson:gson:2.8.2'

$ / code>

以下是测试(几乎没有任何内容):

  @RunWith(AndroidJUnit4 :: class)
class ShoppingListDaoTest {
@Test
fun useAppContext(){
//正在测试的应用程序的上下文。
val appContext = InstrumentationRegistry.getTargetContext()
assertEquals(com.blabla.shoppinglistapp,appContext.packageName)
}

私有lateinit var database:ShoppingListDatabase

@Before
fun initDb(){
//使用内存数据库,因为这里存储的信息在测试后消失
database = Room.inMemoryDatabaseBuilder(InstrumentationRegistry。 getContext(),
ShoppingListDatabase :: class.java)
//允许主线程查询,仅用于测试
.allowMainThreadQueries()
.build()
}

@
后有趣closeDb(){
database.close()
}


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


}

当我尝试运行tes ti get this:

而且它不是开始的



UPDATE



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

这是为了将这一行添加到gradle.properties:

这是我以前在原始项目中完成的步骤(我也遇到过这个错误)。
不幸的是,之后它给我提出了我的问题中的错误。
所以,真正的问题是如何用aapt2来解决这个问题,从而完成这项工作。
另见这里:



任何想法?

解决方案

我也有同样的问题。每当我尝试运行单独的测试或测试课程时,我都会收到空测试套件错误。看起来这是Android Studio中的一个bug。



相反,右键单击 androidTest 包并单击运行测试在......。这是我能够得到它的唯一方法。



礼貌:

更新

这固定在 Android Studio 3.1 版本中。即使是 gradlew connectedDebugAndroidTest 现在是固定。


I have a problem with running my androidTest.

Here is my setup in 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'
}

And here is the test(almost nothing here):

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


}

When i try to run the test i get this:

And it is not starting

UPDATE

I noticed some really bad thing.When i started new project in Android Studio and try to run default androidTest it gives me known error:

The workround for this is to add this line to gradle.properties:

This is the steps that I've done in my original project previously(i ran into that error also).Unfortunately, after it it give me the error from my question.So it really is the matter of how to solve the isse with the aapt2 to make this work.See here also: AAPT2 error: check logs for details, after workround the androidTest not working

Any idea?

解决方案

I too had the same problem. Whenever I tried to run the individual test or test class, I kept getting "Empty Test Suite" error. It seems this is a bug in Android Studio.

Instead, right click the androidTest package and click "Run 'Tests in ...". This is the only way I could get it to work.

Courtesy: The codelab guys

UPDATE:

This is fixed in Android Studio 3.1 version. Even the gradlew connectedDebugAndroidTest issue is now fixed.

这篇关于类未找到,androidTest中使用Android Studio 3.0.1的空测试套件,Room,Kotlin的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 01:06