iOS单元測试:Specta + Expecta + OCMock + OHHTTPStubs + KIF
框架选择
XCTest简单介绍
BDD框架 — Specta
1. 简单介绍
以下三个OC BDD框架相对于官方框架XCTest都具有更好的可读性。另外如今已经有了比較流行的swift BDD框架: https://github.com/railsware/Sleipnir 和 https://github.com/Quick/Quick。
- https://github.com/specta/specta
- https://github.com/kiwi-bdd/Kiwi
- https://github.com/pivotal/cedar
- An OC RSpec-like BDD DSL
- Quick and easy set up
- Build on top of XCTest
- Excellent Xcode integration
2. Specta BDD DSL语法简单介绍
- it(@"should do some stuff asynchronously", ^{
waitUntil(^(DoneCallback done) {
// Async example blocks need to invoke done() callback.
done();
});
});
断言框架 — Expecta
mock框架 — OCMock
另外能够參考开源项目 https://github.com/artsy/eigen,学习当中的OCMock API的使用,框架使用比較简单,看看就懂了,不须要多说。
以下能够看出一个OCMock基本过程:获得OCMockObject -> stub方法 -> 设置expect -> verify校验运行结果 -> 调用stopMocking
- http://ocmock.org/reference/
- http://ocmock.org/features/
- http://ocmock.org/introduction/
- http://www.archive.alexvollmer.com/posts/2010/06/28/making-fun-of-things-with-ocmock/
- http://hackazach.net/code/2014/03/03/effective-testing-with-ocmock/。翻译:http://zixun.github.io/blog/2015/04/16/iosdan-yuan-ce-shi-xi-lie-yi-ocmockchang-jian-shi-yong-fang-shi/
- http://engineering.aweber.com/improving-ios-unit-tests-with-ocmock/
OHHTTPStubs
return [request.URL.host isEqualToString:@"mywebservice.com"];
} withStubResponse:^OHHTTPStubsResponse*(NSURLRequest *request) {
// Stub it with our "wsresponse.json" stub file (which is in same bundle as self)
NSString* fixture = OHPathForFile(@"wsresponse.json", self.class);
return [OHHTTPStubsResponse responseWithFileAtPath:fixture
statusCode:200 headers:@{@"Content-Type":@"application/json"}];
{
NSURLRequest* request = ...
XCTestExpectation* responseArrived = [self expectationWithDescription:@"response of async request has arrived"];
__block NSData* receivedData = nil;
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse* response, NSData* data, NSError* error)
{
receivedData = data;
[responseArrived fulfill];
}
];
[self waitForExpectationsWithTimeout:timeout handler:^{
// By the time we reach this code, the while loop has exited
// so the response has arrived or the test has timed out
XCTAssertNotNil(receivedData, @"Received data should not be nil");
}];
}
F.I.R.S.T 原则
- Fast — 測试应该可以被常常执行
- Isolated — 測试本身不能依赖于外部因素或其它測试的结果
- Repeatable — 每次执行測试都应该产生同样的结果
- Self-verifying — 測试应该依赖于断言,不须要人为干预
- Timely — 測试应该和生产代码一同书写
- 不要測试私有方法
- 不要Stub私有方法
- 不要Stub外部库
- 正确地Stub依赖
- 不要測试构造函数
參考资料
- http://www.objc.io/issues/15-testing/,(翻译:http://objccn.io/issue-15/ )
- https://github.com/artsy/eigen,很专业的APP的开源码,http://objccn.io/issue-22-2/
- <Functional Reactive Programming on iOS>: RAC + 单元測试
- http://www.jianshu.com/p/73f9d719cee4
- http://nshipster.com/unit-testing/
- http://onevcat.com/2014/02/ios-test-with-kiwi/
- http://onevcat.com/2014/05/kiwi-mock-stub-test/
- https://github.com/dblock/fui,find unused objective-c imports
- <Testing with Xcode>
- <Pro iOS Continuous Integration>
iOS单元測试:Specta + Expecta + OCMock + OHHTTPStubs + KIF相关推荐
- 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 ...
- atitit.jndi的架构与原理以及资源配置and单元測试实践
atitit.jndi的架构与原理以及资源配置and单元測试实践 1. jndi架构 1 2. jndi实现原理 3 3. jndi资源配置 3 3.1. resin <database> ...
- 太白---落燕纷飞第一重 Android单元測试Instrumentation和irobotium
PS:叫太白---落燕纷飞纯粹好玩(天涯明月游戏画面感,打击感,碰撞尽管做的不尽人意,可是太白这个职业还是不错,用作开头,,做个旁白而已). 这里的单元測试不管是instrumentation还是ir ...
- Android单元測试之JUnit
随着近期几年測试方面的工作慢慢火热起来.常常看见有招聘測试project师的招聘信息.在Java中有单元測试这么一个JUnit 方式,Android眼下主要编写的语言是Java,所以在Android开 ...
- 使用maven运行单元測试总结
maven本身没有单元測试框架,可是maven的default生命周期的test阶段绑定了maven-surefire-plugin插件,该插件能够调用Junit3.Junit4.TestNG等Jav ...
- 在Eclipse中使用JUnit4进行单元測试(0基础篇)
本文绝大部分内容引自这篇文章: http://www.devx.com/Java/Article/31983/0/page/1 我们在编写大型程序的时候,须要写成千上万个方法或函数,这些函数的功能可能 ...
- 【Android进阶】Junit单元測试环境搭建以及简单有用
单元測试的目的 首先.Junit单元測试要实现的功能,就是用来測试写好的方法是否可以正确的运行,一般多用于对业务方法的測试. 单元測试的环境配置 1.在AndroidManifest清单文件的Appl ...
- 利用Continuous Testing实现Eclipse环境自己主动单元測试
当你Eclipse环境中改动项目中的某个方法时,你可能因为各种原因没有执行单元測试,结果代码提交,悲剧就可能随之而来. 所幸infinitest(http://infinitest.github.io ...
- Android 进行单元測试难在哪-part3
原文链接 : HOW TO MAKE OUR ANDROID APPS UNIT TESTABLE (PT. 1) 原文作者 : Matthew Dupree 译文出自 : 开发技术前线 www.de ...
最新文章
- 【Flutter】Icons 组件 ( FlutterIcon 下载图标 | 自定义 svg 图标生成 ttf 字体文件 | 使用下载的 ttf 图标文件 )
- mysqldump 导出某几张表
- 中国.NET域名注册量近55万个 稳居全球第三位
- Excel表中分类汇总数据如何只复制结果
- MongoDB sharding模式实现(http://blog.chinaunix.net/xmlrpc.php?r=blog/articleuid=28266791id=5758139 )
- 关于多线程编程您不知道的 5 件事 有关高性能线程处理的微妙之处
- MySQL 基础————常用数据类型
- 制作Android系统签名方法
- 虚拟机桥接模式网络配置
- cmake:cmake_policy命令
- 记公司同事的一次集体活动
- win10系统无法打开计算器等应用解决办法
- 分布式-幂等性解决方案
- 迷你播放器--第一阶段(4)--内存卡检索不到音乐的错误处理--程序内部空间管理
- 字节跳动最爱考的前端面试题:Node.js 基础
- burp抓手机数据包
- Qt for WebAssembly in Windows
- 语音算法笔记(3)——从序列建模的角度理解ASR
- notion函数_高效工作必备,手把手教你玩转 Notion 模板(附下载)
- Wannafly挑战赛26
热门文章
- linux查看系统日志常见命令以及方法
- 【博览群书】《实战大数据》——属于我的第一本大数据图书
- python使用requests_toolbelt库做multipart格式HTTP上传请求
- 自考计算机应用与辅助设计,黑龙江2012年自考计算机应用与辅助设计考试大纲.doc...
- Kafka 深度剖析
- C++A类继承B C类_基金定投买a类还是c类好,c类基金为什么不适合定投
- 如何从 PC 切换到 Mac,以及你需要了解的内容
- tar 解压文件到指定目录
- wordpress插件_7个WordPress插件为您的网站准备圣诞节
- SAP CRM 开发学习资料和教程整理