如何测试 Android 库项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3603657/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 10:55:58  来源:igfitidea点击:

How to test an Android Library Project

androidjunitapk

提问by Lily

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

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

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

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

采纳答案by CommonsWare

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

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

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

回答by alexaschka

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" (targetPackageleave as is).

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

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

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

Then run you tests.

然后运行你的测试。

回答by yuzisee

http://www.paulbutcher.com/2010/09/android-library-project-with-tests-step-by-step/helps describe the process needed to implement the second suggestion in CommonsWare's answer

http://www.paulbutcher.com/2010/09/android-library-project-with-tests-step-by-step/有助于描述在 CommonsWare 的回答中实施第二个建议所需的过程

回答by Avilio

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.

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

回答by Danny Remington - OMS

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.

注意:此解决方案基于使用 Eclipse Indigo (3.8.2),虽然基本原理是相同的,但对于另一个 IDE,可能需要稍微不同地实现。

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).
  1. 通过在创建过程中选中“Is Library”复选框来创建一个新的 Android Library 项目。(例如名为“RemingtonAndroidTools”的 Android 项目)。
  2. 构建 Android 库项目并验证它是否在 bin 文件夹中创建了一个 jar 文件。(例如名为“RemingtonAndroidTools.jar”的 jar 文件。)
  3. 创建一个空的 Android 项目,用于测试将用作 Android 测试应用程序的 Android 应用程序。(例如名为“RemingtonAndroidToolsTestApp”的 Android 项目)。您不需要修改 Android Test App 项目的源代码或资源,除非您有一些必须添加以进行测试的内容。无需对 Android 测试应用程序项目进行任何修改即可测试许多内容。 Android 测试应用程序项目是您的 Android 库项目和 Android Junit 项目之间的桥梁,它使通过 Android Junit 测试 Android 库项目成为可能。
  4. 转到 Android Test App 项目的 Java Build Path 的 Library 选项卡(在本例中为“RemingtonAndroidToolsTestApp”)。
  5. 通过“添加罐子...”按钮添加 Android 库项目(本例中为“RemingtonAndroidTools”)的 jar 文件(本例中为“RemingtonAndroidTools.jar”)。
  6. 创建一个用作 Android 库测试器的新 Android 测试项目(例如“RemingtonAndroidToolsTester”),并选择 Android 测试应用项目(在本例中为“RemingtonAndroidToolsTestApp”)作为目标。
  7. 转到 Android Library Tester 项目的 Java Build Path 的 Library 选项卡(本例中为“RemingtonAndroidToolsTester”)。
  8. 通过“添加罐子...”按钮添加 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”)。

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

您应该最终得到三个与此类似的项目(Android 库、Android 测试应用程序、Android 库测试器):

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:

你应该最终得到一个类来测试你的 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);
   }

}

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)

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

回答by Cheryl Simon

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.

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