XCTest学习笔记
XCTest学习笔记
现在学习了项目中使用的KIF测试,用于UI测试,可是模拟用户的输入点击等情况。通过学习了解到苹果xcode自带测试框架,后来研究学习了一下,下面是学习的记录
XCTest是Xcode5中新引入的一个测试框架,与 Xcode 的 IDE 直接集成,是OCUnit的更现代化实现。XCTest的许多的功能都类似OCUnit。XCTest又可以分为两部分:Unit Test 和 UI Test,分别对应单元测试和UI测试。有一些三方的测试库也是基于XCTest框架的
一、运行第一个单元测试:
1.在Xcode 5中新建一个工程默认自带一个单元测试的文件夹,IDE自动生成了一个实现XCTestCase的.m文件,里面有一个失败测试(早期版本中实现的是SenTestCase,是苹果集成的第三方的,现在苹果建议使用新的XCTestCase)。
2.测试函数的要求是:1.必须无返回值;2.以test开头;
3.测试函数执行的顺序:以函数名中test后面的字符大小有关,比如-(void)test001XXX会先于-(void)test002XXX执行;
4.运行单元测试的快捷键:CMD + U;
5.需要知道每个测试的断言
6.iOS6之后支持异步测试
二、断言测试语句:
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没有发生具体异常、具体异常名称的异常时通过测试,反之不通过
三、常用测试
性能测试
所谓性能测试,主要就是评估一段代码的运行时间,XCTest的性能的测试利用如下格式
- (void)testPerformanceExample {
// This is an example of a performance test case.
[self measureBlock:^{
// Put the code you want to measure the time of here.
}];
}
例如,我要评估一段代码,这段代码的功能是把一张图片缩小到指定的大小。
这段代码如下,这段代码我放在UIImage的类别里。
+ (UIImage*)imageWithImage:(UIImage*)image
scaledToSize:(CGSize)newSize
{
UIGraphicsBeginImageContext( newSize );
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
然后测试用例如图,主要判断resize后是否为nil,并且尺寸是否对。
- (void)testPerformanceExample {
UIImage * image = [UIImage imageNamed:@"icon.png"];
[self measureBlock:^{
UIImage * resizedImage = [UIImage imageWithImage:image scaledToSize:CGSizeMake(100, 100)];
XCTAssertNotNil(resizedImage,@"resized image should not be nil");
CGFloat resizedWidth = resizedImage.size.width;
CGFloat resizedHeight = resizedImage.size.height;
XCTAssert(resizedHeight == 100 && resizedWidth == 100,@"Size is not right");
}];
}
输出
Test Suite ‘Selected tests’ started at 2015-06-28 05:42:39 +0000
Test Suite ‘DemoTests.xctest’ started at 2015-06-28 05:42:39 +0000
Test Suite ‘DemoTests’ started at 2015-06-28 05:42:39 +0000
Test Case ‘-[DemoTests testPerformanceExample]’ started.
/Users/huangwenchen/Desktop/Demo/DemoTests/DemoTests.m:41: Test Case ‘-[DemoTests testPerformanceExample]’ measured [Time, seconds] average: 0.000, relative standard deviation: 40.714%, values: [0.000241, 0.000116, 0.000128, 0.000089, 0.000087, 0.000081, 0.000101, 0.000093, 0.000092, 0.000087], performanceMetricID:com.apple.XCTPerformanceMetric_WallClockTime, baselineName: “”, baselineAverage: , maxPercentRegression: 10.000%, maxPercentRelativeStandardDeviation: 10.000%, maxRegression: 0.100, maxStandardDeviation: 0.100
Test Case ‘-[DemoTests testPerformanceExample]’ passed (0.357 seconds).
Test Suite ‘DemoTests’ passed at 2015-06-28 05:42:40 +0000.
Executed 1 test, with 0 failures (0 unexpected) in 0.357 (0.358) seconds
Test Suite ‘DemoTests.xctest’ passed at 2015-06-28 05:42:40 +0000.
Executed 1 test, with 0 failures (0 unexpected) in 0.357 (0.358) seconds
Test Suite ‘Selected tests’ passed at 2015-06-28 05:42:40 +0000.
Executed 1 test, with 0 failures (0 unexpected) in 0.357 (0.360) seconds
异步测试
异步测试的逻辑如下,首先定义一个或者多个XCTestExpectation,表示异步测试想要的结果。然后设置timeout,表示异步测试最多可以执行的时间。最后,在异步的代码完成的最后,调用fullfill来通知异步测试满足条件。
- (void)testAsyncFunction{
XCTestExpectation * expectation = [self expectationWithDescription:@"Just a demo expectation,should pass"];
//Async function when finished call [expectation fullfill]
[self waitForExpectationsWithTimeout:10 handler:^(NSError *error) {
//Do something when time out
}];
}
举例
- (void)testAsyncFunction{
XCTestExpectation * expectation = [self expectationWithDescription:@"Just a demo expectation,should pass"];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
sleep(1);
NSLog(@"Async test");
XCTAssert(YES,"should pass");
[expectation fulfill];
});
[self waitForExpectationsWithTimeout:10 handler:^(NSError *error) {
//Do something when time out
}];
}
测试结果
Test Suite ‘Selected tests’ started at 2015-06-28 05:49:43 +0000
Test Suite ‘DemoTests.xctest’ started at 2015-06-28 05:49:43 +0000
Test Suite ‘DemoTests’ started at 2015-06-28 05:49:43 +0000
Test Case ‘-[DemoTests testAsyncFunction]’ started.
2015-06-28 13:49:44.920 Demo[2157:145428] Async test
Test Case ‘-[DemoTests testAsyncFunction]’ passed (1.006 seconds).
Test Suite ‘DemoTests’ passed at 2015-06-28 05:49:44 +0000.
Executed 1 test, with 0 failures (0 unexpected) in 1.006 (1.007) seconds
Test Suite ‘DemoTests.xctest’ passed at 2015-06-28 05:49:44 +0000.
Executed 1 test, with 0 failures (0 unexpected) in 1.006 (1.009) seconds
Test Suite ‘Selected tests’ passed at 2015-06-28 05:49:44 +0000.
很好的一篇文章:
http://wiki.jikexueyuan.com/project/objc/test/15-2.html
blog.csdn.net/hello_hwc
上一篇: fetch的使用
下一篇: PowerMock介绍和用法