如何测试 Android 库项目
我正在编写一个基于 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
引用文档:
“有两种推荐的设置方法对库项目中的代码和资源进行测试:
您可以设置一个测试项目来检测依赖于库项目的应用程序项目,然后您可以将测试添加到项目中以获取特定于库的功能。
您可以设置一个依赖于库的标准应用程序项目。将仪器放入该项目中。这使您可以创建一个包含测试/仪器和要测试的代码的独立项目。”
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."
在您的测试项目中,只需更改包名称,使其与您的库的包相同。
例如,您有一个包为
"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 seepackage="com.example.lib.test"
, andtargetPackage="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 inProject->Properties->Java Build Path
tab.Then run you tests.
根据 文档:
测试库模块与测试应用程序相同。
主要区别在于库及其依赖项会自动包含为测试 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.
注意:该解决方案基于使用 Eclipse Indigo (3.8.2),对于其他 IDE,其实现方式可能略有不同,但基本原理是相同的。
我遇到了类似的问题,我发现执行以下操作总是有效的:(
注意:这些说明用于从头开始构建新的项目组。如果您已经构建了项目组的一部分,那么您可能已经修改您的项目,以便它们以相同的方式连接。)
您最终应该得到三个与此类似的项目(Android Library、Android Test App、Android Library Tester):
您最终应该得到一个用于测试 Android 库的类,该类看起来与此类似:
然后您可以添加任何您想要的测试。您不需要进一步引用 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.)
You should end up with three projects (Android Library, Android Test App, Android Library Tester) that look similar to this:
You should end up with a class for testing your Android Library that looks similar to this:
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 特定代码,您可以只使用标准 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.