浓缩咖啡测试 - Android

Espresso Testing - Android

我在 Android Espresso 测试时遇到问题。我似乎无法访问 "AndroidJUnit4.class"。下面的代码将“@RunWith(AndroidJUnit4.class)”作为 "Cannot resolve symbol AndroidJUnit4" .

import android.test.suitebuilder.annotation.LargeTest;

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

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

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

    @Test
    public void listGoesOverTheFold() {
        onView(withText("Hello world!")).check(matches(isDisplayed()));
    }
}

我知道这与 build.gradle 文件设置有关,但我不知道出了什么问题。下面是我正在使用的副本。

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.1"

    defaultConfig {
        applicationId "example.com.myapp"
        minSdkVersion 22
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:cardview-v7:23.0.1'
    compile 'com.android.support:recyclerview-v7:23.0.1'
    compile 'com.android.support:design:23.0.1'
    compile 'com.android.support:appcompat-v7:23.0.1'

    testCompile 'junit:junit:4.12'
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1'
    androidTestCompile 'com.android.support.test:runner:0.4.1'
    androidTestCompile 'com.android.support.test:rules:0.4'
    androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.2'
    compile project(':volleylibrarygitpull')
}

configurations.all {
    resolutionStrategy.force 'com.android.support:support-annotations:22.2.0'
}

如果有人能告诉我我的 build.gradle 文件有什么问题以便我使用 Espresso 测试框架,我将不胜感激。我需要支持 material design 所以 API 级别低于 23.0.1 具有 cardview, recyclerview 就可以了。

根据https://guides.codepath.com/android/UI-Testing-with-Espresso

您试图将测试放在错误的文件夹中,这就是 Android Studio 无法识别 JUnit4 Class 的原因。阅读上面的教程以使用 androidTest 文件夹。

另请注意,您对同一个库的模块使用了不同的版本:

androidTestCompile 'com.android.support.test:runner:0.4.1'
androidTestCompile 'com.android.support.test:rules:0.4'