Using custom InstrumentationTestRunner in Eclipse causes Error - android

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.

Related

Unable to find instrumentation target package firebase error

I'm writing an instrumentation test for my app in a separated empty project using UIAutomator and AndroidJUnit4 as runner. It runs well on my device. For testing it on Firebase, it needs the application APK which I provided, and the test APK which I took from C:\Users\user\MyApplication\app\build\outputs\apk\androidTest\debug and when I run the test it fails with this error:"Unable to find instrumentation target package:" am I missing something?
I created another manifest into the androidTest package and the target package is red with "cannot resolve symbol" and nothing changed.
I also changed the targetPackage to the name of the package of the application I'm trying to test, in case that was the problem, and nothing changed.
My androidTest manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.myapplication.test"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="18" android:targetSdkVersion="28"/>
<instrumentation
android:targetPackage="com.example.myapplication"
android:name="androidx.test.runner.AndroidJUnitRunner"
/>
<application
tools:replace="label" android:label="SampleTest"/>
</manifest>
After a lot of research, I found out that gradle changes the target package automatically to the application package name which I'm writing test on.
So I had to move my tests to my application project and generate test apk from it, so it will have the same target package while setting the same signature for both.

How to make android:usesCleartextTraffic="true" only for instrumentation tests?

I'm using RESTMock for my instrumentation tests, but it only works if I set usesCleartextTraffic to true in my manifest. I only want that to be true for instrumentation tests, though. Is there a way to do that?
I tried creating a new manifest file in the androidTest folder. The tests run but they fail like usesCleartextTraffic is still false.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="my.package">
<application android:usesCleartextTraffic="true" />
</manifest>
I know that RESTMock supports https starting from version 0.3.2, but I'd rather not have to deal with it. I actually followed their guide and ended up with this error from OkHttp3:
java.lang.AssertionError: java.security.NoSuchAlgorithmException: The BC provider no longer provides an implementation for KeyPairGenerator.RSA. Please see https://android-developers.googleblog.com/2018/03/cryptography-changes-in-android-p.html for more details.
Any ideas?
EDIT:
I followed this answer and moved this manifest I created to the debug source folder and then it worked. Now the android:usesCleartextTraffic="true" option is only applied to my debug build, which is used by the instrumentation tests, so it works, but it still doesn't feel like the proper solution.
For me the solution is to add a simple AndroidManifest.xml in androidTest/AndroidManifest.xml. This is also mentioned in the answer you reference, but in that case it didn't work because old tooling didn't merge this AndroidManifest.xml.
So, inside androidTest directory, and next to java directory, I have the following:
~/source/my-library/src/androidTest develop*
❯ ls
AndroidManifest.xml java
With this AndroidManifest.xml file:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mypackage.mylibrary">
<application
android:usesCleartextTraffic="true" />
</manifest>

how to run android unit test project

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

Android Shared Library not Running

Okay I have a project that is using the android-rss library (org.mcsoxford.rss). I created a separate library project for the android-rss. When I try to run my project I get an error saying that the launch was canceled. "Installation error: INSTALL_FAILED_MISSING_SHARED_LIBRARY". I went through the tutorial on Android on how to reference the library eclipse project. I have everything setup right. I also put in the xml file a uses-library. Not sure what the problem is. Here is my the uses in the Android-Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
...
<uses-library android:required="true" android:name="org.mcsoxford.rss"></uses-library>
...
</manifest>
And I have it referenced in the ANdroid Library. I can build the project and see the reference to the library in the project. No errors nothing. The reference lib is exported too.
Here are my console output errors:
[2011-04-18 11:46:43 - BOTM] Installation error: INSTALL_FAILED_MISSING_SHARED_LIBRARY [2011-04-18 11:46:43 - BOTM] Please check logcat output for more details.
[2011-04-18 11:46:44 - BOTM] Launch canceled!
I haven't check that yet, but maybe it will help.
Please download android-rss from: https://github.com/ahorn/android-rss and look into README file.
That part may be useful:
"To reference this library project from within your Android application,
navigate to the /tools/ directory and use the following command:
android update project \
--target \
--path path/to/your/project \
--library path/to/android-rss
This command appends to the "default.properties" file in your Android
project a new "android.library.reference" property. The value of this
new property should be the relative path to the directory which you
created when you fetched the Android RSS library source code with Git.
The library is compiled by the Android build framework when you build
your mobile app which was specified in the --path argument above."
Did you mention in the application launcher activity in the manifest file as follows,
<application>
<activity android:name=".LoginScreen"
android:label="#string/app_name"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Here Login screen is my main activity to launch as soon as app starts.
While installing the application on to the device, PackageManager checks for the shared libraries that are been used by the application.
If the shared libraries are missing on the target device, it will not allow user to install the application on that device.
So, basically the library "org.mcsoxford.rss" is missing from the device on which you are trying to install your application.
In order to over come this installation issue, you can make the following change on the manifest file:
android:required="**false**"
The above field states that you application will still work with out the library that has been mentioned on the uses-library tag. This will allow the application to be installed on the device, even if the shared library is missing.

Android Eclipse Plugin: Instrumentation Test Runner not specified

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.

Categories

Resources