我正在尝试在新的Android项目中创建Espresso UI测试,但我遇到了以下问题。
如果我尝试创建一个空的测试类:
import android.content.Intent;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import android.test.ActivityInstrumentationTestCase2;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
@RunWith(AndroidJUnit4.class)
public class LoginActivityTest extends ActivityInstrumentationTestCase2<LoginActivity> {
}
我总是收到此错误消息:
cannot resolve symbol AndroidJUnit4.class
几乎所有导入的库都标记为未使用。
build.gradle文件包含以下内容:
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.0"
defaultConfig {
applicationId "com.some.thing.xxx"
minSdkVersion 14
targetSdkVersion 23
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
lintOptions {
abortOnError false
}
packagingOptions {
exclude 'LICENSE.txt'
}
}
repositories {
mavenCentral()
maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
maven { url "https://jitpack.io" }
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.0.0'
compile 'com.google.android.gms:play-services:7.8.0'
compile 'com.mcxiaoke.volley:library:1.0.18'
compile 'com.orhanobut:logger:1.11'
// App dependencies
compile 'com.android.support:support-annotations:23.0.0'
// TESTING DEPENDENCIES
androidTestCompile 'com.android.support.test:runner:0.3'
// Set this dependency to use JUnit 4 rules
androidTestCompile 'com.android.support.test:rules:0.3'
// Set this dependency to build and run Espresso tests
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2'
// add this for intent mocking support
androidTestCompile 'com.android.support.test.espresso:espresso-intents:2.2'
// add this for webview testing support
androidTestCompile 'com.android.support.test.espresso:espresso-web:2.2'
// Set this dependency to build and run UI Automator tests
androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.1'
androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.2'
}
如果我把这些设置放在我的其他测试项目上就可以了,所以我不知道会出现什么问题?
我已经按照本教程:“
Cannot resolve symbol 'AndroidJUnit4'
我试图通过以下问题解决它: {{3}}
但没有运气。
非常感谢任何建议。
答案 0 :(得分:14)
我也尝试过来自vogella的相同教程,并遇到了很多问题。我遇到的第一个问题之一是v23库的注释版本和Espresso库之间的依赖冲突。
然后我找到了另一个最近更新的Roger Hu“UI Testting with Espresso”教程。我注意到Espresso尚未支持Marshmallow。
依赖关系添加如下:
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2') {
// Necessary if your app targets Marshmallow (since Espresso
// hasn't moved to Marshmallow yet)
exclude group: 'com.android.support', module: 'support-annotations'
}
androidTestCompile('com.android.support.test:runner:0.3') {
// Necessary if your app targets Marshmallow (since the test runner
// hasn't moved to Marshmallow yet)
exclude group: 'com.android.support', module: 'support-annotations'
}
这解决了我的依赖冲突,我没有看到任何其他问题发生。
答案 1 :(得分:9)
我通过手动导入以下内容解决了这个问题,我认为它应该自动导入,但它并没有:
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.action.ViewActions.closeSoftKeyboard;
import static android.support.test.espresso.action.ViewActions.typeText;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
答案 2 :(得分:5)
我用改变常数
解决了它minSdkVersion
到 build.gradle 文件中的第18版。
关注gradle.file正在运行:
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.0"
defaultConfig {
applicationId "com.something.xxx"
minSdkVersion 18
targetSdkVersion 23
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
lintOptions {
abortOnError false
}
packagingOptions {
exclude 'LICENSE.txt'
}
}
repositories {
mavenCentral()
maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
maven { url "https://jitpack.io" }
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.0.0'
compile 'com.google.android.gms:play-services:7.8.0'
compile 'com.mcxiaoke.volley:library:1.0.18'
compile 'com.orhanobut:logger:1.11'
// TESTING DEPENDENCIES
androidTestCompile 'com.android.support:support-annotations:23.0.0'
androidTestCompile 'com.android.support.test:runner:0.3'
// Set this dependency to use JUnit 4 rules
androidTestCompile 'com.android.support.test:rules:0.3'
// Set this dependency to build and run Espresso tests
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2'
// add this for intent mocking support
androidTestCompile 'com.android.support.test.espresso:espresso-intents:2.2'
// add this for webview testing support
androidTestCompile 'com.android.support.test.espresso:espresso-web:2.2'
// Set this dependency to build and run UI Automator tests
androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.1'
androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.2'
}
答案 3 :(得分:5)
根据上述gradle更改:
androidTestCompile 'com.android.support.test:runner:0.3'
您需要更改为
androidTestCompile('com.android.support.test:runner:0.3') {
exclude group: 'com.android.support', module: 'support-annotations'
}
对我来说,即使有上述变化也无法工作,所以我注意到的是我错过了以下内容:
androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.1'
它对我来说很好。
完整的build.gradle可以在下面找到:
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "21.1.2"
lintOptions {
// IMPORTANT: We are disabling this rule to avoid build errors on PrettyTime. Although
//pretty time references an InvalidPackage it does not do it in the code sections we use
//given how easy this library is to use I would prefer not to replace it with something
//like Joda-Time which is overkill for such a small section of the app.
disable 'InvalidPackage'
}
packagingOptions {
exclude 'LICENSE.txt'
}
defaultConfig {
applicationId "co.test.dialer"
minSdkVersion 18
targetSdkVersion 22
versionCode 15
versionName "0.6.15."
renderscriptTargetApi 22
renderscriptSupportModeEnabled true
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
signingConfigs {
production {
storeFile file("keystore.jks")
storePassword "hello"
keyAlias "production"
keyPassword "android"
}
debug {
storeFile file("keystore.jks")
storePassword "hello"
keyAlias "debug"
keyPassword "android"
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.production
}
debug {
minifyEnabled false
debuggable true
applicationIdSuffix ".debug"
signingConfig signingConfigs.debug
}
internal_test {
minifyEnabled false
debuggable true
applicationIdSuffix ".test"
signingConfig signingConfigs.debug
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.0.1'
compile 'com.android.support:support-v13:23.0.1'
compile 'com.android.support:cardview-v7:23.0.1'
compile 'com.android.support:design:23.0.1'
compile 'com.android.support:recyclerview-v7:23.0.1'
compile 'com.google.android.gms:play-services-gcm:8.1.0'
compile 'com.jakewharton:butterknife:6.1.0'
compile 'com.afollestad:material-dialogs:0.7.8.0'
compile 'com.googlecode.libphonenumber:libphonenumber:3.1'
compile 'com.mcxiaoke.volley:library:1.0.15'
compile 'squizbit.com.jsonobjectified:jetjson:1.0.3@aar'
compile 'com.google.android.gms:play-services-analytics:8.1.0'
releaseCompile 'co.test.dialersdk:dialersdk:1.0.8@aar';
debugCompile 'co.test.dialersdk:dialersdk-debug:1.0.8@aar';
internal_testCompile 'co.test.dialersdk:dialersdk-internal_test:1.0.8@aar';
androidTestCompile('com.android.support.test:runner:0.3') {
exclude group: 'com.android.support', module: 'support-annotations'
}
androidTestCompile('com.android.support.test:rules:0.3') {
exclude group: 'com.android.support', module: 'support-annotations'
}
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2') {
exclude group: 'com.android.support', module: 'support-annotations'
}
androidTestCompile('com.android.support.test.espresso:espresso-intents:2.2') {
exclude group: 'com.android.support', module: 'support-annotations'
}
androidTestCompile('com.android.support.test.espresso:espresso-contrib:2.2') {
exclude group: 'com.android.support', module: 'support-annotations'
exclude group: 'com.android.support', module: 'appcompat'
exclude group: 'com.android.support', module: 'support-v4'
exclude module: 'recyclerview-v7'
}
androidTestCompile('com.android.support.test.espresso:espresso-web:2.2') {
exclude group: 'com.android.support', module: 'support-annotations'
}
androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.1'
}
希望这肯定会帮助某些人,因为即使在完成了vogella教程的完整步骤之后,我已经努力了半天才能修复它。
答案 4 :(得分:4)
您收到该错误消息的原因可能是因为您的测试所在的文件夹与规范不匹配。该文件夹必须是 src / androidTest / java 。
Take a look at this article ,其中说......
运行检测单元测试要运行检测测试,请按照 这些步骤:
单击同步,确保您的项目与Gradle同步 工具栏中的项目。通过以下方式之一运行测试: 要运行单个测试,请打开“项目”窗口,然后右键单击a 单击测试并单击运行。要测试类中的所有方法,请右键单击a 测试文件中的类或方法,然后单击“运行”。在a中运行所有测试 在目录中,右键单击该目录并选择Run tests。该 Gradle的Android插件编译位于的检测测试代码 在默认目录(src / androidTest / java /)中,构建一个测试APK 和生产APK,在连接的设备上安装两个APK或 模拟器,并运行测试。然后,Android Studio会显示结果 在“运行”窗口中执行检测的测试执行。
因此,对于仪器测试文件夹必须(不要忘记案例)
的src / androidTest / JAVA
和本地测试文件夹必须
的src /测试/ JAVA
然后,您可以让您的包文件夹与您的应用包匹配
希望,这对社区有帮助!
答案 5 :(得分:2)
我有同样的问题,我决定更改我的Build Variant。我在发布版本中运行测试。
答案 6 :(得分:2)
您可能有多种Build类型,默认Android Project创建两种构建类型(debug / release),更改构建变量以进行调试或将值设置如下
http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Testing
Currently only one Build Type is tested. By default it is the debug Build Type, but this can be reconfigured with:
android {
...
testBuildType "staging"
}
答案 7 :(得分:1)
您可以参考此answer。
“我犯了错误,将测试类放在src / test。将它们移动到src / androidTest / java / ...之后,依赖项就解决了。也许这也是你的问题。”