使用Espresso进行测试时,<package>中未找到任何测试

时间:2015-12-19 07:07:27

标签: java android junit android-espresso

我想在我的Android应用中测试我的MainActivity。因此,我创建了第一个测试按钮功能的测试用例。如果用户单击此特定按钮,则应打开一个新活动。

这是我的代码:

@RunWith(AndroidJUnit4.class)
public class MainActivityTest {

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

    @Test
    public void startNewActivityTest() {
        Intents.init();
        onView(withId(R.id.main_new)).perform(click());
        intended(hasComponent(NewActivity.class.getName()));
        Intents.release();
    }

    @Before
    public void setup() {
        closeSoftKeyboard();
    }

}

不幸的是,我得到以下异常:

junit.framework.AssertionFailedError: No tests found in package.MainActivityTest
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:191)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:176)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:554)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1701)

这是我的Gradle文件:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 19
    buildToolsVersion "23.0.1"

    lintOptions {
        abortOnError false
    }

    defaultConfig {
        applicationId "package"
        minSdkVersion 14
        targetSdkVersion 19
    }

    signingConfigs {
        debug {
            storeFile file(RELEASE_STORE_FILE)
            storePassword RELEASE_STORE_PASSWORD
            keyAlias RELEASE_KEY_ALIAS
            keyPassword RELEASE_KEY_PASSWORD
        }
        release {
            storeFile file(RELEASE_STORE_FILE)
            storePassword RELEASE_STORE_PASSWORD
            keyAlias RELEASE_KEY_ALIAS
            keyPassword RELEASE_KEY_PASSWORD
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }
}

dependencies {
    compile 'com.android.support:support-v4:19.1.0'
    compile 'com.google.android.gms:play-services:4.2.+'
    androidTestCompile 'com.android.support:support-annotations:19.0.1'
    androidTestCompile 'com.android.support.test:runner:0.4.1'
    androidTestCompile 'com.android.support.test:rules:0.4.1'
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1'
    // Set this dependency if you want to use Hamcrest matching
    androidTestCompile 'org.hamcrest:hamcrest-library:1.3'
    androidTestCompile 'com.android.support.test.espresso:espresso-intents:2.2.1'
}

2 个答案:

答案 0 :(得分:23)

Set the instrumentation runner

  

将以下行添加到同一build.gradle文件中   android.defaultConfig:testInstrumentationRunner   &#34; android.support.test.runner.AndroidJUnitRunner&#34;

apply plugin: 'com.android.application'

android {
    ...

    defaultConfig {
        ...

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

dependencies {
    // App's dependencies, including test
    compile 'com.android.support:support-annotations:23.0.1'

    ...
}

我从未使用Espresso Intents,但您可能需要this here {/ 3>}

  

使用时使用IntentsTestRule而不是ActivityTestRule   咖啡-意图。 IntentsTestRule使其易于使用   功能UI测试中的Espresso-Intents API。这个班是一个   ActivityTestRule的扩展,初始化Espresso-Intents   在每次测试之前用@Test注释并发布Espresso-Intents   每次试运行后。每次测试后活动将终止   并且此规则可以与ActivityTestRule一样使用。

答案 1 :(得分:1)

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

失踪