Android Studio Espresso 空测试套件

标签 android unit-testing android-studio android-espresso

当我运行使用 Espresso 的测试方法时,它会提示我选择一个模拟器,然后我选择我现有的模拟器并启动我的应用程序。模拟器随后自动重启我的应用程序,然后显示测试套件为空。

The error

我的 espresso 测试用例位于与我要测试的 Activity 相同的模块的 androidTest 文件夹中。我编写了一个简单的“isDisplayed()”类型的测试并右键单击该方法并单击运行。我查看了有关 Stack Overflow 和其他资源的其他问题,但我无法弄清楚是什么导致了这个问题。 logcat 什么都不显示,当我尝试将 Log.d("debug", "hello hello") 放入测试方法(如下所示)时,logcat 中什么也没有显示,当我尝试放置 System.out 时也没有任何显示.println("hello") 在测试方法中。似乎虽然我运行了该方法,但我的代码都没有运行!

下面是我的一些build.grade。

apply plugin: 'com.android.application'

android {
   compileSdkVersion 17
   buildToolsVersion "21.1.2"
   defaultConfig {
       applicationId "x"
       minSdkVersion 17
       targetSdkVersion 17
       versionCode 1
       versionName "1.0"

       testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
   }


    packagingOptions {
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
        exclude 'LICENSE.txt'
    }
}


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

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile 'com.android.support.test:rules:0.3'
    androidTestCompile 'com.android.support.test:runner:0.3'
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2'
}

这是我要运行的测试用例。

@RunWith(AndroidJUnit4.class)
public class EspressoTest1 extends ActivityInstrumentationTestCase2<P1>{


    private P1 mActivity;

    public EspressoTest1() {
        super(P1.class);
    }

    public void setUp() throws Exception {

        super.setUp();
        injectInstrumentation(InstrumentationRegistry.getInstrumentation());
        mActivity = getActivity();

    }

    public void Happy_test() {

        onView(withId(R.id.Btn)).check(matches(isDisplayed()));

    }
}

Test Configuration

这是测试运行配置。

最佳答案

您的测试缺少 @Test 注释。因此,由于您的设置方法丢失了 @Before

关于Android Studio Espresso 空测试套件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31123017/

相关文章:

iphone - 从 UIBarButtonItem 获取 UIBarButtonSystemItem

android-studio - Android Studio 显示错误的文件内容

android - 我想检查手机是否包含 SD 卡,如果 SD 卡不可用,以下方法总是返回 true

android - Android-设置Google Play服务和Firebase-Gradle构建错误

android - 在android中使用twitter api的登录问题

java - 如何模拟和验证在子类中调用的 ScheduledExecutorService 方法

android - 使用最小磁盘空间的 Android Studio

java - 我应该如何在 Android Studio 3.0.1 中导出到 zip 文件

android - 如何在 ListView 中显示空列表?

c# - 使用 Nsubstitute 模拟 RestClient ExecuteAsync