單元測(cè)試主要是檢測(cè)某個(gè)方法或者接口是否可以正常使用
一.環(huán)境搭建
略
二.測(cè)試工具
IOS自帶的單元測(cè)試工具:IOS Unit Testing Bunde
IOS自帶的UI測(cè)試工具:IOS UI Testing Bundle
三.測(cè)試實(shí)踐功能
獲取定位城市
四.被測(cè)試功能
//獲取當(dāng)前定位的城市
- (void)cityStr:(NSString*)cityStr privenceStr:(NSString*)privenceStr;
{
WS(bself);
//城市
self.cityStr=cityStr;
//省份
self.privenceStr=privenceStr;
.若定位城市 為空取值判斷.....
.........................
}
五.單元測(cè)試方法
導(dǎo)入單元測(cè)試與被測(cè)試方法相關(guān)包
#import<XCTest/XCTest.h>
#import"XXController.h"
//測(cè)試的接口方法要繼承XCTestCase
@interfaceXXXControllerTest : XCTestCase{
@private XXXController* userHomeVC;
@private XXXView *homeBtnV;
}
@end
@implementationXXXControllerTest
//setUp為初始选调,測(cè)試執(zhí)行前調(diào)用
- (void)setUp {
[supersetUp];
userHomeVC=[[XXXController alloc]init];
homeBtnV=[[XXX alloc]init];
// Put
setup code here. This method is called before the invocation of each test method
in the class.
}
//tearDown為結(jié)束,測(cè)試執(zhí)行后調(diào)用
- (void)tearDown {
// Put
teardown code here. This method is called after the invocation of each test
method in the class.
[supertearDown];
}
//測(cè)試方法命名必須以test開頭
-(void)testGetCity{
*userHomeVC=[[XXXController alloc]init];
//預(yù)期結(jié)果賦值
NSString*equalCity=@"安慶";
NSString*equalPrivence=@"安徽";
//調(diào)用被測(cè)試方法傳參
[userHomeVCcityStr:@"安慶"privenceStr:@"安徽"];
//將獲取倒的省份賦值給privenceStr
NSString*privenceStr=userHomeVC.privenceStr;
//將獲取倒的省份賦值給city
NSString*city=userHomeVC.cityStr;
//輸出日志
NSLog(@"省=%@城市=%@equalCity=%@",privenceStr,city,equalCity);
//斷言獲取倒的省份是否等于預(yù)期的省份,如果正確就返回true,否側(cè)反回else
XCTAssertEqualObjects(city, equalCity,@"獲取城市驗(yàn)證成功");
//斷言獲取倒的省份是否等于預(yù)期的城市 如果正確就返回true,否側(cè)反回else
XCTAssertEqualObjects(privenceStr, equalPrivence,@"獲取省驗(yàn)證成功");
}
六.測(cè)試結(jié)果