java - 使用 Espresso 测试时未在 <package> 中找到测试

标签 java android junit android-espresso

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

这是我的代码:

@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'
}

最佳答案

Set the instrumentation runner

Add to the same build.gradle file the following line in android.defaultConfig: testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

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 :

Use IntentsTestRule instead of ActivityTestRule when using Espresso-Intents. IntentsTestRule makes it easy to use Espresso-Intents APIs in functional UI tests. This class is an extension of ActivityTestRule, which initializes Espresso-Intents before each test annotated with @Test and releases Espresso-Intents after each test run. The activity will be terminated after each test and this rule can be used in the same way as ActivityTestRule.

关于java - 使用 Espresso 测试时未在 <package> 中找到测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34368276/

相关文章:

android - 将 appcompat 和 buildtools 切换到 v21,aapt.exe 已停止工作

android - ShareActionProvider 在 android 中没有任何操作栏

android - Android 4.4 Kitkat 上的 Aquery Android 问题

java - 使用 JUnit 处理 EDT/非 EDT 问题的最佳方法是什么?

java - 如何使用带有滚动页面的卡片 View 和 slider ?

java - 为什么我的 JFrame 在几秒钟后关闭?

java - 如何使用 jUnit 测试已处理的异常?

spring - junit中的ContextConfiguration继承

java - 用户输入时的地址验证

java - 循环条件 : why "not-equal" instead of "lower-than"