27

I wrote a testcase class for a library project,I created this test case project separately.

when I am trying to run this test project.the console is showing the error "Unable to find instrumentation target package: <with package name>",

I google for the solution also,in some sites they gave check the tag android:targetPackage attribute in AndroidManifestFile .

I checked the manifest the instrumentation tag is correct it is targeting correctly the libraryproject package.

Could any one help in this,to make my testcase project for a library runnable

Janusz
  • 187,060
  • 113
  • 301
  • 369
Sindhu Perumalla
  • 425
  • 1
  • 6
  • 14

4 Answers4

24

You have to create and setup an Application Project that depends on the Library Project, in order to test the Library Project. Quoting from official developer's guide:

Testing a Library Project

There are two recommended ways of setting up testing on code and resources in a library project:

  • You can set up a test project that instruments an application project that depends on the library project. You can then add tests to the project for library-specific features.

  • You can set up a set up a standard application project that depends on the library and put the instrumentation in that project. This lets you create a self-contained project that contains both the tests/instrumentations and the code to test.

Android Library Projects are not built directly, but are built along with the Main Application Project. In another words, you cannot compile it directly to its own .apk and run it on an Android device. On the other hand, instrumentation tests work by running the test project application ("Run As..."->"Android JUnit Test" in Eclipse) after installing both app.apk and test.apk on the device, and then using test.apk to manipulate the app.apk—hence the "instrumentation" part of the test name.


Update:

Note that if you use second approach, you can create a Test Project testing Library Project without creating a third Application Project, because a Test Project itself is also a regular Application Project.

Jon Adams
  • 24,464
  • 18
  • 82
  • 120
yorkw
  • 40,926
  • 10
  • 117
  • 130
  • Thank you very much for the reply So now I will write test case for the sample project which will use our library project ,In our library project we have some server interactions can We write test cases to test our library project and can we execute the test cases which compares the values depending upon the results from server response.And I have another doubt should we include the target package name of library project or sample project(which uses the library project). – Sindhu Perumalla Apr 16 '12 at 11:37
  • Simple answer for both of your questions are YES. and the target package name of instrumentation test must always point to a concrete app.apk application, i.e. your sample project(which use the library project). – yorkw Apr 16 '12 at 11:41
  • How to write test case for server interaction ? please provide me any link for reference – Sindhu Perumalla Apr 16 '12 at 12:10
  • What kind of server interaction? your question is too vague and it is hard to answer in comment, you'd better try something out yourself first and ask specific new question if you got any trouble. At the mean time, worth to check out [this Q&A](http://stackoverflow.com/questions/10136682/how-to-unit-test-a-class-that-uses-httpclient-in-android-using-the-built-in-fram) for a case study (unit-test HttpClient). – yorkw Apr 16 '12 at 20:59
11

When you create android test project remember to set good target package

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.test" <-------------------
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk android:minSdkVersion="8" />
    <instrumentation
        android:name="android.test.InstrumentationTestRunner"
        android:targetPackage="com.example.test" >  <-----------------
    </instrumentation>
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <uses-library android:name="android.test.runner" />
        <activity android:name="TestActivity" />
    </application>
</manifest>
Gelldur
  • 11,187
  • 7
  • 57
  • 68
7

In my case, I was running with gradle, running just the connectedInstrumentTest task with

./gradlew connectedInstrumentTest

and I was getting the "Unable to find instrumentation info" error... The problem was I also needed to include the installDebug task to be executed before the connectedInstrumentTest task, like this:

./gradlew installDebug connectedInstrumentTest
gsaslis
  • 3,066
  • 2
  • 26
  • 32
  • 1
    This helped. I was confused because I didn't get the error the first time I ran ./gradlew connectedCheck - because the app was already installed on the device! – Vito Andolini Jan 03 '15 at 23:16
0

In my case I run instrumentation tests in Firebase. See https://firebase.google.com/docs/test-lab/android/command-line#running_your_instrumentation_tests for information.

First we should create application and test apks:

gradlew assembleDebug
gradlew assembleAndroidTest  (or similar tasks)

Firebase will create app/build/outputs/apk/.../app-debug.apk and app-debug-androidTest.apk for you. Then you can use these files for instrumentation test:

gcloud firebase test android run \
  --type instrumentation \
  --app <...>/app-debug.apk \
  --test <...>/app-debug-androidTest.apk
  ...

I made a mistake and created the first apk with a command like gradlew assembleDev that made a file with another package, not com.xyz.debug, but com.xyz.dev.

CoolMind
  • 26,736
  • 15
  • 188
  • 224