您的位置:首页 > 移动开发 > IOS开发

iOS单元测试

2015-12-15 17:24 465 查看
一、使用系统集成XCTest进行性能测试

1、为何要进行单元测试:http://my.oschina.net/w11h22j33/blog/205152, http://blog.csdn.net/qinlicang/article/details/42519787
2、单元测试的使用:(command + U)

1)主要的三个方法:

1. - (void)setUp:每个test方法执行前调用

   2. - (void)tearDown:每个test方法执行后调用

   3. - (void)testXXX:命名为XXX的测试方法
2)基本功能测试(1、准备测试数据;2、调用调用目标API;3、验证输出和行为)

#pragma mark -基本功能测试(验证手机号正则表达式的正确与否)
- (void)testExample {
BOOL telNumber = [UtilscheckTelNumber:@"13120998375"];

XCTAssertTrue(telNumber,
@"手机正则表达式不正确");
}
3)性能测试(主要评估一段代码的运行时间)
#pragma mark -性能测试(测试录音格式转换和存储所消耗的时间)
- (void)testPerformanceExample {
NSData *cafData = [NSDatadataWithContentsOfFile:[NSHomeDirectory()stringByAppendingPathComponent:@"Documents/recording.caf"]];
NSString *filePath2 = [NSStringstringWithFormat:@"%@/%@.amr",NSHomeDirectory(),
[NSStringstringWithFormat:@"%@/%@",@"Documents/",@"tt"]];
[selfmeasureBlock:^{
NSData *amrData =EncodeWAVEToAMR(cafData,1,16);
[amrDatawriteToFile:filePath2atomically:YES];
}];
}
4)异步测试

#pragma mark -异步测试 (通过网络请求做异步测试)
- (void)testGetWithUrl{
NSURL *url = [NSURLURLWithString:@"http://www.baidu.com"];
XCTestExpectation *expectation = [selfexpectationWithDescription:@"GET
\(URL)"];
NSURLSession *session = [NSURLSessionsharedSession];

id task = [session
dataTaskWithURL:url completionHandler:^(NSData *_Nullable data,NSURLResponse
*_Nullable response,
NSError *_Nullable error) {

XCTAssertNotNil(data,
"返回数据不应该为空");
XCTAssertNil(error,"error应该为nil");
[expectationfulfill];
}];

[task resume];
[selfwaitForExpectationsWithTimeout:20.0handler:^(NSError
*_Nullable error) {

if(error){
NSLog(@"请求超时");

[task cancel];

}

}];
}
补充:1、如何测试单例中的方法:http://www.cocoachina.com/ios/20150507/11766.html
2、使用RAC开发的相关测试:http://www.csdn.net/article/2015-09-06/2825624
结语:如果XCTest无法满足测试需要,可以试着使用OCMock 和GHUnit进行单元测试

-------------------------------------------插入断言--------------------------------------------------------------------------------

断言:

======
- 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语言标量、结构体或联合体时使用,实际测试发现NSString也可以);

- 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没有发生具体异常、具体异常名称的异常时通过测试,反之不通过
参考资料连接:
======
> Xcode5单元测试(一)使用XCTest进行单元测试:http://www.it165.net/pro/html/201403/10828.html
> Testing with Xcode文档(中文版):编写测试类与方法:http://www.cocoachina.com/ios/20140715/9144.html
> 官方文档:https://developer.apple.com/library/mac/documentation/DeveloperTools/Conceptual/testing_with_xcode/testing_3_writing_test_classes/testing_3_writing_test_classes.html#//apple_ref/doc/uid/TP40014132-CH4-SW34

二、UI Test
1、添加iOS UI Testing Bundle .添加以后才可以进行UI Test 操作
2、
[app.tabBars.buttons[@"患者"]pressForDuration:3.0];
与 [app.tabBars.buttons[@"患者"] tap];
的区别:
第一个是点击tabbar以后,在下一个页面停留3秒; 第二个方法是点击按钮,然后一个页面运行时间
3、屏幕录制的过程出现部分按钮不识别的Bug,暂时无法解决,请绕过这个测试。
补充:
1、了解API: http://blog.csdn.net/itfootball/article/details/46624331 2、操作规范:http://www.cocoachina.com/ios/20150821/13154.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: