關(guān)于iOS單元測(cè)試
單元測(cè)試是針對(duì)程序模塊來進(jìn)行正確性檢驗(yàn)的測(cè)試工作博秫。程序單元是應(yīng)用的最小可測(cè)試部件仔蝌。進(jìn)行單元測(cè)試漩符,目的就是為了證明這段代碼的行為和我們期望的一致,比如測(cè)試一些功能是否正常酝豪,接口是否正常涛碑。
iOS集成了自己的測(cè)試框架Unit Tests
和UI Tests
。在新建一個(gè)工程的時(shí)候孵淘,可以直接選擇包含單元測(cè)試框架:
也可以在Xcode上方菜單欄選擇 File->New->Target蒲障,打開之后選擇iOS->iOS Unit Testing Bundle。
工程建好之后瘫证,我們可以看到一個(gè)工程名+Tests的文件夾揉阎,打開里面的.m文件,我們可以看到有幾個(gè)方法:
- (void)setUp {
[super setUp];
// Put setup code here. This method is called before the invocation of each test method in the class.
// 這個(gè)方法在測(cè)試方法調(diào)用之前被調(diào)用
}
- (void)tearDown {
// Put teardown code here. This method is called after the invocation of each test method in the class.
// 釋放測(cè)試用例的資源代碼背捌,這個(gè)方法會(huì)每個(gè)測(cè)試用例執(zhí)行后調(diào)用
[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.
// 測(cè)試用例的例子毙籽,注意測(cè)試用例一定要test開頭
}
- (void)testPerformanceExample {
// This is an example of a performance test case.
// 測(cè)試性能例子
[self measureBlock:^{
// Put the code you want to measure the time of here.
// 測(cè)試代碼執(zhí)行時(shí)間
}];
}
上面代碼中的- (void)testExample
方法只是一個(gè)測(cè)試用例的例子,我們還可以自己定義測(cè)試用例方法毡庆,但是必須以test
來開頭坑赡,一般test后面跟上要測(cè)試的方法名。
如何進(jìn)行測(cè)試
我們可以先在ViewController.h
中隨便定義一個(gè)方法么抗,返回一個(gè)int值:
- (int)number;
然后在ViewController.m
中實(shí)現(xiàn):
- (int)number{
return 10;
}
接下來我們開始對(duì)這個(gè)方法進(jìn)行簡(jiǎn)單的測(cè)試垮衷,我們?cè)跍y(cè)試文件中導(dǎo)入ViewController.h
,然后創(chuàng)建一個(gè)屬性:
@property (nonatomic, strong) ViewController *vc;
在測(cè)試類的- (void)setUp
中對(duì)vc
進(jìn)行初始化:_vc = [[ViewController alloc] init];
乖坠。在tearDown
方法中將測(cè)試用例的資源釋放:self.vc = nil;
注意釋放要在[super tearDown]
之前執(zhí)行搀突。
然后寫測(cè)試用例:
- (void)testExample {
int result = [self.vc number];
// 測(cè)試返回的結(jié)果是否等于10
XCTAssertEqual(result, 10,@"hahaha");
}
最后Command+u執(zhí)行測(cè)試。也可以把測(cè)試的結(jié)果改一下運(yùn)行看測(cè)試不通過會(huì)有什么效果熊泵。
XCTest一些常用的斷言(暫時(shí)寫這么多仰迁,其他自己查):
XCTFail(format…) 生成一個(gè)失敗的測(cè)試;
XCTAssertNil(a1, format...)為空判斷顽分,a1為空時(shí)通過徐许,反之不通過;
XCTAssertNotNil(a1, format…)不為空判斷卒蘸,a1不為空時(shí)通過雌隅,反之不通過;
XCTAssert(expression, format...)當(dāng)expression求值為TRUE時(shí)通過缸沃;
XCTAssertTrue(expression, format...)當(dāng)expression求值為TRUE時(shí)通過恰起;
XCTAssertFalse(expression, format...)當(dāng)expression求值為False時(shí)通過;
XCTAssertEqualObjects(a1, a2, format...)判斷相等趾牧,[a1 isEqual:a2]值為TRUE時(shí)通過检盼,其中一個(gè)不為空時(shí),不通過翘单;
XCTAssertNotEqualObjects(a1, a2, format...)判斷不等吨枉,[a1 isEqual:a2]值為False時(shí)通過蹦渣;
XCTAssertEqual(a1, a2, format...)判斷相等(當(dāng)a1和a2是 C語言標(biāo)量、結(jié)構(gòu)體或聯(lián)合體時(shí)使用, 判斷的是變量的地址貌亭,如果地址相同則返回TRUE柬唯,否則返回NO);
XCTAssertNotEqual(a1, a2, format...)判斷不等(當(dāng)a1和a2是 C語言標(biāo)量圃庭、結(jié)構(gòu)體或聯(lián)合體時(shí)使用)权逗;
XCTAssertEqualWithAccuracy(a1, a2, accuracy, format...)判斷相等,(double或float類型)提供一個(gè)誤差范圍冤议,當(dāng)在誤差范圍(+/-accuracy)以內(nèi)相等時(shí)通過測(cè)試斟薇;
XCTAssertNotEqualWithAccuracy(a1, a2, accuracy, format...) 判斷不等,(double或float類型)提供一個(gè)誤差范圍恕酸,當(dāng)在誤差范圍以內(nèi)不等時(shí)通過測(cè)試堪滨;
完整測(cè)試代碼:
#import <XCTest/XCTest.h>
#import "ViewController.h"
@interface XCTestZcTests : XCTestCase
@property (nonatomic, strong) ViewController *vc;
@end
@implementation XCTestZcTests
- (void)setUp {
[super setUp];
// Put setup code here. This method is called before the invocation of each test method in the class.
// 初始化代碼 在測(cè)試方法之前調(diào)用
_vc = [[ViewController alloc] init];
}
- (void)tearDown {
// Put teardown code here. This method is called after the invocation of each test method in the class.
// 釋放測(cè)試用例的資源,每個(gè)測(cè)試用例執(zhí)行后調(diào)用
self.vc = nil;
[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.
// 測(cè)試用例 (一定要test開頭)
int result = [self.vc number];
XCTAssertEqual(result, 10,@"hahaha");
XCTAssertTrue(result);
}
- (void)testPerformanceExample {
// This is an example of a performance test case.
// 測(cè)試性能例子
[self measureBlock:^{
// Put the code you want to measure the time of here.
// 需要測(cè)試性能的代碼
}];
}
//- (void)testTear{
// int result = [self.vc number];
// XCTAssertFalse(result);
//}
- (void)testPerformaneHaha{
[self measureBlock:^{
NSLog(@"test");
}];
}
@end
真正的大師蕊温,永遠(yuǎn)懷著一顆學(xué)徒的心——易