如何测试Android库项目

时间:2023-01-27 10:39:44

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库项目的正确方法是什么?

6 个解决方案

#1


64  

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

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

#2


17  

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

在您的测试项目中,只需更改包名称,使其与库的包相同。例如,您有一个库,其包是“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 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.

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

Then run you tests.

然后运行测试。

#3


13  

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的回答中实现第二个建议所需的过程

#4


1  

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的主清单中。

#5


0  

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版本。

#6


0  

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. 通过在创建期间选中“Is Library”复选框来创建新的Android库项目。 (例如名为“RemingtonAndroidTools”的Android项目)。
  3. 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".)
  4. 构建Android Library项目并验证它是否在bin文件夹中创建了一个jar文件。 (例如名为“RemingtonAndroidTools.jar”的jar文件。)
  5. 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.
  6. 创建一个空的Android项目,用于测试将作为Android测试应用的Android应用。 (例如,名为“RemingtonAndroidToolsTestApp”的Android项目)。除非您必须添加某些内容以进行测试,否则您无需修改​​Android Test App项目的源代码或资源。可以在不对Android Test App Project进行任何修改的情况下测试许多内容。 Android Test App项目是您的Android库项目和Android Junit项目之间的桥梁,可以通过Android Junit测试Android库项目。
  7. Go the Library tab of Java Build Path for the Android Test App project ("RemingtonAndroidToolsTestApp" in this example).
  8. 转到Android Test App项目的Java Build Path的Library选项卡(本例中为“RemingtonAndroidToolsTestApp”)。
  9. Add the jar file ("RemingtonAndroidTools.jar" in this example) of the Android Library Project ("RemingtonAndroidTools" in this example) via the "Add Jars..." button.
  10. 通过“Add Jars ...”按钮添加Android库项目(本例中为“RemingtonAndroidTools”)的jar文件(本例中为“RemingtonAndroidTools.jar”)。
  11. 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.
  12. 创建一个新的Android Test项目(例如“RemingtonAndroidToolsTester”),它将作为Android Library Tester并选择Android Test App项目(在此示例中为“RemingtonAndroidToolsTestApp”)作为目标。
  13. Go the Library tab of Java Build Path for the Android Library Tester project ("RemingtonAndroidToolsTester" in this example).
  14. 转到Android Library Tester项目的Java Build Path的Library选项卡(本例中为“RemingtonAndroidToolsTester”)。
  15. Add the jar file ("RemingtonAndroidTools.jar" in this example) of the Android Library Project ("RemingtonAndroidTools" in this example) via the "Add Jars..." button.
  16. 通过“Add Jars ...”按钮添加Android库项目(本例中为“RemingtonAndroidTools”)的jar文件(本例中为“RemingtonAndroidTools.jar”)。
  17. 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.
  18. 在Android Library Tester项目中找到Android软件包的最后一个文件夹(例如“danny.remington.remington_android_tools_test_app.test”)并添加一个继承自ActivityInstrumentationTestCase2的测试类(例如“MainActivityTest”)。
  19. 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.
  20. 编辑测试类(在此示例中为“TestActivityTest”)以使用Android Test App的活动(例如“TestActivity”)(在此示例中为“RemingtonAndroidToolsTestApp”)作为ActivityInstrumentationTestCase2的参数。
  21. 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).
  22. 编辑测试类(在此示例中为“TestActivityTest”)并创建一个默认构造函数,该构造函数调用super(Class)并传入Android Test App的类(例如“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库测试程序),看起来类似于:

如何测试Android库项目

如何测试Android库项目

