Android 低于 Lollipop 的 Android 版本的仪器测试失败
Android instrumented tests failing for Android versions below Lollipop
我已经花了一天时间寻找问题的解决方案,这就是我提交它的原因。
我使用 Espresso 进行 运行 Android 测试。对于 Lollipop 和更新的版本,一切都很好,但对于旧的 Android 版本(从项目的目标 API 16 到第一个工作版本的 API 21)就不行了。
我的问题是,用于测试的文件是 Android Lollipop 和更新版本的 /androidTest
文件夹中的文件,但对于旧版本,它使用默认风格,即 'mock'味道。
例如,我在 /mock 和 /androidTest 文件夹中都有一个 class UserManager,我需要测试才能使用 /androidTest 目录中的那个。
你知道如何纠正吗?这只破坏了 Android 4.x 的测试,对于较新的版本,预期的行为发生了,所以我真的很失望。
我尝试设置 sourceSets 但它没有解决我的问题。
我看到 multiDex 可能会干扰 androidTest,所以我尝试了一些对其他人有用的解决方案,但它也没有解决它。
如果有人可以提供帮助,我会采纳任何建议。
谢谢
Android Studio 3.2.1
Gradle4.6
android {
compileSdkVersion 28
buildToolsVersion '28.0.3'
defaultConfig {
applicationId "my.app"
minSdkVersion 16
targetSdkVersion 28
versionCode Integer.parseInt(app_version_code)
versionName app_version_name
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
multiDexEnabled true
}
}
dependencies {
testImplementation 'junit:junit:4.12'
testImplementation 'org.json:json:20140107'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test:rules:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-intents:3.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-contrib:3.0.2'
}
好的,我终于找到了解决问题的方法。
感谢 this post 我发现这是一个 multidex 问题。
所以我在我的项目中添加了一个 proguard 规则文件,并在我的 build.gradle:
中添加了这些行
buildTypes {
debug {
// Other debug settings
multiDexKeepProguard file('proguard-multidex-rules.pro')
}
}
proguard-multidex-rules.pro内容:
-keep class android.support.test.internal** { *; }
-keep class org.junit.** { *; }
-keep @com.google.common.annotations.VisibleForTesting class *
-keepclasseswithmembers class * {
@com.google.common.annotations.VisibleForTesting *;
}
现在要做的最后一件事是在插桩测试中需要的每个 class 声明之前添加 @VisibleForTesting
注释。它对我有用,现在用于测试的文件是带注释的文件 (found here)。
问题已解决
我已经花了一天时间寻找问题的解决方案,这就是我提交它的原因。
我使用 Espresso 进行 运行 Android 测试。对于 Lollipop 和更新的版本,一切都很好,但对于旧的 Android 版本(从项目的目标 API 16 到第一个工作版本的 API 21)就不行了。
我的问题是,用于测试的文件是 Android Lollipop 和更新版本的 /androidTest
文件夹中的文件,但对于旧版本,它使用默认风格,即 'mock'味道。
例如,我在 /mock 和 /androidTest 文件夹中都有一个 class UserManager,我需要测试才能使用 /androidTest 目录中的那个。
你知道如何纠正吗?这只破坏了 Android 4.x 的测试,对于较新的版本,预期的行为发生了,所以我真的很失望。
我尝试设置 sourceSets 但它没有解决我的问题。
我看到 multiDex 可能会干扰 androidTest,所以我尝试了一些对其他人有用的解决方案,但它也没有解决它。
如果有人可以提供帮助,我会采纳任何建议。
谢谢
Android Studio 3.2.1
Gradle4.6
android {
compileSdkVersion 28
buildToolsVersion '28.0.3'
defaultConfig {
applicationId "my.app"
minSdkVersion 16
targetSdkVersion 28
versionCode Integer.parseInt(app_version_code)
versionName app_version_name
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
multiDexEnabled true
}
}
dependencies {
testImplementation 'junit:junit:4.12'
testImplementation 'org.json:json:20140107'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test:rules:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-intents:3.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-contrib:3.0.2'
}
好的,我终于找到了解决问题的方法。
感谢 this post 我发现这是一个 multidex 问题。
所以我在我的项目中添加了一个 proguard 规则文件,并在我的 build.gradle:
中添加了这些行buildTypes {
debug {
// Other debug settings
multiDexKeepProguard file('proguard-multidex-rules.pro')
}
}
proguard-multidex-rules.pro内容:
-keep class android.support.test.internal** { *; }
-keep class org.junit.** { *; }
-keep @com.google.common.annotations.VisibleForTesting class *
-keepclasseswithmembers class * {
@com.google.common.annotations.VisibleForTesting *;
}
现在要做的最后一件事是在插桩测试中需要的每个 class 声明之前添加 @VisibleForTesting
注释。它对我有用,现在用于测试的文件是带注释的文件 (found here)。
问题已解决