可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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.
回答1:
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.
回答2:
In my case, the solution was:
- View > Tool Windows > Build Variants
- Select a *Debug variant
回答3:
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" … />
回答4:
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.
回答5:
In my case the Run/Debug Configurations were wrong.
One Solution:
Go to Run/Debug Configurations
Run -> Edit Configurations...
Setup a Android-Test for your test class
Select your Android test configuration on the left side
or create a new one with the plus icon and name it e.g. ClassNameTest
Select the module containing your test class. In the simplest case the test class is in your app module so select app.
Select on the next row your test configuration. I use:
- Class: to run all tests of one class.
Choice your test class
Finally configure your target device and select ok.
回答6:
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.
You find the Run/Debug Configurations: Run -> Edit Configurations ...
回答7:
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.
回答8:
It seams you have not good project structure.
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:
- 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)
- 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.
回答9:
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.
回答10:
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.
回答11:
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.
回答12:
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.
回答13:
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.
回答14:
In my case, the issue was in device too. Other devices were working fine. Simple uninstall and reinstall fixed it.