最簡單的單元測試
1.新建項目:
2.最簡單的測試,注意截圖路徑的問題
進入到這個類,setUp是每個測試方法調用前執行,tearDown是每個測試方法調用后執行。testExample是測試方法,和我們新建的沒有差別。不過測試方法必須testXXX的格式,且不能有參數,不然不會識別為測試方法。測試方法的執行順序是字典序排序。
按快捷鍵Command + U進行單元測試,這個快捷鍵是全部測試。
testExample方法中輸入
NSLog(@"自定義測試testExample");
int a= 3;
XCTAssertTrue(a == 0,"a 不能等于 0");
點擊播放按鈕,開始單個方法的測試:
出現如下結果,由于我們斷言a是等于0的,而a等于3,所以測試沒有通過。當然有其它的,用到再看,demo里都有。
進行網絡請求的測試
使用CocoaPods安裝AFNetworking和STAlertView(CocoaPods安裝和使用教程 )
Pofile:
platform :ios, '7.0'
target 'UnitTestDemoTests' do
pod 'AFNetworking', '~> 2.5.0'
pod 'STAlertView', '~> 1.0.0'
end
target 'UnitTestDemoTestsTests' do
pod 'AFNetworking', '~> 2.5.0'
pod 'STAlertView', '~> 1.0.0'
end
iOS9的http安全問題:現在進行異步請求的網絡測試,由于測試方法主線程執行完就會結束,所以需要設置一下,否則沒法查看異步返回結果。在方法結束前設置等待,調回回來的時候再讓它繼續執行。(另一種異步函數的單元測試)定義宏如下:
//waitForExpectationsWithTimeout是等待時間,超過了就不再等待往下執行。
#define WAIT do {\
[self expectationForNotification:@"RSBaseTest" object:nil handler:nil];\
[self waitForExpectationsWithTimeout:30 handler:nil];\
} while (0);
#define NOTIFY \
[[NSNotificationCenter defaultCenter]postNotificationName:@"RSBaseTest" object:nil];
增加測試方法testRequest:
-(void)testRequest{
// 1.獲得請求管理者
AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
mgr.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",nil];
// 2.發送GET請求
[mgr GET:@"http://www.weather.com.cn/adat/sk/101110101.html" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"responseObject:%@",responseObject);
XCTAssertNotNil(responseObject, @"返回出錯");
NOTIFY //繼續執行
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"error:%@",error);
XCTAssertNil(error, @"請求出錯");
NOTIFY //繼續執行
}];
WAIT //暫停
}
有時候我們想測試一下整個流程是否可以跑通,比如獲取驗證碼、登錄、上傳頭像,查詢個人資料。其實只要輸入驗證碼就可以完成整個測試。這時候就需要用到輸入框了,以便程序繼續執行。使用了一個第三方的彈出輸入框STAlertView,前面已經設置。
STAlertView的使用方法:
self.stAlertView = [[STAlertView alloc]initWithTitle:@"驗證碼" message:nil textFieldHint:@"請輸入手機驗證碼" textFieldValue:nil cancelButtonTitle:@"取消" otherButtonTitle:@"確定" cancelButtonBlock:^{
//點擊取消返回后執行
[self testAlertViewCancel];
NOTIFY //繼續執行
} otherButtonBlock:^(NSString *b) {
//點擊確定后執行
[self alertViewComfirm:b];
NOTIFY //繼續執行
}];
[self.stAlertView show];
3.性能測試
性能測試主要使用 measureBlock 方法 ,用于測試一組方法的執行時間,通過設置baseline(基準)和stddev(標準偏差)來判斷方法是否能通過性能測試。
- (void)testPerformanceExample {
// This is an example of a performance test case.
[self measureBlock:^{
//Put the code you want to measure the time of here.
//你的性能測試的代碼放在這里
}];
}
4.命令行測試
測試不僅可以在xcode中執行,也可以在命令行中執行,這個便于代碼持續集成和構建,在git提交中也編譯檢查代碼
如果你有development-enabled設備插入,你可以按照名稱或 id 調用他們。例如,如果你有一個名為”Development iPod touch”的 iPod 設備連接了測試的代碼,可以使用下面的命令來測試代碼
> xcodebuild test -project MyAppProject.xcodeproj -scheme MyApp -destination 'platform=iOS,name=Development iPod touch
測試也可以在 iOS模擬器上運行。使用模擬器可以應對不同的外形因素和操作系統版本。例如
> xcodebuild test -project MyAppProject.xcodeproj -scheme MyApp -destination 'platform=iOS Simulator,name=iPhone,0S=7.0'
-destination 參數可以被連接在一起,這樣你只需使用一個命令,就可以跨目標進行指定集成共享方案。例如,下面的命令把之前的三個例子合并到一個命令中
> xcodebuild test -project MyAppProject.xcodeproj -scheme MyApp
-destination 'platform=OS X,arch=x86_64'
-destination 'platform=iOS,name=Development iPod touch'
-destination 'platform=iOS Simulator,name=iPhone,0S=7.0'
斷言種類:
//通用斷言
XCTAssert(expression, format...)
//常用斷言:
XCTAssertTrue(expression, format...)
XCTAssertFalse(expression, format...)
XCTAssertEqual(expression1, expression2, format...)
XCTAssertNotEqual(expression1, expression2, format...)
XCTAssertEqualWithAccuracy(expression1, expression2, accuracy, format...)
XCTAssertNotEqualWithAccuracy(expression1, expression2, accuracy, format...)
XCTAssertNil(expression, format...)
XCTAssertNotNil(expression, format...)
XCTFail(format...) //直接Fail的斷言
注意點
都是血與淚的教訓
使用pod的項目中,在XC測試框架中測試內容包括第三方包時,需要手動去設置Header Search Paths才能找到頭文件 ,還需要設置test target的PODS_ROOT.
Enable Testability 設置為YES
xcode7要使用真機做跑測試時,證書必須配對,否則會報錯exc_breakpoint錯誤