运行Android Espresso UI测试时为什么会出现这些错误?

时间:2015-12-22 13:57:53

标签: android android-testing android-espresso

我尝试使用espresso运行Instrumentation android UI测试,但我收到了这些错误。

Error:(3, 33) error: package android.support.test.rule does not exist
Error:(4, 35) error: package android.support.test.runner does not exist
Error:(7, 17) error: package org.junit does not exist
Error:(8, 17) error: package org.junit does not exist
Error:(9, 24) error: package org.junit.runner does not exist
Error:(13, 44) error: package android.support.test.espresso does not exist
Error:(13, 1) error: static import only from classes and interfaces
Error:(14, 1) error: static import only from classes and interfaces
Error:(14, 51) error: package android.support.test.espresso.action does not exist
Error:(15, 54) error: package android.support.test.espresso.assertion does not exist
Error:(15, 1) error: static import only from classes and interfaces
Error:(16, 52) error: package android.support.test.espresso.matcher does not exist
Error:(16, 1) error: static import only from classes and interfaces
Error:(17, 52) error: package android.support.test.espresso.matcher does not exist
Error:(17, 1) error: static import only from classes and interfaces
Error:(19, 2) error: cannot find symbol class RunWith
Error:(24, 12) error: cannot find symbol class ActivityTestRule
Error:(23, 6) error: cannot find symbol class Rule
Error:(26, 6) error: cannot find symbol class Test
Error:Execution failed for task ':westwing:compileStageDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.

我按照这里的步骤操作: https://google.github.io/android-testing-support-library/docs/espresso/setup/

Gradle文件:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion '23.0.2'


    defaultConfig {
        applicationId "de.westwing.android"
        minSdkVersion 10
        targetSdkVersion 23
        signingConfig signingConfigs.dumpKey

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            // The ProGuard plugin is applied automatically, and the tasks are created automatically
            // if the Build Type is configured to run ProGuard through the minifyEnabled property.
            minifyEnabled false
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android.txt'),
                    'proguard-rules.txt'
            debuggable false
        }
        debug {
            debuggable true
        }
    }

    productFlavors {
        live {
            manifestPlaceholders = [appIcon: "@mipmap/ic_launcher", appName: "@string/app_name"]
            buildConfigField "boolean", "STAGE", "false"
        }
        stage {
            manifestPlaceholders = [appName: getWorkingBranch(), appIcon: "@drawable/ic_stage_launcher"]
            applicationId "de.westwing.android.stage"
            buildConfigField "boolean", "STAGE", "true"
        }
    }


}

dependencies {
    compile project(':appoxee')
    compile project(':android-volley-master')

    compile 'com.squareup.okhttp:okhttp:1.6.0'
    compile 'com.google.code.gson:gson:2.3'
    compile 'com.android.support:support-annotations:23.1.1'
    compile 'com.google.android.gms:play-services-base:8.1.0'
    compile 'com.google.android.gms:play-services-plus:8.1.0'
    compile 'com.google.android.gms:play-services-analytics:8.1.0'
    compile 'com.google.android.gms:play-services-identity:8.1.0'
    compile 'com.google.android.gms:play-services-gcm:8.1.0'
    compile 'com.android.support:gridlayout-v7:23.1.0'
    compile 'com.android.support:cardview-v7:23.1.0'
    compile 'com.android.support:recyclerview-v7:23.1.0'
    compile 'com.facebook.android:facebook-android-sdk:4.5.0'
    compile 'com.jakewharton:butterknife:6.1.0'
    compile 'io.card:android-sdk:5.1.1'
    compile 'com.adjust.sdk:adjust-android:4.1.2'
    compile 'com.intellij:annotations:12.+'
    compile 'com.android.support:design:23.1.0'
    compile 'com.github.chrisbanes.photoview:library:1.2.4'
    compile 'de.greenrobot:eventbus:2.4.0'
    compile 'com.crittercism:crittercism-android-agent:5.4.0'
    compile 'com.android.support:percent:23.1.0'
    compile('com.github.worker8:tourguide:1.0.14-SNAPSHOT@aar') {
        transitive = true
    }
    compile 'net.hockeyapp.android:HockeySDK:3.6.0'

    // Crashlytics Kit
    compile('com.crashlytics.sdk.android:crashlytics:2.3.2@aar') {
        transitive = true
    }

    compile files('libs/FlurryAnalytics-5.6.0.jar')
    compile files('libs/appsee.jar')

    testCompile 'junit:junit:4.12'
    testCompile 'org.mockito:mockito-core:1.10.19'
    testCompile 'org.hamcrest:hamcrest-library:1.1'
    testCompile 'org.robolectric:robolectric:3.0'

    androidTestCompile 'com.android.support:support-annotations:23.1.1'
    androidTestCompile 'com.android.support.test:runner:0.4.1'
    androidTestCompile 'com.android.support.test:rules:0.4.1'
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1'
    // Set this dependency if you want to use Hamcrest matching
    androidTestCompile 'org.hamcrest:hamcrest-library:1.3'
}

buildscript {
    repositories {
        maven { url 'https://maven.fabric.io/public' }
    }

    dependencies {
        classpath 'io.fabric.tools:gradle:1.+'
    }
}

repositories {
    maven { url "https://maven.fabric.io/public" }
    maven { url "https://oss.sonatype.org/content/repositories/snapshots" }
}

android {
    useLibrary 'org.apache.http.legacy'
}
android { sourceSets { main { java.srcDirs = ['src/main/java', 'src/androidTest/java'] } } }

这是我的测试:

package de.westwing.android;

import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import android.test.suitebuilder.annotation.LargeTest;

import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import de.westwing.android.presentation.activities.MainActivity;

import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
import static android.support.test.espresso.matcher.ViewMatchers.withId;

@RunWith(AndroidJUnit4.class)
@LargeTest
public class NavigationViewTest {

    @Rule
    public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule(MainActivity.class);

    @Test
    public void testNavigationViewItems() {
        onView(withId(R.id.nav_email_customer_service))            // withId(R.id.my_view) is a ViewMatcher
                .perform(click())               // click() is a ViewAction
                .check(matches(isDisplayed())); // matches(isDisplayed()) is a ViewAssertion
    }
}

1 个答案:

答案 0 :(得分:0)

我发现问题是什么?

由于我的项目从一开始就没有适当的结构 有一个错误的配置。

我做了什么:

1-打开项目文件夹

2- open src子目录

3-创建一个子文件夹androidTest

4- androidTest内部为"com -> example -> android"

创建包的子文件夹

在Android Studio中打开您的项目

6-添加所需的gradle依赖项

7-创建测试并运行它。

"Enjoy testing :)"
相关问题