【问题标题】:Android Studio Espresso Testing Error: Empty Test SuiteAndroid Studio Espresso 测试错误:空测试套件
【发布时间】:2015-04-28 21:51:50
【问题描述】:

尝试在 Android Studio 中执行测试时,我一直遇到以下错误:测试运行失败:找不到仪器信息:ComponentInfo{.test/android.support.test.runner.AndroidJUnitRunner}

我的测试类在 androidTest/java 目录中并且有一个构造函数。我的 build.gradle 也是正确的。任何帮助表示赞赏。

测试类

@RunWith(AndroidJUnit4.class)
@LargeTest
public class AndroidUITests extends ActivityInstrumentationTestCase2<UserActivity>{

    private UserActivity userActivity;

    public AndroidUITests() {
        super(UserActivity.class);
    }


    @Before
    public void setUp() throws Exception {
        super.setUp();
        injectInstrumentation(InstrumentationRegistry.getInstrumentation());
        userActivity = getActivity();
    }

    @Test
    public void testPhoneIconIsDisplayed() {
        // When the phone_icon view is available,
        // check that it is displayed.
        onView(ViewMatchers.withId(R.id.groupCreate)).perform(click())
                .check(matches(withText("Enter a name")));
    }
}

app/build.gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"

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

    packagingOptions {
        exclude 'LICENSE.txt'
    }
}

dependencies {
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.0'
    androidTestCompile 'com.android.support.test:testing-support-lib:0.1'
}

【问题讨论】:

标签: android testing junit android-espresso


【解决方案1】:

虽然问题已经得到解答,但认为值得为未来的访问者发布。

确保您检查 logcat 日志,以确保在测试运行之前不会导致问题(崩溃)。我的 @BeforeClass 块中有错误代码,尽管已正确设置测试运行器,但导致 Android Studio 中出现“空测试套件”消息。

【讨论】:

  • 还要确保你设置了正确的运行配置——如果你使用“All in package”选项,如果你使用applicationIdSuffix选项,它可能会与applicationId不匹配
  • 谢谢是这样,我希望错误没有误导:)
【解决方案2】:

您必须更新测试“编辑配置”并包含 AndroidJUnitRunner 作为检测运行器。

我在此视频中找到了解决方案: https://youtu.be/TGU0B4qRlHY?t=9m36s

更新

添加@loeschg 的建议: 确保在测试运行之前检查 logcat 日志以确保某些事情不会导致问题(崩溃)。 @BeforeClass 块中的错误代码可能会导致 Android Studio 中出现“Empty Test Suite”消息,尽管已正确设置了测试运行器。

【讨论】:

  • 无法再在运行/调试配置中指定运行器。
  • 非常感谢更新提示!节省了我的时间!
【解决方案3】:

如果你收到这样的错误信息:

Class not found: "com.example.product.package.name.SomeSpecificTest" Empty test suite.

您需要确保您的 Espresso 测试是否已添加到 Run/Debug ConfigurationsAndroid Instrumented Tests 部分下。

似乎 Android Studio 3.1.3 有时会将其错误地添加到 Android JUnit 部分。

确保它看起来像这样:

另外,值得一提的是,我注意到,当我选择了非 Debug 构建变体时,会出现这个问题。

【讨论】:

  • 我也注意到这件事发生在我身上。从 Android JUnit 部分中删除了测试,并且在下一次测试中清理项目确实对我有用。
【解决方案4】:

我相当肯定 android.support.test.runner.AndroidJUnitRunner 是由 runner 库提供的。

  1. 安装Android Support Repository(您已经完成了)
  2. 将运行器添加到您的依赖项中

应该是这样的

dependencies {
    ...
    androidTestCompile 'com.android.support.test:runner:0.2'
}

【讨论】:

    【解决方案5】:

    如果有人在 2018 年仍然为此烦恼,这里是工作设置:

    build.gradle必填):

    <...snip...>
    
    android {
        defaultConfig {
            <...snip...>
            testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
            // ^^^ here it is (first)!
        }
    
    <...snip...>
    
    dependencies {
        <...snip...>
    
        androidTestImplementation 'androidx.test:runner:1.1.1'
        androidTestImplementation 'androidx.test.ext:junit:1.1.0'
        androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
        // ^^^ all three required ^^^ (second)
    }
    

    在仪器化测试中(也需要):

    import androidx.test.espresso.Espresso.onData
    import androidx.test.espresso.Espresso.onView
    import androidx.test.espresso.action.ViewActions.*
    import androidx.test.espresso.assertion.ViewAssertions.*
    import androidx.test.espresso.matcher.RootMatchers.*
    import androidx.test.espresso.matcher.ViewMatchers.*
    import androidx.test.ext.junit.runners.AndroidJUnit4
    import androidx.test.filters.LargeTest
    import org.hamcrest.Matchers.*
    import org.junit.Test
    import org.junit.runner.RunWith
    
    @RunWith(AndroidJUnit4::class)
    @LargeTest
    class ApplicationTest {
        @Test
        fun thruStillThrullyThrue() {
            // do interaction with some UI
            onView(withId(R.id.myButtonOnMyActivity)).perform(click())
    
            // assert expected result
            onView(withId(R.id.myTextViewOnMyActivity)).check(matches(isDisplayed()))
        }
    }
    
    // (third)
    

    【讨论】:

      【解决方案6】:

      我的问题是在 build.gradle (app) 中指定了“testInstrumentationRunner”字段。如果您正在使用它,请将其更改为 androidx (androidx.test.runner.AndroidJUnitRunner)

      希望它对某人有所帮助,干杯!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多