简体   繁体   English

Android上的图书馆项目的独立测试项目

[英]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? 我的问题是: 如何为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. 我有我的Android (在项目设置中标记为“Library”)和一个包含我的JUnit测试类的Android项目。 The test project correctly references the Android library (in the project settings under "Android"). 测试项目正确引用Android库(在“Android”下的项目设置中)。

My library source code is located in the package com.mayastudios . 我的源代码位于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: 这是测试项目的Android清单:

<?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: 但是,当我运行测试项目时(从带有ADT的Eclipse,使用“Run As - > Android JUnit Test”),我收到以下错误:

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. 我试图从清单中删除无效的<instrumentation>标签。

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> . 我到目前为止工作的唯一方法是创建一个默认的Android项目(带有一个Activity),从我的测试项目中引用它,并使用这个默认Android项目的包名称作为<instrumentation>下的targetPackage But that's not what I want. 但这不是我想要的。 I want a stand-alone test project. 我想要一个独立的测试项目。

Any suggestions? 有什么建议?

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. 错误发生在第3行的测试项目的Android清单中:这里提到了错误的包。 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. 测试项目甚至不需要包含Activity。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM