独立测试项目在Android库项目

独立测试项目在Android库项目

本文介绍了独立测试项目在Android库项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是:如何创建一个Android的单机的测试项目为一个Android库

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

我有我的Andr​​oid的的(这是在项目设置标记为库),并包含我的JUnit测试类,一个Android项目。在测试的项目正常(在项目设置的Android)引用了Android库。

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").

我的的源$ C ​​$ C位于包 com.mayastudios 。我所有的测试案例也设在同一个包(但在不同的项目)。所以基本上我有这样的事情:

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
   +- ...

下面是Android清单为的测试的项目:

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>

然而,当我运行的测试的项目(从Eclipse中使用ADT,使用运行方式 - > Android的JUnit测试)我得到以下错误:

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

我试图删除&LT;仪器仪表及GT; 从没有工作清单标签

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

我得到的唯一途径这个至今工作是创建一个默认的Andr​​oid项目(与活动),从我的测试项目中引用它,并使用这个默认的Andr​​oid项目的包名称 targetPackage &LT;仪器仪表&GT; 。但是,这不是我想要的。我希望有一个独立的测试项目。

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.

有什么建议?

推荐答案

嗯,答案就是这么简单。该错误是在测试的Andr​​oid清单的第3行项目:在这里,错包被提及。因此,修正清单如下:

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.

这篇关于独立测试项目在Android库项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 07:40