iOS 单元测试之XCTest详解(一)

http://blog.csdn.net/hello_hwc/article/details/46671053

原创blog,转载请注明出处 
blog.csdn.net/hello_hwc 
欢迎关注我的iOS-SDK详解专栏 
http://blog.csdn.net/column/details/huangwenchen-ios-sdk.html


前言:测试是一个好的App不可缺少的部分。每一个App都是由一个个小的功能组合到一起的。而这些小的功能又是由一个个函数或者说算法组合到一起的。单元测试就是对这些小的功能或者函数进行测试,良好的单元测试会让代码的健壮性提高很多。XCTest就是XCode为我们提供的一个框架,它提供了各个层次的测试。


XCTestCase

每个XCode创建iOS的工程中都有一个叫做”工程名Tests”的分组,这个分组里就是XCTestCase的子类,XCTest中的测试类都是继承自XCTestCase。 
例如新建一个工程,命名为Demo,就能看到如图 
 
看一下这个自动创建的文件里都包含了哪些内容

#import <UIKit/UIKit.h>
#import <XCTest/XCTest.h> @interface DemoTests : XCTestCase @end @implementation DemoTests - (void)setUp {
[super setUp];
// Put setup code here. This method is called before the invocation of each test method in the class.
} - (void)tearDown {
// Put teardown code here. This method is called after the invocation of each test method in the class.
[super tearDown];
} - (void)testExample {
// This is an example of a functional test case.
XCTAssert(YES, @"Pass");
} - (void)testPerformanceExample {
// This is an example of a performance test case.
[self measureBlock:^{
// Put the code you want to measure the time of here.
}];
} @end
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

测试用例的命名

XCTest中所有的测试用例的命名都是以test开头的。例如上文中的

- (void)testExample {
// This is an example of a functional test case.
XCTAssert(YES, @"Pass");
}
  • 1
  • 2
  • 3
  • 4
  • 5

setUp和tearDown

Setup是在所有测试用例运行之前运行的函数,在这个测试用例里进行一些通用的初始化工作

tearDown是在所有的测试用例都执行完毕后执行的


XCode的测试用例导航

测试用例的导航如图,在测试用例的导航里,我们可以运行一组测试用例,也可以运行一个单独的测试用例 

可以鼠标右键来新建一组测试用例。 

也可以为测试用例添加失败断点来方便我们调试 


普通方法测试

例如,新建一个类命名为Model,他有这个方法用来生成10以内的随机数。

-(NSInteger)randomLessThanTen{
return arc4random()%10;
}
  • 1
  • 2
  • 3

于是,测试方法为

-(void)testModelFunc_randomLessThanTen{
Model * model = [[Model alloc] init];
NSInteger num = [model randomLessThanTen];
XCTAssert(num<10,@"num should less than 10");
}
  • 1
  • 2
  • 3
  • 4
  • 5

我们点击如图的左边图标单独运行这个测试用例,当然也可以在上文我提到的导航栏里单独运行。 
 
然后会看到输出表示这个测试用例通过

Test Suite 'Selected tests' started at 2015-06-28 05:24:56 +0000
Test Suite 'DemoTests.xctest' started at 2015-06-28 05:24:56 +0000
Test Suite 'DemoTests' started at 2015-06-28 05:24:56 +0000
Test Case '-[DemoTests testModelFunc_randomLessThanTen]' started.
Test Case '-[DemoTests testModelFunc_randomLessThanTen]' passed (0.000 seconds).
Test Suite 'DemoTests' passed at 2015-06-28 05:24:56 +0000.
Executed 1 test, with 0 failures (0 unexpected) in 0.000 (0.001) seconds
Test Suite 'DemoTests.xctest' passed at 2015-06-28 05:24:56 +0000.
Executed 1 test, with 0 failures (0 unexpected) in 0.000 (0.001) seconds
Test Suite 'Selected tests' passed at 2015-06-28 05:24:56 +0000.
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

常用断言

如何判断一个测试用例成功或者失败呢?XCTest使用断言来实现。 
最基本的断言 
表示如果expression满足,则测试通过,否则对应format的错误。

XCTAssert(expression, format...)
  • 1

还有一个用来直接Fail的断言

XCTFail(format...)
  • 1

其他一些常用的断言:

XCTAssertTrue(expression, format...)
XCTAssertFalse(expression, format...)
XCTAssertEqual(expression1, expression2, format...)
XCTAssertNotEqual(expression1, expression2, format...)
XCTAssertEqualWithAccuracy(expression1, expression2, accuracy, format...)
XCTAssertNotEqualWithAccuracy(expression1, expression2, accuracy, format...)
XCTAssertNil(expression, format...)
XCTAssertNotNil(expression, format...)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

性能测试

所谓性能测试,主要就是评估一段代码的运行时间,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.
}];
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

