![Uploading QQ20160129-3_262826.png . . .]####什么是單元測試?
一聽到單元測試這個詞感覺很高端,其實單元測試就是為你的方法多專門寫一個測試函數。以保證你的方法在不停的修改開發(fā)中。保持正確。如果出錯,第一時間讓你知道,這樣從最小單位開始監(jiān)控來保證軟件的質量。
什么時候用到單元測試:
1、寫完代碼以后:想要驗證一下自己寫的代碼是否有問題。
2、寫代碼之前:就是寫代碼之前所有的功能分模塊的設計好,測試通過了再寫。(我反正是沒用過)。
3、修復某個bug后:一般修復完某個bug,為了確保修復是成功的,會寫測試。
怎么寫單元測試
好像廢話有點多了,還是直接奔主題吧。
創(chuàng)建一個工程,名字隨便取,直接勾選include Unit Tests
萬一我忘了勾選怎么辦呢?可以有其他方式創(chuàng)建File-->new-->target-->iOS-->iOS Unit Testing Bundle。名字自己看著辦吧。
工程創(chuàng)建好后,那要怎么開始測試呢?
找到系統單元測試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.
// 釋放測試用例的資源代碼,這個方法會每個測試用例執(zhí)行后調用
[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屬性
#import <XCTest/XCTest.h>
#import "ViewController.h"
@interface ____Tests : XCTestCase
@property (nonatomic,strong) ViewController *vc;
@end
@implementation ____Tests
測試用例的實現
- (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都行,
工程就跑起來了
我們可以在在控制臺清晰的看到我們要測試的用例子通過了,測試通過的測試方法會有綠色的鉤。
這時候我們改下斷言,把100隨便改成一個數,120.再comand+u運行下,看下什么情況
很明顯是能不能通過的,因為我們要測試的方法返回值是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循環(huán),測試其性能。command+u運行就能看到如圖:
能夠非常直觀的看出其調用的時間,可以用其來對比性能的優(yō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發(fā)生異常時通過;反之不通過;(很變態(tài)) XCTAssertThrowsSpecific(expression, specificException, format...) 異常測試,當expression發(fā)生specificException異常時通過;反之發(fā)生其他異常或不發(fā)生異常均不通過;
XCTAssertThrowsSpecificNamed(expression, specificException, exception_name, format...)異常測試,當expression發(fā)生具體異常、具體異常名稱的異常時通過測試,反之不通過;
XCTAssertNoThrow(expression, format…)異常測試,當expression沒有發(fā)生異常時通過測試;
XCTAssertNoThrowSpecific(expression, specificException, format...)異常測試,當expression沒有發(fā)生具體異常、具體異常名稱的異常時通過測試,反之不通過;
XCTAssertNoThrowSpecificNamed(expression, specificException, exception_name, format...)異常測試,當expression沒有發(fā)生具體異常、具體異常名稱的異常時通過測試,反之不通過