前一段時間一直在看iOS測試相關的知識以及測試框架、測試工具的使用,現在拿來總結以下。
GHUnit 是iOS單元測試的一個開源框架,它與XCode集成的OCUnit測試框架有着很大的不同。OCUnit測試框架是集成在你的項目目錄中的,而GHUnit測試框架是單獨存在的一個target,可以獨立運行,並且有UI界面;另外GHUnit是開源的,也意味着你可以根據測試計劃自定義框架功能,提高你的測試效率。
那麼首先我們先從GHUnit測試框架的配置入手:參考官方文檔:http://gh-unit.github.io/gh-unit/docs/appledoc_include/guide_install_ios_7.html,官方文檔中的開發環境是Xcode 5,現在已經更新的Xcode7,但是基本的配置步驟是沒有太大的區別。
Installing in iOS (Xcode 5)
To use GHUnit in your project, you’ll need to create and configure a test target.
Create Test Target
- You’ll want to create a separate Test target. Select the project file for your app in the Project Navigator. From there, select the “Add Target…” button in the right window.
- Select iOS, Application, Empty Application. Select Next.
- Name it Tests or something similar. Select Finish.
Configure the Test Target
- Select the created Test target and select the tab “Build Phases”. Open the “Link Binary With Libraries” option and click on the “+” button.
-
Select
QuartzCore.framework
and click “Add”. -
Download GHUnitIOS.framework and unzip it in your Test Target directory (a subdirectory of your project directory).
-
Select the created Test target and select the tab “Build Phases”. Open the “Link Binary With Libraries” option and click on the “+” button.
- Click “Add Other…”.
-
Select the
GHUnitIOS.framework
from your Test Target directory. -
We want to enable use of Objective-C categories, which isn’t enabled for static libraries by default. In the Tests target, Build Settings, under Other Linker Flags, add
-ObjC
.
- Select and delete the files from the existing Tests folder. Leave the Supporting Files folder. GHUnit will provide the application delegate below.
- In Tests folder, in Supporting Files, main.m, replace the last argument of UIApplicationMain with
@"GHUnitIOSAppDelegate"
. Remove the#import "AppDelegate.h"
if present.
- Select the Tests target, iPhone Simulator configuration:
- Hit Run, and you’ll hopefully see the test application running (but without any tests).