Android Studio import existing unit tests “Unable to find instrumentation info”

前端 未结 15 1826
故里飘歌
故里飘歌 2021-02-01 12:17

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 te

相关标签:
15条回答
  • 2021-02-01 12:57

    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.

    0 讨论(0)
  • 2021-02-01 12:58

    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)
    
    1. 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.
    0 讨论(0)
  • 2021-02-01 13:00

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

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

    In my case, the solution was:

    • View > Tool Windows > Build Variants
    • Select a *Debug variant
    0 讨论(0)
  • 2021-02-01 13:01

    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" … />
      
    0 讨论(0)
  • 2021-02-01 13:04

    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.

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