【问题标题】:Android : basic test with espressoAndroid : 浓缩咖啡的基本测试
【发布时间】:2018-06-22 13:10:21
【问题描述】:

我正在尝试在我的 Android 应用程序中添加一个基本的浓缩咖啡测试。我的测试文件(位于 androidTest/java 文件夹中)由“记录浓缩咖啡测试”功能生成,如下所示:

package info.my_xxxx.activities;


import android.support.test.espresso.ViewInteraction;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import android.test.suitebuilder.annotation.LargeTest;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;

import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import info.my_xxxx.R;

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;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
import static org.hamcrest.Matchers.allOf;

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

    @Rule
    public IntentsTestRule<SplashActivity> mActivityTestRule = new IntentsTestRule<>(SplashActivity.class);

    @Test
    public void adviceDisplay2() {
        // Added a sleep statement to match the app's execution delay.
        // The recommended way to handle such scenarios is to use Espresso idling resources:
        // https://google.github.io/android-testing-support-library/docs/espresso/idling-resource/index.html
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        ViewInteraction appCompatImageView = onView(
                allOf(withId(R.id.third_button),
                        childAtPosition(
                                allOf(withId(R.id.linear_layout),
                                        childAtPosition(
                                                withId(R.id.bottom_button),
                                                0)),
                                2),
                        isDisplayed()));
        appCompatImageView.perform(click());

        ViewInteraction textView = onView(
                allOf(withId(R.id.advice),
                        childAtPosition(
                                allOf(withId(R.id.RelativeLayout01),
                                        childAtPosition(
                                                withId(android.R.id.content),
                                                0)),
                                1),
                        isDisplayed()));
        textView.check(matches(isDisplayed()));

    }

    private static Matcher<View> childAtPosition(
            final Matcher<View> parentMatcher, final int position) {

        return new TypeSafeMatcher<View>() {
            @Override
            public void describeTo(Description description) {
                description.appendText("Child at position " + position + " in parent ");
                parentMatcher.describeTo(description);
            }

            @Override
            public boolean matchesSafely(View view) {
                ViewParent parent = view.getParent();
                return parent instanceof ViewGroup && parentMatcher.matches(parent)
                        && view.equals(((ViewGroup) parent).getChildAt(position));
            }
        };
    }
}

如您所见,它只是单击视图并检查视图是否存在...我的 gradle 文件(模块级别)是:

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

    dependencies {
        classpath 'io.fabric.tools:gradle:1.+'
    }
}
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'io.fabric'

repositories {
    maven { url 'https://maven.fabric.io/public' }
    google()
    mavenCentral()
}

android {

    dexOptions {
        javaMaxHeapSize "4g"
    }

    compileSdkVersion 26
    buildToolsVersion '26.0.2'

    defaultConfig {
        applicationId "info.xxx"
        minSdkVersion 16
        targetSdkVersion 26
        multiDexEnabled true
        testInstrumentationRunner 'android.support.test.runner.AndroidJUnitRunner'

    }

    signingConfigs{
        release {
            v2SigningEnabled false
        }
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }

    buildTypes {
        release {
            debuggable false
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
}

dependencies {

    implementation  'com.google.android.gms:play-services-analytics:11.8.0'

    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support:design:26.1.0'

    implementation 'com.android.support:multidex:1.0.2'
    implementation ('com.squareup:android-times-square:1.7.2@aar'){
        exclude group: 'com.android.support', module: 'mediarouter-v7'
    }

    implementation files('libs/GraphView-3.1.3.jar')
    implementation('com.crashlytics.sdk.android:crashlytics:2.6.8@aar') {
        transitive = true
    }

    // Kotlin related
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
    implementation "org.jetbrains.anko:anko-appcompat-v7-commons:$anko_version"
    implementation "org.jetbrains.anko:anko-design:$anko_version"
    implementation "org.jetbrains.anko:anko-design-coroutines:$anko_version"

    // Required for local unit tests (JUnit 4 framework)
    testImplementation 'junit:junit:4.12'

    //Mockito framework
    testImplementation 'org.mockito:mockito-core:1.10.19'

    // Required for instrumented tests
    androidTestImplementation 'com.android.support:support-annotations:26.+'
    androidTestImplementation ('com.android.support.test:runner:1.0.1'){
        exclude module: 'support-annotations'
    }
    androidTestImplementation ('com.android.support.test.espresso:espresso-core:3.0.1'){
        exclude module: 'support-annotations'
    }

}

所有依赖项似乎都可以,但是从 android studio 运行测试时,几分钟后我收到了这条消息:

android.support.test.espresso.AppNotIdleException:循环 2969 迭代超过 60 秒。以下空闲条件失败。在 dalvik.system.VMStack.getThreadStackTrace(本机方法)在 java.lang.Thread.getStackTrace(Thread.java:1536) 在 android.support.test.espresso.base.DefaultFailureHandler.getUserFriendlyError(DefaultFailureHandler.java:90) 在 android.support.test.espresso.base.DefaultFailureHandler.handle(DefaultFailureHandler.java:52) 在 android.support.test.espresso.ViewInteraction.waitForAndHandleInteractionResults(ViewInteraction.java:312) 在 android.support.test.espresso.ViewInteraction.desugaredPerform(ViewInteraction.java:167) 在 android.support.test.espresso.ViewInteraction.perform(ViewInteraction.java:110) 在 info.xxx.activities.AdviceDisplay2.adviceDisplay2(AdviceDisplay2.java:56) 在 java.lang.reflect.Method.invoke(Native Method) 在 org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) 在 org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) 在 org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) 在 org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) 在 android.support.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:433) 在 org.junit.rules.RunRules.evaluate(RunRules.java:20) 在 org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) 在 org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) 在 org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) 在 org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) 在 org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) 在 org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) 在 org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) 在 org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) 在 org.junit.runners.ParentRunner.run(ParentRunner.java:363) 在 org.junit.runners.Suite.runChild(Suite.java:128) 在 org.junit.runners.Suite.runChild(Suite.java:27) 在 org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) 在 org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) 在 org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) 在 org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) 在 org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) 在 org.junit.runners.ParentRunner.run(ParentRunner.java:363) 在 org.junit.runner.JUnitCore.run(JUnitCore.java:137) 在 org.junit.runner.JUnitCore.run(JUnitCore.java:115) 在 android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:58) 在 android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:375) 在 android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:2074)

测试运行完成。

第 56 行是:

appCompatImageView.perform(click());

感谢您的阅读

【问题讨论】:

  • 如何将 Espresso 集成到您的项目中。我像你一样使用它的依赖,但它没有定义。

标签: android android-espresso android-espresso-recorder


【解决方案1】:

我今天遇到了同样的错误,但就我而言,它与库版本控制有关。我浪费了很长时间试图弄清楚如何让我的测试重新通过,而解决我的问题的关键之一是将buildToolsVersioncompileSdkVersionsupport-annotations lib 设置为相同的版本。

那么,请您尝试更新您的buildToolsVersion '25.2.0' 并检查它是否有效?

【讨论】:

  • 感谢您的回答。即使我更改了 buildToolsVersion,它也会返回消息:警告:指定的 Android SDK 构建工具版本(25.0.2)被忽略,因为它低于 Android Gradle 插件 3.0.1 的最低支持版本(26.0.2)。将使用 Android SDK Build Tools 26.0.2。要禁止显示此警告,请从您的 build.gradle 文件中删除“buildToolsVersion '25.0.2'”,因为每个版本的 Android Gradle 插件现在都有一个默认版本的构建工具。
  • 我明白了......如何将其他人更改为 sdk 版本 26 并将支持注释更改为 26.0.2? (也许您需要更新一些其他依赖项)
  • 我已将 sdk 和依赖项更新为 26(请参阅消息中更新的我的 gradle 文件)但同样的错误仍然存​​在....
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多