Test running failed: Unable to find instrumentation info for: ComponentInfo{} — error trying to test in IntelliJ with Gradle

后端 未结 10 1853
野的像风
野的像风 2021-02-01 01:14

Everytime I try to run my tests the console says this:

Running tests
Test running startedTest running failed: Unable to find instrumentation info for:
ComponentI         


        
相关标签:
10条回答
  • 2021-02-01 01:35

    I had to do a combination of VikingGlen's answer, Liuting's answer, and this answer. This answer works for Android Studio version 2.1.

    1. Run -> Edit Configurations... -> General -> Specific instrumentation runner (optional): "android.support.test.runner.AndroidJUnitRunner"

    2. In build.gradle (the one with all your dependencies), put this:

    defaultConfig {
       testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    } 
    

    Then it could run tests of this type:

    @RunWith(AndroidJUnit4.class)
    @LargeTest
    public class ApplicationTest {
    
        @Rule
        public ActivityTestRule<MainActivity> mActivityTestRule = new ActivityTestRule<>(MainActivity.class);
    
        @Test
        public void login() {
            //Open Drawer to click on navigation.
            onView(withId(R.id.drawer_layout))
                    .check(matches(isClosed(Gravity.LEFT))) // Left Drawer should be closed.
                    .perform(open()); // Open Drawer
        }
    }
    
    0 讨论(0)
  • 2021-02-01 01:37

    I had this problem and fixed it by going to Run -> Edit Configurations -> Green '+' button at the top left -> JUnit

    From there, set the 'use the classpath mod...' to 'app' (or your default app name, which is the one that appears to the left of the run (play button) when you run the app)

    Finally, put your test class name in the 'class:' textbox. Click apply and okay.

    At this point, if the test class doesn't have other errors, it should work.

    0 讨论(0)
  • 2021-02-01 01:38

    The solution for my problem is to change the method name from

    @Test
    public void test() {
        ...
    }
    

    to

    @Test
    public void testSomething() {
        ...
    }
    

    Hope it helps someone.

    0 讨论(0)
  • 2021-02-01 01:44

    This is what I noticed in my project, in my app(main) module build.gradle I had the following buildType configuration

    buildTypes {
            debug {
                multiDexEnabled true
            }
    
            mock {
                initWith(buildTypes.debug)
            }
        }
    testBuildType "mock"
    

    When I used AndroidJUnitRunner as the test runner(both from Android Studio) and as testInstrumentationRunner in build.gradle, tests ran without hitch.

    In a submodule that had multiDexEnabled true as defaultConfig

    defaultConfig {
        multiDexEnabled true
        ....
    }
    

    I ran into the problem of

    Test running startedTest running failed: Unable to find instrumentation info for:{mypackage.x.y/android.support.test.runner.AndroidJUnitRunner"}
    

    when I specified AndroidJUnitRunner in IDE and the submodule build.gradle. And this was fixed by specifying MultiDexTestRunner as the test runner in IDE/build.gradle.

    To summarize, Use MultiDexTestRunner to run tests when multiDexEnabled true is specified in build.gradle, else use AndroidJUnitRunner as the test runner.

    0 讨论(0)
提交回复
热议问题