![Uploading QQ20160129-3_262826.png . . .]####什麼是單元測試?
一聽到單元測試這個詞感受很高端,其實單元測試就是爲你的方法多專門寫一個測試函數。以保證你的方法在不停的修改開發中。保持正確。若是出錯,第一時間讓你知道,這樣從最小單位開始監控來保證軟件的質量。express
一、寫完代碼之後:想要驗證一下本身寫的代碼是否有問題。
二、寫代碼以前:就是寫代碼以前全部的功能分模塊的設計好,測試經過了再寫。(我反正是沒用過)。
三、修復某個bug後:通常修復完某個bug,爲了確保修復是成功的,會寫測試。框架
好像廢話有點多了,仍是直接奔主題吧。
建立一個工程,名字隨便取,直接勾選include Unit Tests異步
萬一我忘了勾選怎麼辦呢?能夠有其餘方式建立File-->new-->target-->iOS-->iOS Unit Testing Bundle。名字本身看着辦吧。函數
工程建立好後,那要怎麼開始測試呢?
找到系統單元測試Testes文件夾中.m文件看中會到看到幾個方法,咱們來看下這個幾個方法是何時調用和他們各類的做用性能
- (void)setUp { [super setUp]; // Put setup code here. This method is called before the invocation of each test method in the class. //初始化的代碼,在測試方法調用以前調用 } - (void)tearDown { // Put teardown code here. This method is called after the invocation of each test method in the class. // 釋放測試用例的資源代碼,這個方法會每一個測試用例執行後調用 [super tearDown]; } - (void)testExample { // This is an example of a functional test case. // Use XCTAssert and related functions to verify your tests produce the correct results. // 測試用例的例子,注意測試用例必定要test開頭 } - (void)testPerformanceExample { // This is an example of a performance test case. // 測試性能例子 [self measureBlock:^{ // Put the code you want to measure the time of here. // 須要測試性能的代碼 }]; }
在ViewController中寫一個簡單的方法單元測試
- (int)getNum;
實現:測試
- (int)getNum { return 100; }
在測試的文件中導入ViewController.h,而且定義一個vc屬性ui
#import <XCTest/XCTest.h> #import "ViewController.h" @interface ____Tests : XCTestCase @property (nonatomic,strong) ViewController *vc; @end @implementation ____Tests
測試用例的實現atom
- (void)setUp { [super setUp]; // 實例化須要測試的類 self.vc = [[ViewController alloc] init]; } - (void)tearDown { // 清空 self.vc = nil; [super tearDown]; } - (void)testMyFuc { // 調用須要測試的方法, int result = [self.vc getNum]; // 若是不相等則會提示@「測試不經過」 XCTAssertEqual(result, 100,@"測試不經過"); }
command+u快捷方式運行,或者produce-->test都行,
工程就跑起來了spa
咱們能夠在在控制檯清晰的看到咱們要測試的用例子經過了,測試經過的測試方法會有綠色的鉤。
很明顯是能不能經過的,由於咱們要測試的方法返回值是100,
自帶的測試框架還能測試某個方法的性能,
- (void)testPerformanceExample { // This is an example of a performance test case. [self measureBlock:^{ // Put the code you want to measure the time of here. for (int i = 0; i<100; i++) { NSLog(@"dd"); } }];
}
咱們在例子中添加一個for循環,測試其性能。command+u運行就能看到如圖:
可以很是直觀的看出其調用的時間,能夠用其來對比性能的優劣。
另外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語言標量、結構體或聯合體時使用, 判斷的是變量的地址,若是地址相同則返回TRUE,不然返回NO); 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異常時經過;反之發生其餘異常或不發生異常均不經過; XCTAssertThrowsSpecificNamed(expression, specificException, exception_name, format...)異常測試,當expression發生具體異常、具體異常名稱的異常時經過測試,反之不經過; XCTAssertNoThrow(expression, format…)異常測試,當expression沒有發生異常時經過測試; XCTAssertNoThrowSpecific(expression, specificException, format...)異常測試,當expression沒有發生具體異常、具體異常名稱的異常時經過測試,反之不經過; XCTAssertNoThrowSpecificNamed(expression, specificException, exception_name, format...)異常測試,當expression沒有發生具體異常、具體異常名稱的異常時經過測試,反之不經過