Android 自动化测试解决方案

huangxl89 贡献于2012-08-19

作者 Huangxl  创建于2012-07-20 04:29:00   修改者Huangxl  修改于2012-07-20 04:30:00字数7320

文档摘要: Android SDK自带一个测试工具MonkeyRunner,它提供的API和执行环境可以运行Python语言编写的测试代码。它提供API来连接设备,安装/卸载应用,运行应用,截屏,比对图片来判断特定命令执行后的屏幕是否包含预期信息,以及运行对应用的测试。
关键词:

Android自动化测试解决方案   现在,已经有大量的Android自动化测试架构或工具可供我们使用,其中包括:Activity Instrumentation, MonkeyRunner, Robotium, 以及Robolectric。另外LessPainful也提供服务来进行真实设备上的自动化测试。   Android自身提供了对instrumentation测试的基本支持,其中之一就是位于android.test包内的ActivityInstrumentationTestCase2类,它扩展了JUnit的TestCase类来提供Android activities的功能测试。在应用测试中,每一个activity首先会被Instrumentation初始化,然后再加载到Android模拟器或设备的Dalvik虚拟机中来执行。   Android SDK自带一个测试工具MonkeyRunner,它提供的API和执行环境可以运行Python语言编写的测试代码。它提供API来连接设备,安装/卸载应用,运行应用,截屏,比对图片来判断特定命令执行后的屏幕是否包含预期信息,以及运行对应用的测试。MonkeyRunner使用ActivityInstrumentationTestCase2, ProviderTestCase,ServiceTestCasek, SingleLaunchActivityTestCase及其他类来定义测试用例,并使用InstrumentationTestRunner类来运行测试。   Robotium是另一种通过InstrumentationTestRunner来完成Android交互式测试的架构,它横跨多个activities,支持功能测试,系统测试和接收测试。Robotium支持Activities、Dialogs、Toasts、Menus、Context Menus甚至Honeycomb,并且它可以同Maven和Ant集成来完成持续集成测试。Robotium被称之为针对Android应用的又一个Selenium。   Robolectric另辟蹊径,它并不依赖于Android提供的测试功能,它使用了shadow objects并且运行测试于普通的工作站/服务器JVM,不像模拟器或设备需要dexing(Android dex编译器将类文件编译成Android设备上的Dalvik VM使用的格式),打包,部署和运行的过程,大大减少了测试执行的时间。Pivotal实验室声称使用Robolectric可以在28秒内运行1047个测试。   LessPainful将Android测试又推进了一步,它提供了一个多设备平台自动化测试的服务。用户上传应用(*.apk)和用Cucumber(一种业务相关的DSL)编写的测试文件,选择测试运行需要的设备配置,最后测试将自动执行并生成测试报告。它支持的设备包括Garmin Asus,几款HTC,LG,Samsung Galaxy,Sony Xperia和Motorola Motodefy。   为了了解更多LessPainful提供的服务细节,我们采访了LessPainful公司的CEO Jonas Maturana Larsen。下面就是这次简短的访问:   InfoQ:在不同版本的Android上运行应用程序,存在什么问题?为了保证程序能正常运行,开发者需要在Android的每一个版本上测试他的应用吗?   JML:举个例子,SAXParser在Android 2.2之前有一个bug存在于对ContentHandler.startElement的回调中,它导致应用产生错误的行为。   到目前为止,我们已经在很多方面发现了不同操作系统版本间的差异性。其中一些可能在2.1-update1上导致崩溃,但可以正常运行于2.1-update3和2.2.   InfoQ:不同的设备对Android来说,有没有真正的区别?你能否给我们举个例子,比如Android2.2应用可以运行在HTC但不能运行于Samsung?(或其他各种Android版本和设备制造商的组合)   JML:在LG手机,HorizontalScrollViews有时会导致子视图上的背景图片消失。这个问题存在于我们测试的所有的LG手机,不管Android版本是多少。   如果你不自己处理这类问题,它将导致你的应用在不同设备上不尽相同。例如,Motorola将会用红色边框来高亮一个输入域。在我曾经参与的一个项目中,我们用同样的红色边框来表示输入有误。   还有一些问题,与其说和制造商相关,不如说是和硬件相关:比如,一些手机使用了较小的RAM和高分辨率的摄像头,当你处理手机上的图像时就会将导致崩溃。   InfoQ:这些测试是如何执行的?   JML:测试就如同运行一个ActivityInstrumentationTestCase2,主要使用Robotium来运行。我们对应用所做的唯一修改就是去掉已有的签名,再为它重新生成我们的签名文件。   在测试运行完成后,应用会被卸载,而手机也会被恢复到初始设置。   InfoQ:与MonkeyRunner,Robotium和Robolectric相比,你们所提供的服务有什么优势呢?   JML:LessPainful是一种服务,而并不仅仅是一种架构。我们希望创建一种服务,不但使测试能够进行,并且比起其他任何一种架构,它能够节省我们大量测试时间,还能够帮助我们发现更多的bug。   另外,我们相信使用Cucumber,可以清晰地定义高层次测试描述,同时可以更好地被开发团队以外的人员共享。   以Git领域为例,我们更希望成为像是GitHub那样,而不只是通常的git库。   InfoQ:你们有计划未来要支持更多的设备吗?   JML:是的。我们计划继续增加对更多设备的支持。如果有这样的要求提出,我们就会努力完成它。   目前,我们也在着手于对iOS设备的支持,希望beta版能在今年秋季发布。   InfoQ:什么是LessPainful企业版?   JML:我们将提供一个工具集,它就类似于一个Mac Mini,但我们会非常灵活的满足顾客的需求。LessPainful企业版目前还没有正式推出,所以敬请期待。 Automated Testing Solutions for Android Posted by Abel Avram on Sep 30, 2011 There are a number of automated testing frameworks or tools for Android applications, including but not limited to Activity Instrumentation, MonkeyRunner, Robotium, or Robolectric. LessPainful is providing automated tested on real devices as a service. Android has basic support for instrumentation tests, one of the possibilities being the ActivityInstrumentationTestCase2 class belonging to the android.test package and extending JUnit’s TestCase in order to provide functional testing of Android activities. When an application is tested, each instrumented activity is launched inside the Dalvik VM on an Android emulator or an actual device. The Android SDK comes with a testing tool called MonkeyRunner, providing an API and an execution environment for running tests written in Python. The tool has APIs for connecting to a device, installing/uninstalling apps, running apps, taking screenshots, comparing images to see if the screen contains what is supposed to contain after certain commands have been performed, and to run a test package against an application. MonkeyRunner uses the InstrumentationTestRunner class to run tests by defining test cases with ActivityInstrumentationTestCase2, ProviderTestCase, ServiceTestCase, SingleLaunchActivityTestCase, and others. Robotium is yet another framework that interacts with Android’s testing support via the InstrumentationTestRunner, providing functional, system and acceptance tests running across multiple activities. Robotium supports Activities, Dialogs, Toasts, Menus and Context Menus even for Honeycomb, and it is integrated with Maven and Ant for running continuous integration tests. Robotium is similar to Selenium, but for Android applications. Robolectric has taken a different path. Instead on relying on Android’s testing functionality provided, this testing framework uses shadow objects and runs the tests on a regular workstation/server JVM avoiding dexing, packaging, deploying and running the application tested on an emulator or a real device, reducing the time needed to perform tests. Pivotal Labs claims to be able to run 1,047 tests in 28 seconds with Robolectric. LessPainful has taken Android testing a step further, offering a service for automated testing of applications on multiple real devices. The user uploads the application (*.apk) and a test file written in Cucumber, a business-readable DSL, selects the configuration of devices he wants the tests to run on, and then the tests are automatically executed and a report is generated. The list of devices supported include Garmin Asus, several HTCs, LGs, Samsung Galaxy, Sony Xperia, and Motorola Motodefy. We have talked to Jonas Maturana Larsen, CEO of LessPainful, to find out more details about the service. Following is a short interview: InfoQ: What problems there are running apps on different versions of Android? Does a developer need to test his app on every version of Android to make sure that it runs correctly? JML: One example: The SAXParser on Android before 2.2 has a bug in the callback to ContentHandler.startElement this causes the app to misbehave. In general we have seen quite a few differences between OS versions. Something would crash on 2.1-update1 but work on 2.1-update3 and 2.2. InfoQ: Is there a real Android fragmentation among different devices? Can you give an example of an application that runs on Android 2.2 (for example) on HTC and does not run on a Samsung? (Use any combination of Android version and device manufacturers.) JML: On LG phones HorizontalScrollViews will sometimes cause background images on child views to disappear. It occurs on all the LG phones we have tested and across different OS versions. The themes will cause your app to look different on different devices if you don't handle it yourself. Motorola will, for example, highlight an input field with a red border. I have worked on a project where we'd use exactly the same red border for show input errors. Other problems are more hardware related than manufacturer related: Some phones with little RAM and a high resolution camera will crash if you try to process an image on the phone. InfoQ: How are tests executed? JML: The test is basically run as an ActivityInstrumentationTestCase2 and using Robotium. The only modification we make to the application is to strip the signature and resign it. After the test has run the app is uninstalled and the phone settings are brought back to default. InfoQ: What are the advantages of your service compared to MonkeyRunner, Robotium or Robolectric? JML: LessPainful is a service not just a framework. We want to build a service that not only makes it possible to test but does so in a way that saves people massive amounts of time and helps them find more errors than they would if they were using one of the frameworks. Another thing is that we believe that using Cucumber results in nice high level test specifications that can be shared outside the dev team. You might say that we want to be more like github is to git than some git library is to git. InfoQ: Do you plan on supporting more devices in the future? JML: Yes. We plan to continually add support for more devices. If somebody asks for a device we try to get it. We are currently working on supporting iOS devices too. We expect it to be ready for public beta during the fall. InfoQ: What is LessPainful Enterprise? JML: We will provide a box. Most likely it will be a Mac Mini but we are very flexible when it comes to customer needs. LessPainful Enterprise is currently not an off-the-shelf product so it really comes down to what the customer is looking for.

下载文档到电脑,查找使用更方便

文档的实际排版效果,会与网站的显示效果略有不同!!

需要 3 金币 [ 分享文档获得金币 ] 1 人已下载

下载文档