例如,我要评估一段代码,这段代码的功能是把一张图片缩小到指定的大小。 
这段代码如下,这段代码我放在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;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

然后测试用例如图,主要判断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");
}];
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

输出

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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

异步测试

异步测试的逻辑如下,首先定义一个或者多个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
}];
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

举例

- (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
}];
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

测试结果

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.

iOS 单元测试之XCTest详解(一)的更多相关文章

  1. Java基础学习总结(24)——Java单元测试之JUnit4详解

    Java单元测试之JUnit4详解 与JUnit3不同,JUnit4通过注解的方式来识别测试方法.目前支持的主要注解有: @BeforeClass 全局只会执行一次,而且是第一个运行 @Before  ...

  2. iOS学习之UINavigationController详解与使用(一)添加UIBarButtonItem

    http://blog.csdn.net/totogo2010/article/details/7681879 1.UINavigationController导航控制器如何使用 UINavigati ...

  3. IOS—UITextFiled控件详解

    IOS—UITextFiled控件详解 //初始化textfield并设置位置及大小 UITextField *text = [[UITextField alloc]initWithFrame:CGR ...

  4. [转]iOS学习之UINavigationController详解与使用(三)ToolBar

    转载地址:http://blog.csdn.net/totogo2010/article/details/7682641 iOS学习之UINavigationController详解与使用(二)页面切 ...

  5. IOS 友盟使用详解

    IOS 友盟使用详解 这篇博客将会详细介绍友盟的使用,希望对博友们有所帮助. 首先我们在浏览器上搜索友盟. 在这里我们选择官网这个,进去友盟官网后我们按照下图进行选择. 接下来选择如下图 Next 这 ...

  6. iOS原生地图开发详解

    在上一篇博客中:http://my.oschina.net/u/2340880/blog/414760.对iOS中的定位服务进行了详细的介绍与参数说明,在开发中,地位服务往往与地图框架结合使用,这篇博 ...

  7. [转]iOS学习之UINavigationController详解与使用(二)页面切换和segmentedController

    转载地址:http://blog.csdn.net/totogo2010/article/details/7682433 iOS学习之UINavigationController详解与使用(一)添加U ...

  8. iOS中—触摸事件详解及使用

    iOS中--触摸事件详解及使用 (一)初识 要想学好触摸事件,这第一部分的基础理论是必须要学会的,希望大家可以耐心看完. 1.基本概念: 触摸事件 是iOS事件中的一种事件类型,在iOS中按照事件划分 ...

  9. ios新特征 ARC详解

    IOS ARC 分类: IOS ARC2013-01-17 09:16 2069人阅读 评论(0) 收藏 举报   目录(?)[+]   关闭工程的ARC(Automatic Reference Co ...

随机推荐

  1. 【原】CSS3 Media在常用设备的设置值

    摘要:今天的一个小小的项目中,在各种手机上样式都显示正常,唯独iphone4s的有些许问题.产品经理又说iphone4s用户还挺多的,无奈,只能查一查iphone4s的media值,顺便做个小小总结; ...

  2. 条件运算符(?:)和 $""替代string.Format()

    1. 条件运算符(?:)根据Boolean表达式的值返回两个值之一.表达式如下: condition ? first_expression : second_expression 2. $" ...

  3. Extjs GridPanel用法详解

    Extjs GridPanel 提供了非常强大数据表格功能,在GridPanel可以展示数据列表,可以对数据列表进行选择.编辑等.在之前的Extjs MVC开发模式详解中,我们已经使用到了GridPa ...

  4. phpcms后台获取当前登录账号的数据

    $amdinid=$_SESSION['userid'];$infoadmin=$this->admin->get_one(array('userid'=>$amdinid)); v ...

  5. angularjs笔记(二)

    AngularJS API 4.AngularJS过滤器 使用一个管道符(|)添加到表达式和指令中 例1.格式化字母转为大写 <!DOCTYPE html> <html> &l ...

  6. 【ZeroClipboard is not defined】的解决方法

    参考:http://www.cnblogs.com/jfw10973/p/3921899.html https://github.com/zeroclipboard/zeroclipboard 近期该 ...

  7. eshop截取字符串长度 和去掉省略号

    <!-- {if $goods.goods_brief} --> {$goods.goods_brief|truncate:17}<!-- {/if} --> 去掉省略号: 找 ...

  8. Create new tool for CSV

    CsvFileStream.cs public class CsvFileStream { TextReader stream; bool EOS = false; bool EOL = false; ...

  9. 注入问题0x00

    1.sqlmap遇到MySQL注入可以成功getshell,但是,遇到sqlserver注入未成功getshell. 2.xp_cmdshell 如何 getshell(1433未对外开放). 解决方 ...

  10. Java面试笔记

    1.&和&& if(str != null& !str.equals("")){ System.out.println("ok" ...