red*_*rom 18 user-interface android unit-testing android-espresso
我正在尝试在新的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> {
}
Run Code Online (Sandbox Code Playgroud)
我总是收到此错误消息:
cannot resolve symbol AndroidJUnit4.class
Run Code Online (Sandbox Code Playgroud)
几乎所有导入的库都标记为未使用.
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'
}
Run Code Online (Sandbox Code Playgroud)
如果我把这些设置放在我的其他测试项目上就行了,所以我不知道什么是错的?
我按照本教程:"
http://www.vogella.com/tutorials/AndroidTestingEspresso/article.html
并且我试图通过以下问题来解决它: 无法解析符号'AndroidJUnit4'
但没有运气.
非常感谢任何建议.
pas*_*rby 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'
}
Run Code Online (Sandbox Code Playgroud)
这解决了我的依赖冲突,我没有看到任何其他问题发生.
yeh*_*att 10
我通过手动导入以下内容解决了这个问题,我认为它应该自动导入,但它没有:
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;
Run Code Online (Sandbox Code Playgroud)
我用改变常数来解决它
minSdkVersion
Run Code Online (Sandbox Code Playgroud)
到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'
}
Run Code Online (Sandbox Code Playgroud)
小智 5
根据上述gradle变化给出:
androidTestCompile 'com.android.support.test:runner:0.3'
Run Code Online (Sandbox Code Playgroud)
你需要换到
androidTestCompile('com.android.support.test:runner:0.3') {
exclude group: 'com.android.support', module: 'support-annotations'
}
Run Code Online (Sandbox Code Playgroud)
对我而言,即使有上述变化也无法正常工作,所以我注意到的是我错过了以下内容:
androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.1'
Run Code Online (Sandbox Code Playgroud)
它对我来说很好.
完整的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'
}
Run Code Online (Sandbox Code Playgroud)
希望这肯定会帮助某人,因为即使在完成了vogella教程的完整步骤之后,我已经努力了半天才能修复它.
收到该错误消息的原因可能是因为测试所在的文件夹不符合规范。该文件夹必须是src / androidTest / java。
看一下这篇文章说...
运行仪器化的单元测试要运行仪器化的测试,请按照下列步骤操作:
通过单击工具栏中的“同步项目”,确保您的项目与Gradle同步。通过以下方式之一运行测试:要运行单个测试,请打开“项目”窗口,然后右键单击测试,然后单击“运行”。要测试类中的所有方法,请右键单击测试文件中的类或方法,然后单击运行。要在目录中运行所有测试,请右键单击该目录,然后选择“运行测试”。Android Gradle插件会编译位于默认目录(src / androidTest / java /)中的已检测测试代码,构建测试APK和正式版APK,将两个APK安装在所连接的设备或仿真器上,然后运行测试。然后,Android Studio在“运行”窗口中显示已执行测试的结果。
因此,对于仪器测试人员,文件夹必须为(不要忘记这种情况)
src / androidTest / java
对于本地测试,该文件夹必须为
src /测试/ java
然后,您可以拥有一个包文件夹来匹配您的应用程序包
希望这对社区有所帮助!
归档时间: |
|
查看次数: |
23997 次 |
最近记录: |