原创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的project中都有一个叫做”project名Tests”的分组,这个分组里就是XCTestCase的子类。XCTest中的測试类都是继承自XCTestCase。

比如新建一个project,命名为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

測试用例的命名

XCTest中全部的測试用例的命名都是以test开头的。比如上文中的

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

setUp和tearDown

Setup是在全部測试用例执行之前执行的函数,在这个測试用例里进行一些通用的初始化工作

tearDown是在全部的測试用例都执行完毕后执行的


XCode的測试用例导航

測试用例的导航如图。在測试用例的导航里,我们能够执行一组測试用例,也能够执行一个单独的測试用例

能够鼠标右键来新建一组測试用例。

也能够为測试用例加入失败断点来方便我们调试


普通方法測试

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

-(NSInteger)randomLessThanTen{
return arc4random()%10;
}

于是。測试方法为

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

我们点击如图的左边图标单独执行这个測试用例,当然也能够在上文我提到的导航栏里单独执行。



然后会看到输出表示这个測试用例通过

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.

经常使用断言

怎样推断一个測试用例成功或者失败呢?XCTest使用断言来实现。

最主要的断言

表示假设expression满足。则測试通过,否则相应format的错误。

XCTAssert(expression, format...)

另一个用来直接Fail的断言

XCTFail(format...)

其它一些经常使用的断言:

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...)

性能測试

所谓性能測试,主要就是评估一段代码的执行时间,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.

兴许:

计划下一篇会解说Mock 測试以及一些经常使用的Mock小工具。


iOS 单元測试之XCTest具体解释(一)的更多相关文章

  1. iOS单元測试:Specta + Expecta + OCMock + OHHTTPStubs + KIF

    框架选择 參考这篇选型文章,http://zixun.github.io/blog/2015/04/11/iosdan-yuan-ce-shi-xi-lie-dan-yuan-ce-shi-kuang ...

  2. ios单元測试之GHUnit

    1.相同创建一个測试的project, 2.通过cocoaPod来下载GHUnit框架,或者到github上下载.由于这个框架是开源的第三方框架. 同一时候加入QuartCore.framework( ...

  3. [iOS翻译]《iOS7 by Tutorials》在Xcode 5里使用单元測试(上)

    简单介绍: 单元測试是软件开发的一个重要方面.毕竟,单元測试能够帮你找到bug和崩溃原因,而程序崩溃是Apple在审查时拒绝app上架的首要原因. 单元測试不是万能的,但Apple把它作为开发工具包的 ...

  4. Android单元測试之JUnit

    随着近期几年測试方面的工作慢慢火热起来.常常看见有招聘測试project师的招聘信息.在Java中有单元測试这么一个JUnit 方式,Android眼下主要编写的语言是Java,所以在Android开 ...

  5. Android 进行单元測试难在哪-part3

    原文链接 : HOW TO MAKE OUR ANDROID APPS UNIT TESTABLE (PT. 1) 原文作者 : Matthew Dupree 译文出自 : 开发技术前线 www.de ...

  6. 【Android进阶】Junit单元測试环境搭建以及简单有用

    单元測试的目的 首先.Junit单元測试要实现的功能,就是用来測试写好的方法是否可以正确的运行,一般多用于对业务方法的測试. 单元測试的环境配置 1.在AndroidManifest清单文件的Appl ...

  7. 使用maven运行单元測试总结

    maven本身没有单元測试框架,可是maven的default生命周期的test阶段绑定了maven-surefire-plugin插件,该插件能够调用Junit3.Junit4.TestNG等Jav ...

  8. method initializationerror not found:JUnit4单元測试报错问题

           今天使用JUnit 4进行单元測试时,測试程序一直执行不起来,报method initializationerror not found错误.例如以下:            网上说版本 ...

  9. 利用Continuous Testing实现Eclipse环境自己主动单元測试

    当你Eclipse环境中改动项目中的某个方法时,你可能因为各种原因没有执行单元測试,结果代码提交,悲剧就可能随之而来. 所幸infinitest(http://infinitest.github.io ...

随机推荐

  1. java假设模拟请求重新启动路由器(网络爬虫经常使用),还有java怎样下载图片

    我们假设在公司或家里使用网络爬虫去抓取自己索要的一些数据的时候,经常对方的站点有defence机制,会给你的http请求返回500错误,仅仅要是同样IP就请求不到数据,这时候我们仅仅能去重新启动路由器 ...

  2. Koa -- 基于 Node.js 平台的下一代 web 开发框架

    http://koa.bootcss.com/ 多研究点 react 和 nodejs 这个是未来

  3. 【thinking in java】读书笔记(一)

    近期開始读tij,好记性不如烂笔头,所以还是记录一下,方便以后查阅. 一.各种初始化问题: 方法重载的问题: 方法的重载,差别是靠传入方法的參数,而不是返回值.比方f(),假设是返回值的话,easy产 ...

  4. [WP8] Binding时,依照DataType来选择DataTemplate

    原文 [WP8] Binding时,依照DataType来选择DataTemplate 范例下载 范例程序代码:点此下载 问题情景 在开发WPF.WP8...这类应用程序的时候,透过Binding机制 ...

  5. io 测试

    //其中能够实现编码的只有OutputStreamWriter和对应inputStreamReader package net; import java.io.BufferedWriter; impo ...

  6. Mysql找回管理员password

    我们使用MYSQL的时候有可能由于种种原因忘记ROOTpassword,假设是那样数据库可能就废掉了.可是今天给大家分享下找回ROOTpassword的方法或者说是在不知道rootpassword的情 ...

  7. 我工作这几年(五)-- Android学习4.5月总结(一)

    今年是对我个人成长和程序员生涯冲击很大的一年. 有了小孩之后,家里发生了太多太多的事情,现在已经慢慢步入正轨,还好撑过来了,当然还有老婆.岳父岳母.我爸妈.还有好多关心支持我的人的帮助.在各种挫折交替 ...

  8. 在VC资源文件中加入声音资源

    本文介绍如何在VC资源文件中加入自己的声音资源,使自己的应用程序可以播放声音. 1.首先用文本编辑器(如记事本)打开资源文件(.rc文件) 在最后加入自己的声音资源,如下IDW WAVE " ...

  9. 【PHP SDK for OpenStack/Rackspace APIs】身份验证

    在你使用php-opencloud之前必须先取得云服务提供商的身份验证.这是整个过程中最简单也是最让人沮丧的部分. 说它最简单是因为你只需要两部分信息: 云服务提供商的身份验证端点 用来身份验证的证书 ...

  10. 《深入理解mybatis原理》 MyBatis事务管理机制

    MyBatis作为Java语言的数据库框架,对数据库的事务管理是其很重要的一个方面.本文将讲述MyBatis的事务管理的实现机制. 首先介绍MyBatis的事务Transaction的接口设计以及其不 ...