Android Espresso 测试:找不到类:...空测试套件

Posted

技术标签:

【中文标题】Android Espresso 测试:找不到类:...空测试套件【英文标题】:Android Espresso test: Class not found: ...Empty test suite 【发布时间】:2017-01-12 21:23:03 【问题描述】:

我正在尝试将此示例中的内容 - https://github.com/googlesamples/android-testing/tree/master/ui/espresso/BasicSample - 实现到我的应用程序中。

当我尝试执行简单的 Espresso 测试时,会发生这种情况:

"/Applications/Android Studio 2.2.3.app/Contents/jre/jdk/Contents/Home/bin/java" (...)

进程以退出代码 1 结束 找不到类:“com.faces_shop.app.MainActivityTest”空测试套件。

(从 Android Studio 复制粘贴)

测试:

package com.faces_shop.app;

import android.support.test.filters.LargeTest;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;

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

import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.closeSoftKeyboard;
import static android.support.test.espresso.action.ViewActions.typeText;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;

/**
 * Created by AnonymizedForReview on 2017-01-12.
 */
@RunWith(AndroidJUnit4.class)
@LargeTest
public class MainActivityTest 

    public static final String STRING_TO_BE_TYPED = "Espresso";

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

    @Test
    public void changeText_sameActivity() 
        // Type text and then press the button.
        onView(withId(R.id.editFilter))
                .perform(typeText(STRING_TO_BE_TYPED), closeSoftKeyboard());
//        onView(withId(R.id.changeTextBt)).perform(click());

        // Check that the text was changed.
        onView(withId(R.id.editFilter)).check(matches(withText(STRING_TO_BE_TYPED)));
    



应用的 build.gradle:

apply plugin: 'com.android.application'

android 
//    productFlavors 
//        /* https://android-developers.googleblog.com/2015/12/leveraging-product-flavors-in-android.html */
//        mock 
//            applicationIdSuffix = ".mock"
//        
//        prod
//    
    compileSdkVersion 25
    buildToolsVersion "25.0.0"
    defaultConfig 
        applicationId "com.faces_shop.app"
        minSdkVersion 16
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    
    buildTypes 
        release 
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        
    


dependencies 
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile project(':FacesApi')

//    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', 
//        exclude group: 'com.android.support', module: 'support-annotations'
//    )
    // Testing-only dependencies
    // Force usage of support annotations in the test app, since it is internally used by the runner module.
    androidTestCompile 'com.android.support:support-annotations:' + rootProject.supportLibVersion;
    androidTestCompile 'com.android.support.test:runner:' + rootProject.runnerVersion;
    androidTestCompile 'com.android.support.test:rules:' + rootProject.rulesVersion;
    androidTestCompile 'com.android.support.test.espresso:espresso-core:' + rootProject.espressoVersion;

    compile 'com.android.support:appcompat-v7:25.0.1'
    compile 'com.android.support:recyclerview-v7:25.0.1'
    testCompile 'junit:junit:4.12'

顶层build.gradle:

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript 
    repositories 
        jcenter()
    
    dependencies 
        classpath 'com.android.tools.build:gradle:2.2.3'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    


allprojects 
    repositories 
        jcenter()
    


ext 
    buildToolsVersion = "24.0.1"
    supportLibVersion = "25.0.1"
    runnerVersion = "0.5"
    rulesVersion = "0.5"
    espressoVersion = "2.2.2"

我做错了什么?

为什么看不到测试类?

它在 androidTest 的这条路径中:.../Examples/DynamicList/FacesApp/src/androidTest/java/com/faces_shop/app/MainActivityTest.java

【问题讨论】:

Running ../gradlew connectedCheck 确实找到并运行测试。所以我想问题出在Android Studio中。 Android Studio 中的清理/重建没有帮助。重新启动 Android Studio 也没有帮助。 【参考方案1】:

回答我自己的问题:

解决方案

原来,Android Studio 中有一个运行/调试配置,为 "JUnit",而它应该是 "Android Tests"

发生这种情况是因为,最初,测试类意外地位于 src/test/java,而不是 src/androidTest/java。因此,当我最初运行它时,运行/调试配置文件被创建为 JUnit,然后在随后尝试运行该类时被重用。我想它可以在 Android Studio 中进行改进,根据类的当前状态确定运行/调试类型(虽然不是抱怨 ;-))...

【讨论】:

如何将配置更改为“Android Tests”? 我在 androidTest 包中的 tetss 作为 junit 测试运行,我什至无法选择我必须运行测试的设备。 如果指示的解决方案不起作用,请尝试在 Edit Configuration ***.com/a/53715513/967131 中手动创建新的 Android Instrumented Tests 配置 我在 Android Instrumented 测试中遇到了同样的问题,IDE 将其创建为 Android JUnit。我删除了现有配置并手动将其重新创建为 Android Instrumented Test,它可以正常工作。

以上是关于Android Espresso 测试:找不到类:...空测试套件的主要内容,如果未能解决你的问题,请参考以下文章

Android Studio Espresso 测试错误:空测试套件

Espresso 找不到视图:NoMatchingViewException

Android - Espresso:为每次测试重新创建活动

使用 Android-Espresso 运行多个测试时出现内存不足异常

Android Espresso 内部匕首工厂类的 NoClassDefFoundError

检查 Espresso 是不是显示对话框