没有测试使用Espresso发现消息

时间:2016-08-24 14:32:37

标签: android junit4 android-espresso

我在Espresso中进行了简单的测试。

public class MainActivityTest {


@Rule
public final ActivityRule<MainActivity> main = new ActivityRule<>(MainActivity.class);

@Test
public void shouldBeAbleToLaunchMainScreen(){
    onView(withText("Hello")).check(ViewAssertions.matches(isDisplayed()));
   }
}

但是,我无法运行它。为了帮助理解,我将向您展示以下图片。

My Tests

为什么我收到此消息?

错误:未指定Instrumentation runner类

我也宣称它是gradle文件。

apply plugin: 'com.android.application'

android {
compileSdkVersion 24
buildToolsVersion "24.0.0"

 defaultConfig {
    applicationId "theo.testing.androidespresso"
    minSdkVersion 18
    targetSdkVersion 24
    versionCode 1
    versionName "1.0"
    multiDexEnabled true
            testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

}

  buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'),    
        'proguard-rules.pro'
    }
}
packagingOptions{
    exclude 'LICENCE.txt'
}

}

dependencies {
 compile fileTree(dir: 'libs', include: ['*.jar'])
 testCompile 'junit:junit:4.12'
 androidTestCompile 'com.android.support:support-annotations:24.2.0'

 compile 'com.android.support:appcompat-v7:24.2.0'
 androidTestCompile('com.android.support.test.espresso:espresso-
 contrib:2.2.2') {
    // Necessary to avoid version conflicts
    exclude group: 'javax.inject'
    exclude group: 'com.android.support', module: 'appcompat'
    exclude group: 'com.android.support', module: 'support-v4'
    exclude group: 'com.android.support', module: 'support-annotations'
  }


}

有什么想法吗?

感谢。

1 个答案:

答案 0 :(得分:0)

请查看Espresso设置说明:https://google.github.io/android-testing-support-library/docs/espresso/setup/

特别是在这一部分:

  

下载Espresso

     
      
  • 确保您已在Android Support Repository下安装了最新的Extras(请参阅   instructions)。

  •   
  • 打开应用的build.gradle文件。这通常不是顶级build.gradle文件,而是app/build.gradle

  •   
  • 在依赖项中添加以下行:

     androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
     androidTestCompile 'com.android.support.test:runner:0.5'
    
  •   
     

见   (下载)[https://google.github.io/android-testing-support-library/downloads/index.html]   更多artifactsespresso-contribespresso-web,   等)

您已经在build.gradle文件中遗漏了这两个依赖项。

您的Gradle依赖项应如下所示:

dependencies {
 compile fileTree(dir: 'libs', include: ['*.jar'])
 testCompile 'junit:junit:4.12'
 androidTestCompile 'com.android.support:support-annotations:24.2.0'

 compile 'com.android.support:appcompat-v7:24.2.0'
 androidTestCompile 'com.android.support.test:runner:0.5'
 androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
 androidTestCompile('com.android.support.test.espresso:espresso-
 contrib:2.2.2') {
    // Necessary to avoid version conflicts
    exclude group: 'javax.inject'
    exclude group: 'com.android.support', module: 'appcompat'
    exclude group: 'com.android.support', module: 'support-v4'
    exclude group: 'com.android.support', module: 'support-annotations'
  }


}

希望它会有所帮助