使用UIAutomation框架的iOS自动化测试

为了保证iOS应用的质量,需要遵循测试驱动的开发流程,如下图所示。

Your First iOS test program using UIAutomation framework
Your First iOS test program using UIAutomation framework

测试驱动开发(TDD)是一种应用于iOS应用测试的测试模型。在此模型中,测试仪必须遵循以下4个阶段:

  • 设计:找出想要测试的内容,设计测试用例
  • 测试:运行所有测试并查看测试用例是否失败
  • 实现:修改代码,修复导致测试失败的错误
  • 再次测试:如果失败,则回滚到设计。如果所有测试用例都通过,则代码满足整个测试要求。

设置测试环境

要创建IOS测试程序,需要一本Mac Book。Mac已安装:

  • OS X.Mac PC操作系统
  • Xcode IDE,iOS开发工具
  • 自动化测试框架(UIAutomation、OCUnit.)
  • iOS SDK 4(或更高版本)

使用UIAutomation框架创建iOS测试程序。

步骤1) 启动仪器

打开XCode->Open Developer Tool->Instrument

Your First iOS test program using UIAutomation framework
Your First iOS test program using UIAutomation framework

步骤2) 添加自动化功能

在“Instruments”窗口中,选择“Automation Instrument

Your First iOS test program using UIAutomation framework
Your First iOS test program using UIAutomation framework

要创建测试脚本,可以记录测试场景,也可以手动对其进行编程。

步骤3) 仪器正在启动,立即停止录制。如果要开始录制,按红色按钮

Your First iOS test program using UIAutomation framework
Your First iOS test program using UIAutomation framework

步骤4) 在Scripts窗口中,单击Add>Create以创建新脚本

Your First iOS test program using UIAutomation framework
Your First iOS test program using UIAutomation framework

步骤5) 选择目标

在跟踪窗口中,使用Choose Target下拉菜单导航到应用程序的调试版本。

Your First iOS test program using UIAutomation framework
Your First iOS test program using UIAutomation framework

在本例中,我将使用Apple的示例SimpleDrillDown Simple应用程序作为测试中的应用程序。它下面有GUI。

Your First iOS test program using UIAutomation framework
Your First iOS test program using UIAutomation framework

步骤6) 开始录制脚本

通过单击工具顶部或底部的录制按钮录制脚本。

Your First iOS test program using UIAutomation framework
Your First iOS test program using UIAutomation framework

现在,可以对测试中的应用程序执行一些UI操作,并且脚本已被记录下来。

步骤7) 查看脚本

要查看脚本,单击跟踪日志/编辑器日志下拉菜单,然后切换到脚本日志视图。

Your First iOS test program using UIAutomation framework
Your First iOS test program using UIAutomation framework

将看到录制的脚本。

Your First iOS test program using UIAutomation framework
Your First iOS test program using UIAutomation framework

步骤8) 播放脚本

按PLAY按钮,脚本运行,出现日志后即可停止。

Your First iOS test program using UIAutomation framework
Your First iOS test program using UIAutomation framework

使用OCUnit框架创建IOS测试程序

步骤1) 启动Xcode IDE,添加单元测试包目标

Your First iOS test program using UIAutomation framework
Your First iOS test program using UIAutomation framework

步骤2) 如上图所示编写新的单元测试包的名称,然后单击Finish

步骤3) 使单元测试成为活动目标

Your First iOS test program using UIAutomation framework
Your First iOS test program using UIAutomation framework

步骤4) 添加测试类组

Your First iOS test program using UIAutomation framework
Your First iOS test program using UIAutomation framework

步骤5) 添加单元测试类

Your First iOS test program using UIAutomation framework
Your First iOS test program using UIAutomation framework

步骤6) 现在开始工具

Your First iOS test program using UIAutomation framework
Your First iOS test program using UIAutomation framework

OCUnit使用Objective-C语言创建测试程序。开发人员必须了解这种语言。

IT赶路人

专注IT知识分享