如何测试 Android 库项目

发布于 2024-09-16 06:46:45 字数 165 浏览 8 评论 0原文

我正在编写一个基于 Android Bitmap 类(称为 AndroindLib)的 Android 库项目,它仅包含实用程序类(无活动)。我尝试使用 Android JUnit 对其进行测试,但它一直抱怨找不到 AnroidLib.apk

对 Android 库项目进行单元测试的正确方法是什么?

I am writing an Android Library Project basing on Android Bitmap class (call it AndroindLib) which contains only utility class (no activity). I tried to test it using Android JUnit, but it keeps complaining that can't find the AnroidLib.apk

What's the right way to Unit test Android Library Project?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

草莓味的萝莉 2024-09-23 06:46:45

引用文档

“有两种推荐的设置方法对库项目中的代码和资源进行测试:

  • 您可以设置一个测试项目来检测依赖于库项目的应用程序项目,然后您可以将测试添加到项目中以获取特定于库的功能。

  • 您可以设置一个依赖于库的标准应用程序项目。将仪器放入该项目中。这使您可以创建一个包含测试/仪器和要测试的代码的独立项目。”

Quoting the documentation:

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

千里故人稀 2024-09-23 06:46:45

在您的测试项目中,只需更改包名称,使其与您的库的包相同。
例如,您有一个包为 "com.example.lib" 的库。创建一个针对您的库的测试项目。在清单文件中,您将看到 package="com.example.lib.test"targetPackage="com.example.lib"。只需将包从“com.example.lib.test”更改为“com.example.lib”(targetPackage 保持原样)。

另外,请确保该库在 Java 构建路径中引用您的测试项目,而是作为通常的 Android 库引用:在 Eclipse 中,它必须在 Project->Properties 中显示为库->Android 选项卡,但不在 Project->Properties->Java Build Path 选项卡中。

然后运行测试。

In your test project simply change the package name so that it's the same as your library's package.
For example, you have a library whose package is "com.example.lib". Create a test project targeting your library. In the manifest file you'll see package="com.example.lib.test", and targetPackage="com.example.lib". Just change the package from "com.example.lib.test" to "com.example.lib" (targetPackage leave as is).

Also, make sure that the library is referenced to your test project NOT in Java build path, but as a usual Android library : in Eclipse it must be shown as library in Project->Properties->Android tab, but not in Project->Properties->Java Build Path tab.

Then run you tests.

一世旳自豪 2024-09-23 06:46:45

根据 文档

测试库模块与测试应用程序相同。
主要区别在于库及其依赖项会自动包含为测试 APK 的依赖项。这意味着测试 APK 不仅包含其自己的代码,还包含库的 AAR 及其所有依赖项。由于没有单独的“测试中的应用程序”,因此 androidTest 任务仅安装(和卸载)测试 APK。
合并多个清单文件时,Gradle 遵循默认优先级顺序,并将库的清单合并到测试 APK 的主清单中。

Per the documentation:

Testing a library module is the same as testing an app.
The main difference is that the library and its dependencies are automatically included as dependencies of the test APK. This means that the test APK includes not only its own code, but also the library's AAR and all its dependencies. Because there is no separate "app under test," the androidTest task installs (and uninstalls) only the test APK.
When merging multiple manifest files, Gradle follows the default priority order and merges the library's manifest into the test APK's main manifest.

白衬杉格子梦 2024-09-23 06:46:45

注意:该解决方案基于使用 Eclipse Indigo (3.8.2),对于其他 IDE,其实现方式可能略有不同,但基本原理是相同的。

我遇到了类似的问题,我发现执行以下操作总是有效的:(

注意:这些说明用于从头开始构建新的项目组。如果您已经构建了项目组的一部分,那么您可能已经修改您的项目,以便它们以相同的方式连接。

  1. 通过在创建过程中选中“Is Library”复选框来创建一个新的 Android 库项目。 (例如名为“RemingtonAndroidTools”的 Android 项目)。
  2. 构建 Android Library 项目并验证它是否在 bin 文件夹中创建了一个 jar 文件。 (例如,名为“RemingtonAndroidTools.jar”的 jar 文件。)
  3. 创建一个空的 Android 项目来测试将用作 Android 测试应用程序的 Android 应用程序。 (例如名为“RemingtonAndroidToolsTestApp”的 Android 项目)。您不需要修改 Android 测试应用程序项目的源代码或资源,除非您必须添加一些内容来进行测试。无需对 Android 测试应用程序项目进行任何修改即可测试许多内容。 Android 测试应用程序项目是 Android 库项目和 Android Junit 项目之间的桥梁,它使通过 Android Junit 测试 Android 库项目成为可能。
  4. 转到 Android 测试的 Java 构建路径的“库”选项卡应用程序项目(本例中为“RemingtonAndroidToolsTestApp”)。
  5. 通过“Add Jars...”按钮添加 Android 库项目(本例中为“RemingtonAndroidTools”)的 jar 文件(本例中为“RemingtonAndroidTools.jar”)。
  6. 创建一个新的 Android 测试项目(例如“RemingtonAndroidToolsTester”),将其用作 Android 库测试器,并选择 Android 测试应用程序项目(本例中为“RemingtonAndroidToolsTestApp”)作为目标。
  7. 转到 Android Library Tester 项目(本例中为“RemingtonAndroidToolsTester”)的 Java 构建路径的“Library”选项卡。
  8. 通过“Add Jars...”按钮添加 Android 库项目(本例中为“RemingtonAndroidTools”)的 jar 文件(本例中为“RemingtonAndroidTools.jar”)。
  9. 在 Android Library Tester 项目中找到 Android 包的最后一个文件夹(例如“danny.remington.remington_android_tools_test_app.test”),并添加一个继承自 ActivityInstrumentationTestCase2 的测试类(例如“MainActivityTest”)。
  10. 编辑测试类(本例中为“TestActivityTest”)以使用 Android 测试应用程序(本例中为“RemingtonAndroidToolsTestApp”)的活动(例如“TestActivity”)作为 ActivityInstrumentationTestCase2 的参数。
  11. 编辑测试类(本例中为“TestActivityTest”)并创建一个默认构造函数,该构造函数调用 super(Class) 并传入 Android 测试应用程序的类(例如“TestActivity.class”)。

您最终应该得到三个与此类似的项目(Android Library、Android Test App、Android Library Tester):

enter image description此处

在此处输入图像描述

在此处输入图像描述

您最终应该得到一个用于测试 Android 库的类,该类看起来与此类似:

package danny.remington.remington_android_tools_test_app.test;

import android.test.ActivityInstrumentationTestCase2;
import danny.remington.remington_android_tools_test_app.TestActivity;

/**
 * 
 */
public class TestActivityTest extends
      ActivityInstrumentationTestCase2<TestActivity> {

   public TestActivityTest() {
      super(TestActivity.class);
   }

}

然后您可以添加任何您想要的测试。您不需要进一步引用 Android 测试应用程序(本例中为“RemingtonAndroidToolsTestApp”)来运行测试,除非它们需要访问 Android 特定组件(例如 Assets 文件夹)。如果您需要访问任何 Android 特定组件,您可以通过修改 Android 测试应用程序(本例中为“RemingtonAndroidToolsTestApp”),然后通过标准 Android Junit API 提供的工具引用它来实现。 (您可以在这里阅读更多相关信息:http://developer.android.com/tools /testing/testing_android.html

NOTE: This solution is based on using Eclipse Indigo (3.8.2) and might have to be implemented slightly differently for another IDE although the basic principles will be the same.

I had similar issues and I found that do the following always works:

(NOTE: These instructions are for building a new project group from scratch. If you have already built parts of the project group, then you may have to modify your projects so that they connect in the same way.)

  1. Create a new Android Library project by checking the "Is Library" checkbox during creation. (for example an Android project named "RemingtonAndroidTools").
  2. Build the Android Library project and verify that it created a jar file in the bin folder. (for example a jar file named "RemingtonAndroidTools.jar".)
  3. Create an empty Android Project for testing the Android app that will serve as an Android Test App. (For example an Android project named "RemingtonAndroidToolsTestApp"). You will not need to modify the source code or resources of the Android Test App project unless you have something that must be added for testing. Many things can be tested without any modifications to the Android Test App Project. The Android Test App project is a bridge between your Android Library project and the Android Junit project that makes testing of the Android Library project via Android Junit possible.
  4. Go the Library tab of Java Build Path for the Android Test App project ("RemingtonAndroidToolsTestApp" in this example).
  5. Add the jar file ("RemingtonAndroidTools.jar" in this example) of the Android Library Project ("RemingtonAndroidTools" in this example) via the "Add Jars..." button.
  6. Create a new Android Test project (for example "RemingtonAndroidToolsTester") that will serve as an Android Library Tester and select the Android Test App project ("RemingtonAndroidToolsTestApp" in this example) as the target.
  7. Go the Library tab of Java Build Path for the Android Library Tester project ("RemingtonAndroidToolsTester" in this example).
  8. Add the jar file ("RemingtonAndroidTools.jar" in this example) of the Android Library Project ("RemingtonAndroidTools" in this example) via the "Add Jars..." button.
  9. Find the last folder of your Android package in the Android Library Tester project ("danny.remington.remington_android_tools_test_app.test" for example) and add a test class ("MainActivityTest" for example) that inherits from ActivityInstrumentationTestCase2.
  10. Edit the test class ("TestActivityTest" in this example) to use the activity (for example "TestActivity") of the Android Test App ("RemingtonAndroidToolsTestApp" in this example) as the parameter for ActivityInstrumentationTestCase2.
  11. Edit the test class ("TestActivityTest" in this example) and create a default constructor that makes a call to super(Class) and passing in the class of the Android Test App ("TestActivity.class" for example).

You should end up with three projects (Android Library, Android Test App, Android Library Tester) that look similar to this:

enter image description here

enter image description here

enter image description here

You should end up with a class for testing your Android Library that looks similar to this:

package danny.remington.remington_android_tools_test_app.test;

import android.test.ActivityInstrumentationTestCase2;
import danny.remington.remington_android_tools_test_app.TestActivity;

/**
 * 
 */
public class TestActivityTest extends
      ActivityInstrumentationTestCase2<TestActivity> {

   public TestActivityTest() {
      super(TestActivity.class);
   }

}

You can then add any test that you want. You will not need to reference the Android Test App ("RemingtonAndroidToolsTestApp" in this example) further to run your tests unless they require access to an Android specific component (like the Assets folder, for example). If you need to access any Android specific components you can do so by modifying the Android Test App ("RemingtonAndroidToolsTestApp" in this example) and then referencing it via the instrumentation provided by the standard Android Junit API. (You can read more about that here: http://developer.android.com/tools/testing/testing_android.html)

得不到的就毁灭 2024-09-23 06:46:45

如果您的实用类不依赖于任何 Android 特定代码,您可以只使用标准 JUnit 单元测试。无需使用 Android 版本。

If your ulitiy classes do not depend on any android specific code, you can just use standard JUnit unit tests. No need to use the Android versions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文