Android:使用浓咖啡进行基本测试

时间:2018-01-13 04:44:11

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

我试图在我的Android应用程序中添加一个基本的espresso测试。我的测试文件(位于androidTest / java文件夹中)是由'记录espresso测试'生成的。功能如下:

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 SECONDS。以下空闲条件失败。在   dalvik.system.VMStack.getThreadStackTrace(Native Method)at   java.lang.Thread.getStackTrace(Thread.java:1536)at   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)at   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)at   org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)at at   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)at at   org.junit.runners.ParentRunner $ 1.schedule(ParentRunner.java:71)at at   org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)at at   org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:58)at at   org.junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java:268)at at   org.junit.runners.ParentRunner.run(ParentRunner.java:363)at at   org.junit.runners.Suite.runChild(Suite.java:128)at   org.junit.runners.Suite.runChild(Suite.java:27)at   org.junit.runners.ParentRunner $ 3.run(ParentRunner.java:290)at   org.junit.runners.ParentRunner $ 1.schedule(ParentRunner.java:71)at at   org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)at at   org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:58)at at   org.junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java:268)at at   org.junit.runners.ParentRunner.run(ParentRunner.java:363)at at   org.junit.runner.JUnitCore.run(JUnitCore.java:137)at at   org.junit.runner.JUnitCore.run(JUnitCore.java:115)at at   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());

感谢您阅读

1 个答案:

答案 0 :(得分:0)

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

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