Android Espresso:给定版本的 JUnit 不支持测试过滤。请将 JUnit 版本至少升级到 4.6

Android Espresso: Test filtering is not supported for given version of JUnit. Please upgrade JUnit version to at least 4.6

我真的很害怕 post 这个,但是我在 google 上没有找到任何相关的东西,所以我想这是一个菜鸟问题。

我想使用 Espresso 来测试我的 android 应用程序。

我已经尝试下载示例项目,创建一个简单的项目并按照 android 开发人员站点中的描述实施它,但我无法启动和 运行ning。

手动添加静态导入后,我解决了示例测试中的所有编译问题。 但是当我 运行 它时,我得到这个:

:app:cleanTest UP-TO-DATE
:app:preBuild UP-TO-DATE
:app:preDebugBuild UP-TO-DATE
:app:checkDebugManifest
:app:preReleaseBuild UP-TO-DATE
:app:prepareComAndroidSupportAppcompatV72200Library UP-TO-DATE
:app:prepareComAndroidSupportSupportV42200Library UP-TO-DATE
:app:prepareDebugDependencies
:app:compileDebugAidl UP-TO-DATE
:app:compileDebugRenderscript UP-TO-DATE
:app:generateDebugBuildConfig UP-TO-DATE
:app:generateDebugAssets UP-TO-DATE
:app:mergeDebugAssets UP-TO-DATE
:app:generateDebugResValues UP-TO-DATE
:app:generateDebugResources UP-TO-DATE
:app:mergeDebugResources UP-TO-DATE
:app:processDebugManifest UP-TO-DATE
:app:processDebugResources UP-TO-DATE
:app:generateDebugSources UP-TO-DATE
:app:compileDebugJava UP-TO-DATE
:app:preCompileDebugUnitTestJava
:app:compileDebugUnitTestJava UP-TO-DATE
:app:compileDebugUnitTestSources UP-TO-DATE
:app:mockableAndroidJar UP-TO-DATE
:app:assembleDebugUnitTest UP-TO-DATE
:app:testDebug
:app:testDebug FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:testDebug'.
> Test filtering is not supported for given version of JUnit. Please upgrade JUnit version to at least 4.6.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
BUILD FAILED
Total time: 32.847 secs
Test filtering is not supported for given version of JUnit. Please upgrade JUnit version to at least 4.6.

这是我的测试class:

package com.example.federicoponzi.testingexample;

import org.junit.runner.RunWith;

import static android.support.test.espresso.Espresso.onView;
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;
import static android.support.test.espresso.matcher.ViewMatchers.withContentDescription;
import android.app.Activity;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;
import android.test.ActivityInstrumentationTestCase2;
import android.test.suitebuilder.annotation.LargeTest;
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.click;
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;

import android.app.Activity;
import android.test.ActivityInstrumentationTestCase2;

import android.support.test.espresso.action.ViewActions;
import android.support.test.espresso.matcher.ViewMatchers;


/**
 * Created by FedericoPonzi on 10/04/2015.
 */

import org.junit.Test;

@RunWith(AndroidJUnit4.class) //copied from a google example
@LargeTest
public class MainActivityTest extends ActivityInstrumentationTestCase2<MainActivity> {
    public MainActivityTest(){
        super(MainActivity.class);
    }
    Activity mActivity;
    @Override
    protected void setUp() throws Exception {
        super.setUp();

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

    }
    private final String STRING_TO_TYPE = "Hello, Testing";
    @Test
    public void testChangeText_sameActivity()
    {
        onView(withId(R.id.edittextview)).perform(typeText(STRING_TO_TYPE), closeSoftKeyboard());
        onView(withId(R.id.edittextview)).check(matches(withText(STRING_TO_TYPE)));

    }

}

Build.gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1"

    defaultConfig {
        applicationId "com.example.federicoponzi.testingexample"
        minSdkVersion 15
        targetSdkVersion 22
        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(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:appcompat-v7:22.0.0'
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.0'
    androidTestCompile 'com.android.support.test:testing-support-lib:0.1'
}

编辑: gradle 更新后的新错误:

FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:testDebug'.
> No tests found for given includes: [com.example.federicoponzi.testingexample.MainActivityTest]
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
BUILD FAILED
Total time: 7.246 secs
No tests found for given includes: [com.example.federicoponzi.testingexample.MainActivityTest]

重要提示:

androidTestCompile - androidTest 文件夹 - ui 测试

testCompile - test 文件夹 - 单元测试

对于Unit测试:

添加:

testCompile 'junit:junit:4.12'

收件人:

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:appcompat-v7:22.0.0'
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.0'
    androidTestCompile 'com.android.support.test:testing-support-lib:0.1'
    testCompile 'junit:junit:4.12' // <-- added
}

你的 Espresso 测试:

删除 Junit 和注释。

public class MainActivityTest extends ActivityInstrumentationTestCase2<MainActivity> {
    public MainActivityTest(){
        super(MainActivity.class);
    }
    Activity mActivity;

    @Override
    protected void setUp() throws Exception {
        super.setUp();

        mActivity = getActivity();
        // injectInstrumentation(InstrumentationRegistry.getInstrumentation()); // not sure what this is 

    }
    private final String STRING_TO_TYPE = "Hello, Testing";

    public void testChangeText_sameActivity()
    {
        onView(withId(R.id.edittextview)).perform(typeText(STRING_TO_TYPE), closeSoftKeyboard());
        onView(withId(R.id.edittextview)).check(matches(withText(STRING_TO_TYPE)));
    }
}

示例:

https://github.com/googlesamples/android-testing/blob/master/espresso/BasicSample/app/src/androidTest/java/com/example/android/testing/espresso/BasicSample/ChangeTextBehaviorTest.java https://github.com/jaredsburrows/AndroidGradleTemplate

官方文档: https://developer.android.com/tools/testing-support-library/index.html and https://code.google.com/p/android-test-kit/wiki/AndroidJUnitRunnerUserGuide