单元测试主要是检测某个方法或者接口是否可以正常使用
一.环境搭建
略
二.测试工具
IOS自带的单元测试工具:IOS Unit Testing Bunde
IOS自带的UI测试工具:IOS UI Testing Bundle
三.测试实践功能
获取定位城市
四.被测试功能
//获取当前定位的城市
- (void)cityStr:(NSString*)cityStr privenceStr:(NSString*)privenceStr;
{
WS(bself);
//城市
self.cityStr=cityStr;
//省份
self.privenceStr=privenceStr;
.若定位城市 为空取值判断.....
.........................
}
五.单元测试方法
导入单元测试与被测试方法相关包
#import<XCTest/XCTest.h>
#import"XXController.h"
//测试的接口方法要继承XCTestCase
@interfaceXXXControllerTest : XCTestCase{
@private XXXController* userHomeVC;
@private XXXView *homeBtnV;
}
@end
@implementationXXXControllerTest
//setUp为初始,测试执行前调用
- (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为结束,测试执行后调用
- (void)tearDown {
// Put
teardown code here. This method is called after the invocation of each test
method in the class.
[supertearDown];
}
//测试方法命名必须以test开头
-(void)testGetCity{
*userHomeVC=[[XXXController alloc]init];
//预期结果赋值
NSString*equalCity=@"安庆";
NSString*equalPrivence=@"安徽";
//调用被测试方法传参
[userHomeVCcityStr:@"安庆"privenceStr:@"安徽"];
//将获取倒的省份赋值给privenceStr
NSString*privenceStr=userHomeVC.privenceStr;
//将获取倒的省份赋值给city
NSString*city=userHomeVC.cityStr;
//输出日志
NSLog(@"省=%@城市=%@equalCity=%@",privenceStr,city,equalCity);
//断言获取倒的省份是否等于预期的省份,如果正确就返回true,否侧反回else
XCTAssertEqualObjects(city, equalCity,@"获取城市验证成功");
//断言获取倒的省份是否等于预期的城市 如果正确就返回true,否侧反回else
XCTAssertEqualObjects(privenceStr, equalPrivence,@"获取省验证成功");
}
执行成功六.测试结果
执行失败
网友评论