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
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'd like to know if there is a way to add certain permissions (or anything) to an android manifest file, but so that its only used during test runs - not production. I'm looking for something programmatic, not cutting and pasting when I'm testing.
Here's the context:
I'm reading this article: http://developer.android.com/training/location/location-testing.html, the best practice for test running an app used to be creating a 'test-app' however with android studio we now are not meant to create a new app - all testing should be done through the app. (Thank you gradle)
The issue is that this article is written with a testing permission (ACCESS_MOCK_LOCATION) in it, and I don't want that sitting in my app - and if there's a good way of doing it, I'd like to do that.
UPDATE: The reason I had this problem was because of a misunderstanding of the set up of android studio architecture since the migration to Gradle.
I didn't realize that the build types shared the 'androidTest' and 'main' source folders. And so when testing or running the unfinished app, it takes the debug files (if any) and adds all the production stuff to it. So in my case, I added a empty manifest file in debug and simply added the two permissions to it. When I run or test, gradle adds all of my apps things from its other manifest to it this skeletal file (or vice versa, I'm uncertain).
So in the end we don't need to modify the androidTest folder (in fact I don't think we are allowed to add a manifest here) as its completely generated based off of whether a user is running on debug or deployment. Cheers! :-)
Let's say you use the default debug and release build types and you run your tests against the debug build type.
In this case you can create a src/debug/AndroidManifest.xml and add the additional permissions you need in your debug builds:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="your.package">
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"/>
</manifest>
If your project does not follow the default folder hierarchy and you want to add permissions to the debug build add the following block.
sourceSets {
debug {
manifest.srcFile '<your folder>/AndroidManifest.xml'
}
}
I feel there is much confusion around how to use JUnit to test an Android Library project.
I will describe all the errors I encountered so far(for the sake of other programmers trying to do the same) and at the end I will specify my last and current problem.
Since a Library is not an Application, it does not produce an .apk file in the bin directory, just a .jar file, and you need an .apk to use Android JUnit testing
So you definitely need to create a new project for the tests, follow this guide
Note: Even though the guide tells you to use TestCase and its derived class AndroidTestCase, several posts seem to point that TestCase is used only for JUnit3 tests, JUnit4 tests do not extend any class.
I don't know what I am supposed to use when you want to run a test that uses the Android API, if you want to use it under JUnit4, I have not reached that point yet.
For tests that do not use the Android API, use JUnit4 style class. If you follow only the guides your test will fail when you run it "Invalid layout of ... at value". This is because the run configuration includes Android in its classpath. Go to the JUnit test run configuration for the test (Right click on the test->Properties->Run/Debug Settings->Edit) and remove Android from classPath boostrap entries.
EDIT
When you run the test for the first time, in Eclipse, you have to choose which launcher to use "Eclipse JUnit Launcher" or "Android JUnit Test Launcher" if you choose the Android launcher, no need to remove the bootstrap entry.
From now on I had no success. For tests that use the Android API, extend AndroidTestCase. These tests will take longer to launch, they need an emulator running, even if you are not testing the application itself but a utility class with no interface to the user.
To run Android UNIT case test the launcher looks for the target package APK file, which a Library target project does NOT have.
The target package was automatically inserted in the manifest of your test project when you followed the guide in #2. The manifest for the test project should look like this:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.Common.lib.test"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="14" />
<instrumentation
android:name="android.test.InstrumentationTestRunner"
android:targetPackage="com.Common.lib" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<uses-library android:name="android.test.runner" />
</application>
</manifest>
Here the target package is the Library I am trying to test. A solution is given in this post It is to use the test project as application which has an APK. So now I changed the target package from
android:targetPackage="com.Common.lib" />
to
android:targetPackage="com.Common.lib.test" />
But still the launcher looks for the Library APK, even though I added the Library as reference in the test project, see the logcat below (Note the CommonLibTest.apk is loaded succesfully):
[2014-02-24 16:17:50 - CommonLibTest] Dx
trouble writing output: already prepared
[2014-02-24 16:17:51 - CommonLibTest] ------------------------------
[2014-02-24 16:17:51 - CommonLibTest] Android Launch!
[2014-02-24 16:17:51 - CommonLibTest] adb is running normally.
[2014-02-24 16:17:51 - CommonLibTest] Performing android.test.InstrumentationTestRunner JUnit launch
[2014-02-24 16:17:51 - CommonLibTest] Automatic Target Mode: using existing emulator 'emulator-5554' running compatible AVD 'Phone-x86-jb'
[2014-02-24 16:17:51 - CommonLibTest] Uploading CommonLibTest.apk onto device 'emulator-5554'
[2014-02-24 16:17:52 - CommonLibTest] Installing CommonLibTest.apk...
[2014-02-24 16:17:52 - CommonLibTest] Success!
[2014-02-24 16:17:52 - CommonLib] Could not find CommonLib.apk!
[2014-02-24 16:17:52 - CommonLibTest] Launching instrumentation android.test.InstrumentationTestRunner on emulator-5554
[2014-02-24 16:17:53 - CommonLibTest] Test run failed: Instrumentation run failed due to 'java.lang.ClassNotFoundException'
This is as far as I got.
If want to believe someone already has tested a Library project succesfully with JUnit and has the solution to my problem.
To complete the tale, I solved the last obstacle and the test now runs.
Lets start with the bottom line:
AndroidTestCase requires JUnit3 tests. The annotation #Test fails the test with ClassNotFound for the org.junit.Test class.
Once I commented out all #Test annotations the test started to run.
One more guideline: test names should start with the prefix "test" for JUnit3
Strangely the JUnit4 annotation #BeforeClass did not trigger a failure.
Conclusion:
JUnit4 test styles is only good for pure Java Unit tests. For Android Unit tests use JUnit3 style, since JUnit4 is backwards compatible it will compile fine.
Please note that this post refers only to Unit testing, I haven't got to the stage where I need to test the Android application itself with functional tests.
Some findings:
The apk is "needed" since the wizard adds the project-under-test as a project dependency (see properties in the test-project). During testing the framework needs to upload that project to the device somehow, hence the need for an apk. Since there is no apk there will be a warning message. Removing this dependency silences the warning. Note, since no apk is uploaded no library is uploaded to the device either, giving NoClassDefFound exceptions later on in the tests.
In my case, similar to the original post, removing the project-under-test dependency from the test-project and adding all needed jars manually, both built and required, from the-project-under-test solved it. I didn't needed the extra "test application" dependent on the project-under-test some posts elsewere suggests. Maybe this is a convinient solution if one otherwise have a lot of jars to add manually.
Both keeping the project dependency and manually adding the required jars led to problems resolving classes yeilding in NoClassDefFound exceptions. I don't now why.
I'm a newbie for Android and I need help regarding this issue. I am developing a game using Eclipse. In my project, I added AndEngine and AndEngine Augmented Reality as libraries. I was testing the project out in my Android device, every time I tap the text which uses the class of AR, it force close. I was told to register AndEngine in my Android Manifest file. I have checked about it and used <uses-library />. Now, I placed these two lines in my manifest:
<uses-library android:name="org.andengine.extension.augmentedreality"
android:required="true"/>
<uses-library android:name="org.andengine"
android:required="true"/>
but I get this error message: Installation error: INSTALL_FAILED_MISSING_SHARED_LIBRARY
when I comment out those <uses-library /> lines, I can run the app but it force close when tapping the text that directs to the class of AR. Anything wrong in using it? Or any better way in doing it? Please post all your advises in an easy-to-understand-for-newbies way. THANK YOU!
<uses-library> is not meant to include libraries in your application. It is used to restrict app availability in Google Play based on the availability of a library already on the device like maps. AndEngine is a library project so include it in your build via Ant, Maven or whatever IDE you use.
In case you aren't familiar with what a library project is here's a link:
https://www.vogella.com/tutorials/AndroidLibraryProjects/article.html
If this element is present and its android:required attribute is set
to true, the PackageManager framework won't let the user install the
application unless the library is present on the user's device.
<uses-library> - specifies a shared library that the application must be linked against. This element tells the system to include the library's code in the class loader for the package.
creat a libs folder inside the res folder and put the AndEngine jars in it.set the class path of jar.
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.