I'm getting this error when trying to run unit tests from Eclipse with an Android Project. The list of Instrumentation Test Runners is empty in the Android preferences.
[2009-06-17 23:57:51 - MyApp] ERROR:
Application does not specify a
android.test.InstrumentationTestRunner
instrumentation or does not declare
uses-library android.test.runner
It's also annoyingly decided that because I tried to run a unit test once, that's what I always want to do.
You're probably missing the uses-library and instrumentation nodes in your AndroidManifest.xml:
<manifest ...>
<application ...>
<!-- ... -->
<uses-library android:name="android.test.runner" />
</application>
<instrumentation android:name="android.test.InstrumentationTestRunner"
android:targetPackage="your.package"
android:label="your tests label" />
</manifest>
In the Run Configuration you may have Android JUnit Test, if there are any new launch configuration entries inside this, you delete it and then run your application it will run.
NOTE - This is likely to be the solution if you tried to run the test case before adding the correct lines to the manifest as described in the answer from Josef. If you have done this, delete the configuration (which will be complaining that no instrumentation test runner has been specified in its header) and then run it as an Android Junit Test again and it will create a valid configuration picking up the correct stuff that you have added to the manifest (see Josef's answer for this).
One thing I noticed in this discussion that might be tripping some people up is that you need to make sure the "instrumentation" element in your manifest is a child of "manifest" and not of "application." (The examples here are correct, but this easy to mix up.)
http://developer.android.com/guide/topics/manifest/instrumentation-element.html
If you put your instrumentation stuff inside application, it won't be picked up, and your choices in the Eclipse ADT plugin for instrumentation runner may be blank. (But no error is thrown or shown, etc.)
Just do a right click on your test class from eclipse IDE and click on "Run As". After this select "run Configuration" which will launch a Confiuration Window in eclipse and you need to click on the radio button next to the "Instrumentation Runner" and select the configured Instrumentation Runner from the drop down. Now click on apply and then click on Run .
I think this will solve your problem.
Thanks,
Smruti
It's not in your code, it's just eclipse is a little buggy. In your run configurations it could be trying to run a jUnit test, but select Run Application and that error will go away.
Besides ensuring that the below items are declared in the manifest of your test app, check in the Run Configuration that the "Instrumentation runner" field is set to
"com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner".
This what I ran into when figuring out why I test wouldn't run.
Manifest:
<instrumentation android:name="android.test.InstrumentationTestRunner"
android:targetPackage="your.package"
android:label="your tests label" />
and...
<uses-library android:name="android.test.runner" />
The problem is when you created the project, you would have had a AVD, so these configuration would be missing. My suggested way is first create the AVD and then create the android project :).
If you would have already created the project and if does not have much code you have written I would suggest to delete it and create a new one.
Related
I'm trying to test using the following directory structure (which was setup by Android Studio):
I can run some tests just fine, and even the AllTests.java runs fine without the AndroidManifest.xml file even being there. The thing is, for one of my new tests, I need the android.permission.INTERNET permission. So, I added the following to the AndroidManifest.xml file located within the androidTest directory:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.core"
android:versionCode="2"
android:versionName="2.0" >
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
Unfortunately, this doesn't work. I'm still getting the following error when I run one of my tests:
E/RestAPIRequestTest﹕ Permission denied (missing INTERNET permission?)
I've tried setting the package to be com.example.core.test in my AndroidManifest.xml file (since that is what it shows up as in my Settings->Apps list), but with no joy.
I'm thinking it's not even recognizing the AndroidManifest.xml file, since the version number doesn't show in the Settings for the test app, either.
How can I inject the correct permissions for my test project?
I needed to do something similar. I created a folder named "debug" next to androidTest, which corresponds to the debug variant of the app, and put an AndroidManifest.xml with the permission in that folder. Then the permission works under test since the test app uses the debug variant. It's not ideal because it blurs the line between test and debug, which aren't quite the same thing.
I think what's happening is that the permissions in androidTest/AndroidManifest.xml are going to the test app, not the target app, although it's not 100% clear to me if there are actually two different APKs or what.
In older versions of Android Studio and the Android Gradle plugin the androidTest/AndroidManifest.xml file was ignored. This was documented at the tools.android.com site at the time.
With the Android Studio 1.0+ and Android Gradle 1.0+ plugin launch in December 2014 the AndroidManifest.xml file should now be merged with the normal main/AndroidManifest.xml files (in addition to the debug and release manifest files if they exist). More details regarding the manifest merging rules are here.
If you still run into issues or are just debugging manifest related testing issues try this
(Adapt this slightly for Windows):
Drop to a terminal
change to to your project directory
cd MyApplication
Build your project, assuming 'debug' is the build type you want to test with, but you could also be testing with 'release' or a build script defined one.
./gradlew assembleDebugTest
Then inspect your test APK manifest:
ls app/build/intermediates/manifests/test/debug/AndroidManifest.xml
View your application APK manifest:
ls app/build/intermediates/manifests/full/debug/AndroidManifest.xml
A merge output log can be found detailing the manifest merging process:
ls app/build/outputs/apk/manifest-merger-debug-report.txt
A couple of extra notes:
An instrumentation element is automatically added to your test APK's AndroidManifest.xml so you should only be adding extra activities, permissions, etc that your test APK needs.
If testing with mock locations your application APK will need the ACCESS_MOCK_LOCATION permission. You can add the permission to your debug/AndroidManifest.xml file or you can define that the test APK and the application APK should use the same userId when deployed (sharedUserId attribute in your AndroidManifest.xml).
This is a known problem.
Currently (AGP <= 3.4.X) is not supporting AndroidManifest test merging.
This is reported here: https://issuetracker.google.com/issues/127986458
and here there is the issue created by one of the Roboelectric maintainers.
The workaround as described here its near the same proposed by user3286293 and currently is the only way to have the manifest merged for testing purposes.
Hope to see a fix for AGP 3.5 or 3.6
As specified here, during instrumented tests, there are generated two .apk files. If you take a look, the smaller one it's most probably the one named app-debug-androidTest-unaligned.apk and it actually does include the provided permissions.
Inspecting the file with aapt d permissions <apk_file_path>.apk can be useful to see a list of all of them.
Now, there might be an issue with the context itself where the permission is requested. I had a similar problem, trying to write some screenshots on SD card (thus needing the WRITE_EXTERNAL_STORAGE permission).
This answer helped me to fix the problem, although I cannot fully understand why it's necessary.
In few words, you'll need to declare the same android:sharedUserId in both manifests, in order to merge the permissions when both apks are installed on the same device - that happens when tests are running.
This helped me to separate permissions needed just for testing from the one in production.
You need to define that in build.gradle file:
android {
sourceSets {
androidTest.manifest.srcFile "src/androidTest/AndroidManifest.xml"
}
}
One solution would be like build main apk and test apk in single run.
Example: ./gradlew clean :main:assembleDebug :main:assembleDebugAndroidTest.
This will create a new instrumented main application which has all extra permissions required for test application.
I have followed steps as mentioned Robolectric http://robolectric.org/eclipse-quick-start/
I am able to successfully run the test as per steps provided. For a fresh new Project and Test project.
But when I followed same steps for my existing project, then nothing happens only a warning is being shown and test gets terminated.
My existing app is for target android-15 and I am using robolectric-2.2-jar-with-dependencies.jar
P.S. I have created a new Java Project for my existing project.
UPDATE
After hours of debugging I found that, if I remove android:name="com.myapp.bl.MyApplication from AndroidManifest, then its starts executing tests.
<application
android:name="com.myapp.bl.MyApplication"
android:allowBackup="false"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
Is there any addition flag or setting, that I have to pass Robolectric, so that it
understands MyApplication class.
Regards,
Yuvi
I found the issue, there was an exception in MyApplication class. If that exception occurs then application was getting closed.
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.
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.
In my case, the solution was:
View > Tool Windows > Build Variants
Select a *Debug variant
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" … />
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.
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.
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 ...
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.
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.
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.
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.
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.
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.
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.
In my case, the issue was in device too. Other devices were working fine. Simple uninstall and reinstall fixed it.
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.
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.
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.
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"
}
}
I have a seperate Test Project in Eclipse that has been running successfully for a while in both command line and Eclipse. While using Jenkins to run my tests, I've run into the issue where the standard InstrumentationTestRunner does not output in a Jenkins supported xml format. I've read on the internet to use a custom InstrumentationTestRunner. This works in the command line using ADB, but fails in Eclipse when running as Android Test Case.
I've downloaded a custom instrumentation test runner (com.neenbedankt.android.test) and added it to the AndroidManifest like this:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.testedapplication.test"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="7" />
<instrumentation
android:name="com.neenbedankt.android.test.InstrumentationTestRunner"
android:targetPackage="com.testedapplication" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<uses-library android:name="android.test.runner" />
</application>
</manifest>
Here is the error that I get in Eclipse:
[Test Project] is not configured correctly for running tests:
A targetPackage attribute for instrumentation android.test.InstrumentationTestRunner in its AndroidManifest.xml could not be found!
You can see that i've set the targetPackage there, so I'm not sure what else I can do?
Add both instrumentation in your AndroidManifest.xml.
<instrumentation
android:name="android.test.InstrumentationTestRunner"
android:targetPackage="com.example.myapp" />
<instrumentation
android:name=".MyRunner"
android:targetPackage="com.example.myapp" />
Then go to Package explorer --> $(Your Test Prject$) --> Run As --> Run configurations --> Android JUnit Test --> $(Your Test Project) --> Instrumentation Runner and select your runner there.
To make eclipse select custom runner by default when doing run as/android junit - simply switch the order in manifest file. Make sure yours is first
<instrumentation
android:name="*.Custom.TestRunner"
android:targetPackage="com.*" />
<instrumentation
android:name="*.InstrumentationTestRunner"
android:targetPackage="com.*" />
Since I can't see your whole project setting, here's a couple check list I'd try.
Do you have a separate Test Project in Eclipse? Is the above instrumentation block in your source project's manifest file or in the test project's manifest file?
Did you include the <uses-library android:name="android.test.runner" /> block?
Did you put the test project's instrumentation block outside the <application> block and the <uses-library> block inside the test project's application block?
Did you try creating a Test Project in Eclipse through the "New Project -> Android Test Project", and just changing the instrumentation class there after adding the source code? If you don't change the instrumentation class does it work?
Are you running the test in Eclipse by doing "Run As -> Android JUnit test"?
If you already did all that, I think pasting your whole AndroidManifest file would help clarify a bit, and whether or not you have 2 separate projects or not.
Ah so far I've been able to get it to work by having both the android.test.InstrumentationRunner runner AND the customer test runner described in the manifest file. I seems that running from Eclipse will use the android.test.InstrumentationRunner and running from the command line will use the custom test runner if its setup in the ant script.
I know this is silly question, but i am just stuck with this:
1.I have one main project called MainProject.
2. Inside that there is one test project which has its own source and menifest file and in menifest file i added all required to make it test project like this :
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.app.tests" android:versionCode="1"
android:versionName="1.0">
<application>
<uses-library android:name="android.test.runner" />
</application>
<uses-sdk android:minSdkVersion="3" />
<instrumentation android:name="android.test.InstrumentationTestRunner"
android:targetPackage="com.example.app" android:label="Tests for My App" />
</manifest>
Now i right click on it go to "Run As" and "Run Configuration" and select "Android JUnit Test" but it showing error:
MainProject does not specify a android.test.InstrumentationTestRunner instrumentation or does not declare uses-library android.test.runner in its AndroidManifest.xml
Any help plz?
There is one video link of #Lucifer below which is helpful and also my own answer which a link you can check which is also helpful.
Robotium is an Android test automation framework that has full support for native and hybrid applications. Robotium makes it easy to write powerful and robust automatic black-box test cases. With the support of Robotium, test case developers can write function, system and acceptance test scenarios, spanning multiple Android activities.
You can develop powerful test cases, with minimal knowledge of the application under test.
The framework handles multiple Android activities automatically.
Minimal time needed to write solid test cases.
Readability of test cases is greatly improved, compared to standard instrumentation tests.
Test cases are more robust due to the run-time binding to GUI components.
Fast test case execution.
Integrates smoothly with Maven or Ant to run tests as part of continuous integration.
select File/new/project
select Android Test Project
give the Project a name
select test target as the other project you already have, in your posting above "MainProject"
pick the target platform, ie Android 4.0
finish and the test project is made
add classes under src in the test project that extend one of the android.test classes
I am giving answer to my own question,rather it is not a answer it is a link that you can find which is very nice and easy to understand how you can do android unit test using Robotium
Here is the link: LINK