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

时间:2015-04-12 20:05:31

标签: android junit android-espresso

我真的害怕发布这个,但我没有在谷歌上发现任何与之相关的内容,所以我猜这是一个菜鸟问题。

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

我尝试下载示例项目,创建一个简单的项目并按照Android开发人员网站中的描述实现它,但我无法启动并运行。

  • 我有构建工具22.0.1
  • 支持库22

手动添加静态导入后,我已经通过示例测试解决了所有编译问题。 但是当我运行它时,我明白了:

: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.

这是我的测试类:

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]

1 个答案:

答案 0 :(得分:13)

重要的是要知道:

androidTestCompile - androidTest 文件夹 - ui测试

testCompile - 测试文件夹 - 单元测试

对于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.htmlhttps://code.google.com/p/android-test-kit/wiki/AndroidJUnitRunnerUserGuide