简体   繁体   English

Android Studio Espresso测试错误:空测试套件

[英]Android Studio Espresso Testing Error: Empty Test Suite

I keep running into the following error when trying to execute my tests in Android Studio: Test running failed: Unable to find instrumentation info for: ComponentInfo{.test/android.support.test.runner.AndroidJUnitRunner} 尝试在Android Studio中执行测试时,我一直遇到以下错误:测试运行失败:无法找到以下内容的检测信息:ComponentInfo {.test / android.support.test.runner.AndroidJUnitRunner}

My test class is in the androidTest/java directory and has a constructor. 我的测试类在androidTest / java目录中并且有一个构造函数。 My build.gradle is correct too. 我的build.gradle也是正确的。 Any help is appreciated. 任何帮助表示赞赏。

Test Class 测试类

@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: 应用程序/的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'
}

While the question is already answered, figured it was worth posting for future visitors. 虽然这个问题已经得到解答,但我认为这对未来的访问者来说是值得的。

Make sure you check the logcat logs to ensure something isn't causing issues (crash) before the test is even run. 确保检查logcat日志以确保在测试运行之前不会导致问题(崩溃)。 I had bad code in my @BeforeClass block which resulted in the "Empty Test Suite" message in Android Studio despite having properly set the test runner. 我的@BeforeClass块中的代码不好,导致Android Studio中出现“Empty Test Suite”消息,尽管已经正确设置了测试运行器。

You have to update the test "Edit Configuration" and include the AndroidJUnitRunner as the instrumentation runner. 您必须更新测试“编辑配置”并包含AndroidJUnitRunner作为仪表运行程序。

在此输入图像描述

I found the solution in this video: https://youtu.be/TGU0B4qRlHY?t=9m36s 我在此视频中找到了解决方案: https//youtu.be/TGU0B4qRlHY?t = 9m36s

Updated 更新

Adding what @loeschg suggest: Make sure you check the logcat logs to ensure something isn't causing issues (crash) before the test is even run. 添加@loeschg建议的内容:确保检查logcat日志以确保在测试运行之前不会导致问题(崩溃)。 Bad code in the @BeforeClass block could resulted in the "Empty Test Suite" message in Android Studio despite having properly set the test runner. @BeforeClass块中的错误代码可能会在Android Studio中产生“空测试套件”消息,尽管已正确设置测试运行器。

If you get such error message: 如果您收到此类错误消息:

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

You need to make sure if your Espresso test is added under Android Instrumented Tests section in Run/Debug Configurations . 您需要确保在Run/Debug Configurations Android Instrumented Tests部分下添加了Espresso测试。

It seems like Android Studio 3.1.3 is sometimes adding it incorrectly to Android JUnit section instead. 似乎Android Studio 3.1.3有时会错误地将其添加到Android JUnit部分。

Make sure it looks something like this: 确保它看起来像这样: 使用Android Instrumented测试和Android JUnit运行/调试配置

Also, worth mentioning that I've noticed, that this issue occurs for me while a non Debug build variant is selected. 另外,值得一提的是,我注意到,当选择非Debug构建变体时,会出现此问题。

I'm fairly certain android.support.test.runner.AndroidJUnitRunner is provided by the runner library. 我很确定android.support.test.runner.AndroidJUnitRunner是由跑步者库提供的。

  1. Install the Android Support Repository (which you've already done) 安装Android支持存储库 (您已经完成)
  2. Add runner to your dependencies 添加运行器到您的依赖项

it should look like this 它看起来应该是这样的

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

If anyone still bother with this in 2018, here is working setup: 如果有人仍然在2018年打扰这个,这里是工作设置:

build.gradle ( required ): 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)
}

And in instrumented tests ( required too): 在仪器化测试中(也需要 ):

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)

My issue was that the field 'testInstrumentationRunner' is specified in the build.gradle (app). 我的问题是在build.gradle(app)中指定了字段'testInstrumentationRunner'。 Change that to the androidx if you are using it (androidx.test.runner.AndroidJUnitRunner) 如果您正在使用它,请将其更改为androidx(androidx.test.runner.AndroidJUnitRunner)

Hope it helps someone, cheers! 希望它可以帮助别人,欢呼!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM