Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stand-alone test project for a library project on Android

My question is: How do I create an Android stand-alone test project for an Android library?

I've got my Android library (which is marked as "Library" in the project settings) and an Android project containing my JUnit test classes. The test project correctly references the Android library (in the project settings under "Android").

My library source code is located in the package com.mayastudios. All my test cases are also located in the same package (but in a different project). So basically I have something like this:

+- MyLibraryProject
   +- src
      +- com/mayastudios/MyClass.java
   +- AndroidManifest.xml
   +- ...
+- MyTestProject (references MyLibraryProject)
   +- test
      +- com/mayastudios/MyClassTests.java
   +- AndroidManifest.xml
   +- ...

Here's the Android manifest for the test project:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.spatialite.test"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="10" />

    <instrumentation
        android:name="android.test.InstrumentationTestRunner"
        android:targetPackage="com.mayastudios" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

    <application android:label="Spatialite-NewApi-UnitTests">
        <uses-library android:name="android.test.runner" />
    </application>

</manifest>

However, when I run the test project (from Eclipse with ADT, using "Run As -> Android JUnit Test") I get the following error:

Unable to find instrumentation target package: com.mayastudios

Here's the complete Console log:

[2012-04-24 17:24:20 - spatialite-test] Android Launch!
[2012-04-24 17:24:20 - spatialite-test] adb is running normally.
[2012-04-24 17:24:20 - spatialite-test] Performing android.test.InstrumentationTestRunner JUnit launch
[2012-04-24 17:24:20 - spatialite-test] Automatic Target Mode: using device '3732FBC2711300EC'
[2012-04-24 17:24:20 - spatialite-test] Uploading spatialite-test.apk onto device '3732FBC2711300EC'
[2012-04-24 17:24:20 - spatialite-test] Installing spatialite-test.apk...
[2012-04-24 17:24:22 - spatialite-test] Success!
[2012-04-24 17:24:22 - spatialite-test] Launching instrumentation android.test.InstrumentationTestRunner on device 3732FBC2711300EC
[2012-04-24 17:24:22 - spatialite-test] Collecting test information
[2012-04-24 17:24:23 - spatialite-test] Test run failed: Unable to find instrumentation target package: com.mayastudios

I tried to remove the <instrumentation> tag from the manifest which didn't work.

The only way I got this working so far was to create a default Android project (with an Activity), reference it from my test project and use the package name of this default Android project as targetPackage under <instrumentation>. But that's not what I want. I want a stand-alone test project.

Any suggestions?

like image 362
Sebastian Krysmanski Avatar asked Apr 24 '12 15:04

Sebastian Krysmanski


1 Answers

Ah, the answer is so simple. The error is in the Android manifest of the test project in line 3: Here the wrong package is mentioned. So the corrected manifest looks like this:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mayastudios"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="10" />

    <instrumentation
        android:name="android.test.InstrumentationTestRunner"
        android:targetPackage="com.mayastudios" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

    <application android:label="Spatialite-NewApi-UnitTests">
        <uses-library android:name="android.test.runner" />
    </application>

</manifest>

And to all who say that you need three projects (library, project-under-test, test project): They're wrong. A library project and a test project are enough. The test project doesn't even need to contain an Activity.

like image 168
Sebastian Krysmanski Avatar answered Sep 24 '22 05:09

Sebastian Krysmanski