未找到类,使用 Android Studio 3.0.1、Room、Kotlin 的 androidTest 中的空测试套件 [英] Class not found, Empty test suite in androidTest using Android Studio 3.0.1, Room, Kotlin
问题描述
我在运行 androidTest 时遇到问题.
I have a problem with running my androidTest.
这是我在 gradle 中的设置:
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:
进程以退出代码 1 结束找不到类:com.blabla.shoppinglistapp.ShoppingListDaoTest"空测试套件.
Process finished with exit code 1 Class not found: "com.blabla.shoppinglistapp.ShoppingListDaoTest"Empty test suite.
它没有启动
更新
我注意到一些非常糟糕的事情.当我在 Android Studio 中启动新项目并尝试运行默认的 androidTest 时,它给了我一个已知错误:
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:
错误:Gradle:java.util.concurrent.ExecutionException:java.util.concurrent.ExecutionException:com.android.tools.aapt2.Aapt2Exception:AAPT2 错误:检查日志以获取详细信息
Error:Gradle: java.util.concurrent.ExecutionException: java.util.concurrent.ExecutionException: com.android.tools.aapt2.Aapt2Exception: AAPT2 error: check logs for details
对此的解决方法是将此行添加到 gradle.properties:
The workround for this is to add this line to gradle.properties:
android.enableAapt2=false
android.enableAapt2=false
这是我之前在原始项目中完成的步骤(我也遇到了那个错误).不幸的是,它给了我问题中的错误.所以真正的问题是如何使用 aapt2 解决问题以使其工作.另请参见此处:AAPT2 错误:检查日志以获取详细信息,解决后 androidTest 不起作用
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
有什么想法吗?
推荐答案
我也遇到了同样的问题.每当我尝试运行单个测试或测试类时,我都会收到Empty Test Suite"错误.这似乎是 Android Studio 中的一个错误.
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.
相反,右键单击 androidTest
包并单击Run 'Tests in ...".这是我让它工作的唯一方法.
Instead, right click the androidTest
package and click "Run 'Tests in ...". This is the only way I could get it to work.
礼貌:代码实验室人员
更新:
此问题已在 Android Studio 3.1
版本中修复.甚至 gradlew connectedDebugAndroidTest
issue 现在也已修复.
This is fixed in Android Studio 3.1
version. Even the gradlew connectedDebugAndroidTest
issue is now fixed.
这篇关于未找到类,使用 Android Studio 3.0.1、Room、Kotlin 的 androidTest 中的空测试套件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!