Android Studio import existing unit tests "Unable to find instrumentation info"

AndroidJunitAndroid Studio

Android Problem Overview


So I'm trying our Android Studio and testing a project that worked in eclipse. I got everything compiling and the application will launch just fine, but I can't get my unit tests up and working. I eventually got them compiling by adding my applications lib folder as a dependency, but I don't think my run configuration is right because whenever I run my tests I get this error

Installing <packagename>
DEVICE SHELL COMMAND: pm install -r "/data/local/tmp/<packagename>"
pkg: /data/local/tmp/<packagename>
Success


Running tests
Test running started
Test running failed: Unable to find instrumentation info for: ComponentInfo{<packagename>/android.test.InstrumentationTestRunner}
Empty test suite.

Edit: To all new arrivals, the state of Android Studio has changed a lot since I initially posted this question, but many helpful people have continued to post their particular solution for this error. I'd advise sorting by active and checking out the newest answers first.

Android Solutions


Solution 1 - Android

If you have a testInstrumentationRunner defined in your build.gradle such as this:

android {
    defaultConfig {
        testInstrumentationRunner "com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner"

make sure that in the run configuration window you use the exact same "Specific instrumentation runner" in your Android Studio / IntelliJ run configuration for your test.

Solution 2 - Android

In my case, the solution was:

  • View > Tool Windows > Build Variants
  • Select a *Debug variant

Solution 3 - Android

Explanation and solution

This error "Unable to find instrumentation" appears if targetPackage declared in the manifest of the test application is different from the package declared in the manifest of the application being tested:

  • Application being tested:

     <manifest package="com.example.appbeingtested" … >
    
  • Test application :

     <instrumentation
         android:name="android.test.InstrumentationTestRunner"
         android:targetPackage="com.example.appbeingtested" … />
    

Solution 4 - Android

I solved this problem by changing

android.test.InstrumentationTestRunner 

into

com.android.test.runner.MultiDexTestRunner

in EditConfigurations -> Specific Instrumentation Runner (optional) tab.

Turns out, because my app is using MultiDex, I need to change test runner to MultiDexTestRunner as well.

UPDATE:

As @dan comment, InstrumentationTestRunner is deprecated use AndroidJUnitRunner instead.

Solution 5 - Android

In my case the Run/Debug Configurations were wrong.

One Solution:

  1. Go to Run/Debug Configurations

Run -> Edit Configurations...

  1. Setup a Android-Test for your test class

  2. Select your Android test configuration on the left side
    or create a new one with the plus icon and name it e.g. ClassNameTest

  3. Select the module containing your test class. In the simplest case the test class is in your app module so select app.

  4. Select on the next row your test configuration. I use:

  • Class: to run all tests of one class.
  1. Choice your test class

  2. Finally configure your target device and select ok.

Solution 6 - Android

This only works in AndroidStudio Version < 2.3

In my case the wrong instrumentation runner was selected.

I fixed this by specifying the instrumentation runner in the Run/Debug Configuration of the test (see below). There you can select a runner from the list. 1]1

You find the Run/Debug Configurations: Run -> Edit Configurations ...

Solution 7 - Android

It seams you have not good project structure.

  1. Open AndroidManifest.xml and check does it have

    <instrumentation android:name="android.test.InstrumentationTestRunner"
                         android:targetPackage="com.example.YourClass"
                         android:label="Tests for com.example.YourClass"/>
    

If NO do next:

  1. Reorganize your directory structure to the following one: (this is the recomendation from official source)

        MyProject/
              AndroidManifest.xml
              res/
                  ... (resources for main application)
              src/
                  ... (source code for main application) ...
              tests/
                  AndroidManifest.xml
                  res/
                      ... (resources for tests)
                  src/
                      ... (source code for tests)
        
    

2. You see that you need to have inner tests module.
For creating it in Idea IDE do next File -> New Module -> Test Module. After creating you can see one new AndroidManifest.xml. And it has instrumentation declaration inside.

Solution 8 - Android

I've got same problem as @Iuliia Ashomok and tried everything on the internet.

Still no luck.

After 2 days investigation, I found that the problem is created by the mobile phone. .V.

I originally use Xiaomi Mi4i as testing device(rooted) and tests could not be run. Of course, I got the error below.

Test running failed: Unable to find instrumentation info for: ComponentInfo{<packagename>/android.test.InstrumentationTestRunner}

However, when I use Sony Xperia Z3(No root), everything works well.

Solution 9 - Android

I once got this error. I use the sdk tools in CLI mode. And the error happened when i launch 'ant test' in the test project. I later notice that I didn't even build and install the test project before ! (with 'ant debug install') So you should have try to check your running configuration to see if the test project got effectively build before running the test. For the other case, Android Studio use Gradle. I don't know it well but try to check the Gradle settings for the project or the Gradle settings file.

Solution 10 - Android

For me the problem was having this dependency:

debugCompile 'com.android.support.test:rules:0.2'

After I removed it my tests were found and run again.

Note that I didn't get the "Unable to find instrumentation" message but a "No tests found" message.

Solution 11 - Android

It turned out to be a delay problem. It fixed automatically after I waited a while reading the Internet solutions. I recompiled the code, and it worked.

Solution 12 - Android

Since none of these answers helped me, I wanted to share my solution for anyone who is as desperate as I was. :)

