??????????????????
??????????????У????????????UnitTesting???????????????????????饗???????С??λ????????????????????????????????????С??????????
??????????????У??????????????????????????????????????????С?????????????????????????????????????????????е??????
????????????????????????????????????????C??????????????????????????????????????????????????????????????У??????????????
????????????????????????????????????????????????????????????????
??????????????????????????????????????????????bug???????????????????????????????????????????????????????????
????????iOS???????
????xcode????????????????Xcode4.x?м??????????OCUnit??UITests??iOS9???????????????????????????????????????й???????????IncludeUnitTests??IncludeUITests?????????????????????????appName+Tests???????????????????????????????
?????????????????????????????????????
????a.??????????????????л???????
????b.?????????????????н???????????
????c.??????????????????????
????UnitTest??????淽???????
????1.(void)setUp{//?????????????????????????????????????
????[supersetUp];
????//Putsetupcodehere.
????Thismethodiscalledbeforethe
????invocationofeachtestmethodintheclass.
????}
????2.(void)tearDown{//?????????????????????????????????β????
????//Putteardowncodehere.This
????methodiscalledaftertheinvocation
????ofeachtestmethodintheclass.
????[supertearDown];
????}
????3.(void)testExample{//????????????????????????????????£????????????????????????????test+????????????????????????????(????????????????????test?????????в????????????????????ε?)
????//Thisisanexampleofa
????functionaltestcase.
????//UseXCTAssertandrelated
????functionstoverifyyourtests
????producethecorrectresults.
????}
????4.(void)testPerformanceExample{//???????е?block????????????????--????????10?Σ????????????????????ε??????????????10%?????????????block??????????????????????
????[selfmeasureBlock:^{
????//Putthecodeyouwant
????tomeasurethetimeofhere.
????}];
????}
???????????????????????????и????ε?????????????????????????????????????????????????з????κζ???????????????λ???????????????????command+U??????ε??????е????????
???????????????????????????е????????????????????????в??????????????????????λ????????????е???????????????????

?????????????????????????????????????????????????????????????????????????????????????????????г??????壺??XCTest????????????????
????XCTAssertNotNil(a1??format…)??a1???nil?????
????XCTAssert(expression??format...)??expression????YES????
????XCTAssertTrue(expression??format...)??expression????YES??????
????XCTAssertEqualObjects(a1??a2??format...)?ж???????[a1isEqualTo:a2]????YES????????
????XCTAssertEqual(a1??a2??format...)??a1==a2????YES?????
????XCTAssertNotEqual(a1??a2??format...)??a1!=a2????YES?????
????&&
????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...)?ж?????[a1isEqual:a2]??TRUE????????????????????????????
????XCTAssertNotEqualObjects(a1??a2??format...)?ж?????[a1isEqual: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??з?????????????????????????????????????????
????????????
????1???????????????????????????????????????????仯?????????
????e.g:
????@interfaceTestModel1:NSObject
????@property(nonatomic??copy)
????NSStringname;
????@property(nonatomic??strong)
????NSNumberage;
????@property(nonatomic??assign)
????NSUIntegerflags;
????(instancetype)modelWithName:
????(NSString)nameage:(NSNumber)
????ageflags:(NSUInteger)flags;
????(instancetype)initWithDictionary:
????(NSDictionary*)dict;
????(NSDictionary*)modelToDictionary;
????@end
????@implementationTestModel1
????(instancetype)modelWithName:
????(NSString)nameage:(NSNumber
????)ageflags:(NSUInteger)flags
????{
????TestModel1*model=[[selfalloc]init];
????model.name=name;
????model.age=age;
????model.flags=flags;
????returnmodel;
????}
????(instancetype)initWithDictionary:
????(NSDictionary*)dict
????{
????self.name=dict[@"name"];
????self.age=dict[@"age"];
????self.flags=[dict[@"flags"]
????integerValue];
????returnself;
????}
????(NSDictionary*)modelToDictionary
????{
????return@{@"name":self.name??@"age":
????self.age??@"flags":[NSNumber
????numberWithInteger:self.flags]};
????}
????@end
??????????????????棺
????(void)testModelConvert
????{
????NSStringjson=@"{"name":
????"SindriLin"??"age"
????:22??"flags":987654321}";
????NSMutableDictionarydict=
????[[NSJSONSerializationJSONObjec
????tWithData:[jsondataUsingEncoding:
????NSUTF8StringEncoding]options:
????kNilOptionserror:nil]mutableCopy];
????TestModel1model=[[TestModel1
????alloc]initWithDictionary:dict];
????XCTAssertNotNil(model);
????XCTAssertTrue([model.name
????isEqualToString:@"SindriLin"]);
????XCTAssertTrue([model.ageisEqual:
????@(22)]);
????XCTAssertEqual(model.flags??987654321);
????XCTAssertTrue([modelisKindOfClass:
????[TestModel1class]]);
????model=[TestModel1modelWithName:
????@"Tessie"age:dict[@"age"]flags:
????562525];
????XCTAssertNotNil(model);
????XCTAssertTrue([model.nameisEqual
????ToString:@"Tessie"]);
????XCTAssertTrue([model.ageisEqual:dict[@"age"]]);
????XCTAssertEqual(model.flags??562525);
????NSDictionarymodelJSON=[model
????modelToDictionary];
????XCTAssertTrue([modelJSONisEqual:
????dict]==NO);
????dict[@"name"]=@"Tessie";
????dict[@"flags"]=@(562525);
????XCTAssertTrue([modelJSONisEqual:
????dict]);
????}
????2??????????
???????????????У???????????????instrument(xcode->product->profile)???????????????е????????????????????????????????????????
?????????????
????(void)testPerformanceExample{//???????е?block????????????????--????????10?Σ????????????????????ε??????????????10%??
????//Thisisanexampleofaperformance
????testcase.[selfmeasureBlock:^{
????//Putthecodeyouwanttomeasure
????thetimeofhere.
????[TestModel1randomModels];
????//for(inti=0;i<100;++i){
????//NSLog(@"wgj:%d"??i);
????//}
????}];
????}
??????????model?????????
????(NSArray<TestModel1*>)randomModels
????{
????NSMutableArraymodels=@[].mutableCopy;
????NSArray*names=@[
????@"xiaoli01"??@"xiaoli02"??@"xiaoli03"??
????@"xiaoli04"??@"xiaoli05"
????];
????NSArray*ages=@[
????@15?? @20?? @25?? @30?? @35
????];
????NSArray*flags=@[
????@123??@456??@789??@012??@234
????];
????for(NSUIntegeridx=0;idx<100;idx++){
????TestModel1*model=[selfmodelWithName:
????names[arc4random()%names.count]age:
????ages[arc4random()%ages.count]flags:
????[flags[arc4random()%flags.count]
????unsignedIntegerValue]];
????[modelsaddObject:model];
????[NSThreadsleepForTimeInterval:0.01];
????}
????returnmodels;
????}

???????????test?????У????????????????????????????棬????????????????????????????в??????


??????????????????????????????????????????????δ????????????????instrument????instrument??λ????????????????????????????????????????????????????????Щ????????????????????????????????????????????????CPU?????????