如何测试Android库项目

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特定组件(例如Assets文件夹),否则您不需要进一步引用Android测试应用程序(在此示例中为“RemingtonAndroidToolsTestApp”)来运行测试。如果您需要访问任何Android特定组件,您可以通过修改Android测试应用程序(在此示例中为“RemingtonAndroidToolsTestApp”),然后通过标准Android Junit API提供的工具来引用它。 (你可以在这里阅读更多相关内容:http://developer.android.com/tools/testing/testing_android.html)

#1


64  

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

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

#2


17  

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

在您的测试项目中,只需更改包名称,使其与库的包相同。例如,您有一个库,其包是“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 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.

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

Then run you tests.

然后运行测试。

#3


13  

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的回答中实现第二个建议所需的过程

#4


1  

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的主清单中。

#5


0  

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版本。

#6


0  

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. 通过在创建期间选中“Is Library”复选框来创建新的Android库项目。 (例如名为“RemingtonAndroidTools”的Android项目)。
  3. 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".)
  4. 构建Android Library项目并验证它是否在bin文件夹中创建了一个jar文件。 (例如名为“RemingtonAndroidTools.jar”的jar文件。)
  5. 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.
  6. 创建一个空的Android项目,用于测试将作为Android测试应用的Android应用。 (例如,名为“RemingtonAndroidToolsTestApp”的Android项目)。除非您必须添加某些内容以进行测试,否则您无需修改​​Android Test App项目的源代码或资源。可以在不对Android Test App Project进行任何修改的情况下测试许多内容。 Android Test App项目是您的Android库项目和Android Junit项目之间的桥梁,可以通过Android Junit测试Android库项目。
  7. Go the Library tab of Java Build Path for the Android Test App project ("RemingtonAndroidToolsTestApp" in this example).
  8. 转到Android Test App项目的Java Build Path的Library选项卡(本例中为“RemingtonAndroidToolsTestApp”)。
  9. Add the jar file ("RemingtonAndroidTools.jar" in this example) of the Android Library Project ("RemingtonAndroidTools" in this example) via the "Add Jars..." button.
  10. 通过“Add Jars ...”按钮添加Android库项目(本例中为“RemingtonAndroidTools”)的jar文件(本例中为“RemingtonAndroidTools.jar”)。
  11. 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.
  12. 创建一个新的Android Test项目(例如“RemingtonAndroidToolsTester”),它将作为Android Library Tester并选择Android Test App项目(在此示例中为“RemingtonAndroidToolsTestApp”)作为目标。
  13. Go the Library tab of Java Build Path for the Android Library Tester project ("RemingtonAndroidToolsTester" in this example).
  14. 转到Android Library Tester项目的Java Build Path的Library选项卡(本例中为“RemingtonAndroidToolsTester”)。
  15. Add the jar file ("RemingtonAndroidTools.jar" in this example) of the Android Library Project ("RemingtonAndroidTools" in this example) via the "Add Jars..." button.
  16. 通过“Add Jars ...”按钮添加Android库项目(本例中为“RemingtonAndroidTools”)的jar文件(本例中为“RemingtonAndroidTools.jar”)。
  17. 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.
  18. 在Android Library Tester项目中找到Android软件包的最后一个文件夹(例如“danny.remington.remington_android_tools_test_app.test”)并添加一个继承自ActivityInstrumentationTestCase2的测试类(例如“MainActivityTest”)。
  19. 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.
  20. 编辑测试类(在此示例中为“TestActivityTest”)以使用Android Test App的活动(例如“TestActivity”)(在此示例中为“RemingtonAndroidToolsTestApp”)作为ActivityInstrumentationTestCase2的参数。
  21. 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).
  22. 编辑测试类(在此示例中为“TestActivityTest”)并创建一个默认构造函数,该构造函数调用super(Class)并传入Android Test App的类(例如“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库测试程序),看起来类似于:

如何测试Android库项目

如何测试Android库项目

如何测试Android库项目

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特定组件(例如Assets文件夹),否则您不需要进一步引用Android测试应用程序(在此示例中为“RemingtonAndroidToolsTestApp”)来运行测试。如果您需要访问任何Android特定组件,您可以通过修改Android测试应用程序(在此示例中为“RemingtonAndroidToolsTestApp”),然后通过标准Android Junit API提供的工具来引用它。 (你可以在这里阅读更多相关内容:http://developer.android.com/tools/testing/testing_android.html)