Because of the testing libraries that I was using, I needed to enable multidex support by adding multiDexEnabled true to my Gradle build. I'm not sure I had multidex support fully implemented to begin with (the proper way of doing it has changed since I last implemented it) but ultimately, I didn't end up needing it and removing that line from my build fixed the error. My team at work has had a few testing issues related to enabling multidex support… in typical Android style.

Solution 13 - Android

In my case in same classes some test cases were picking correct test runner ( android.support.test.runner.AndroidJUnitRunner ) which was defined in build.gradle, and some test cases were picking up android.test.InstrumentationTestRunner, which was not defined at least in Manifest or build.gradle or edit configuration. Ideally it should have been resolved by Sync Project with Gralde option, though it didn't work.

At last I found wrong test runner defined against a method in .idea/workspace.xml, I changed it manually, and issue got resolved.

Generally we are not supposed to edit this Android Studio generated file, but for me it did worked as last option.

Solution 14 - Android

In my case, the issue was in device too. Other devices were working fine. Simple uninstall and reinstall fixed it.

Solution 15 - Android

This answer is going to explain the history issues and summarise all related settings.

Basically there are 3 possible places for instrumentation test runner configurations.

  1. In EditConfigurations -> General tab -> Specific Instrumentation Runner (optional)
    This is existing in Android Studio 2.2 and version before only, in latest version, it is removed already.

  2. In manifest file, the test runner is configured as below.

    < instrumentation android:name="android.support.test.runner.AndroidJUnitRunner"
           android:targetPackage="com.mytestapp.test"/>

Since "android.test.InstrumentationTestRunner" is deprecated in API level 24, this setting is also not necessary already, as long as you configure the runner in gradle file.
If you want to keep this setting, please make sure the runner name should match to the one you set in the gradle file, otherwise you will got this error also.

  1. In gradle file.
    This is the only recommended way if you use Android Studio v2.3 above.
    android {
    .......
        defaultConfig {  
            .......  
            testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"  
        }   
    }

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAJDView Question on Stackoverflow
Solution 1 - AndroidthoutbeckersView Answer on Stackoverflow
Solution 2 - AndroidMatt McClureView Answer on Stackoverflow
Solution 3 - AndroidBenLView Answer on Stackoverflow
Solution 4 - AndroidaldokView Answer on Stackoverflow
Solution 5 - Androidauerc89View Answer on Stackoverflow
Solution 6 - AndroidPettersonView Answer on Stackoverflow
Solution 7 - AndroidYuliia AshomokView Answer on Stackoverflow
Solution 8 - AndroidVoidExplorerView Answer on Stackoverflow
Solution 9 - AndroidgodidierView Answer on Stackoverflow
Solution 10 - AndroidCiskeView Answer on Stackoverflow
Solution 11 - AndroidmohghaderiView Answer on Stackoverflow
Solution 12 - Androidspaaarky21View Answer on Stackoverflow
Solution 13 - Androiduser7154703View Answer on Stackoverflow
Solution 14 - AndroidBhieferView Answer on Stackoverflow
Solution 15 - AndroidWeidian HuangView Answer on Stackoverflow