Unit Test簡介
單元測試,通常一個單元指的就是應用程序中可以測試的最小單元,xCode5在創建工程的時候就會自帶一個單元測試的文件夾,IDE自動生成了一個實現XCTestCase的.m文件,里面有一個失敗測試(早期版本中實現的是SenTestCase,是蘋果集成的第三方的,現在蘋果建議使用新的XCTestCase)
Unit Test使用
1、 在創建工程的時候,會有是否創建測試文件夾的選項
2、 進入工程目錄,單元測試文件夾里,有默認創建好的測試文件
當然,我們也可以自己創建測試文件,xCode會自動把它放在同名的文件夾下
3、 如何進行測試呢?可以選擇運行測試文件,也可以選擇只運行某個測試用例即方法,當然實際運行過程中,單元測試是在主線程中進行的,也就是啟動app完成后進行
4、測試文件的內容
- 相關函數
- (void)setUp {
[super setUp];
// 測試函數執行前調用.
// In UI tests it is usually best to stop immediately when a failure occurs.
self.continueAfterFailure = NO;
// UI tests must launch the application that they test. Doing this in setup will make sure it happens for each test method.
[[[XCUIApplication alloc] init] launch];
// In UI tests it’s important to set the initial state - such as interface orientation - required for your tests before they run. The setUp method is a good place to do this.
}
- (void)tearDown {
// 測試函數執行后調用.
[super tearDown];
}
//測試函數
- (void)testExample {
// Use recording to get started writing UI tests.
// Use XCTAssert and related functions to verify your tests produce the correct results.
}
測試函數
1、測試函數的要求是:1.必須無返回值;2.以test開頭;
2、測試函數執行的順序:以函數名中test后面的字符大小有關,比如 -(void)test001XXX會先于-(void)test002XXX執行;XCTest框架中的斷言
XCTFail(format…) 生成一個失敗的測試;
XCTAssertNil(a1, format...) 為空判斷, a1 為空時通過,反之不通過;
XCTAssertNotNil(a1, format…) 不為空判斷,a1不為空時通過,反之不通過;
XCTAssert(expression, format...) 當expression求值為TRUE時通過;
XCTAssertTrue(expression, format...) 當expression求值為TRUE時通過;
XCTAssertFalse(expression, format...) 當expression求值為False時通過;
XCTAssertEqualObjects(a1, a2, format...) 判斷相等, [a1 isEqual:a2] 值為TRUE時通過,其中一個不為空時,不通過;
XCTAssertNotEqualObjects(a1, a2, format...) 判斷不等, [a1 isEqual:a2] 值為False時通過;
XCTAssertEqual(a1, a2, format...) 判斷相等(當a1和a2是 C語言標量、結構體或聯合體時使用,實際測試發現NSString也可以);
XCTAssertNotEqual(a1, a2, format...) 判斷不等(當a1和a2是 C語言標量、結構體或聯合體時使用);
XCTAssertEqualWithAccuracy(a1, a2, accuracy, format...) 判斷相等,(double或float類型)提供一個誤差范圍,當在誤差范圍(+/- accuracy )以內相等時通過測試;
XCTAssertNotEqualWithAccuracy(a1, a2, accuracy, format...) 判斷不等,(double或float類型)提供一個誤差范圍,當在誤差范圍以內不等時通過測試;
XCTAssertThrows(expression, format...) 異常測試,當expression發生異常時通過;反之不通過;(很變態)
XCTAssertThrowsSpecific(expression, specificException, format...) 異常測試,當expression發生 specificException 異常時通過;反之發生其他異?;虿话l生異常均不通過;
XCTAssertThrowsSpecificNamed(expression, specificException, exception_name, format...) 異常測試,當expression發生具體異常、具體異常名稱的異常時通過測試,反之不通過;
XCTAssertNoThrow(expression, format…) 異常測試,當expression沒有發生異常時通過測試;
XCTAssertNoThrowSpecific(expression, specificException, format...)異常測試,當expression沒有發生具體異常、具體異常名稱的異常時通過測試,反之不通過;
XCTAssertNoThrowSpecificNamed(expression, specificException, exception_name, format...) 異常測試,當expression沒有發生具體異常、具體異常名稱的異常時通過測試,反之不通過
進階
- 單元測試涉及到真實開發,會需要很多第三方框架,如AFNetworking,這個時候單元測試的target沒有這些庫,所以要配置一下
另外,比如要進行網絡請求,https的問題,在Info.plist中添加NSAppTransportSecurity類型Dictionary。 在NSAppTransportSecurity下添加NSAllowsArbitraryLoads類型Boolean,值設為YES。
總之,要測試實際工程里的功能,就需要和工程target同樣的環境和資源,需要什么找什么就好了
- iOS9的http安全問題:現在進行異步請求的網絡測試,由于測試方法主線程執行完就會結束,所以需要設置一下,否則沒法查看異步返回結果。在方法結束前設置等待,調回回來的時候再讓它繼續執行。
#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];
使用
-(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 //暫停
}