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。
- 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();
});
});
设置多个spec之间共享的test case,第一个參数作为标识符。通过itShouldBehaveLike来执行spec中test case。第一个參数传入sharedExamplesFor设置时使用的标识符。注意。在describe局部使用sharedExamplesFor定义shared examples。能够在它作用域内覆盖全局的shared examples。
断言框架 — Expecta
expect(mapView.nextZoomScale).to.equal(mapView.annotationZoomScaleThreshold)
mock框架 —
OCMock
另外能够參考开源项目 https://github.com/artsy/eigen,学习当中的OCMock
API的使用,框架使用比較简单,看看就懂了,不须要多说。
以下能够看出一个OCMock基本过程:获得OCMockObject -> stub方法 -> 设置expect ->
verify校验运行结果 -> 调用stopMocking
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">
- 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
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 单元測试之XCTest具体解释(一)
原创blog,转载请注明出处 blog.csdn.net/hello_hwc 欢迎关注我的iOS-SDK具体解释专栏 http://blog.csdn.net/column/details/huang ...
- ios单元測试之GHUnit
1.相同创建一个測试的project, 2.通过cocoaPod来下载GHUnit框架,或者到github上下载.由于这个框架是开源的第三方框架. 同一时候加入QuartCore.framework( ...
- [iOS翻译]《iOS7 by Tutorials》在Xcode 5里使用单元測试(上)
简单介绍: 单元測试是软件开发的一个重要方面.毕竟,单元測试能够帮你找到bug和崩溃原因,而程序崩溃是Apple在审查时拒绝app上架的首要原因. 单元測试不是万能的,但Apple把它作为开发工具包的 ...
- 利用Continuous Testing实现Eclipse环境自己主动单元測试
当你Eclipse环境中改动项目中的某个方法时,你可能因为各种原因没有执行单元測试,结果代码提交,悲剧就可能随之而来. 所幸infinitest(http://infinitest.github.io ...
- 在Eclipse中使用JUnit4进行单元測试(0基础篇)
本文绝大部分内容引自这篇文章: http://www.devx.com/Java/Article/31983/0/page/1 我们在编写大型程序的时候,须要写成千上万个方法或函数,这些函数的功能可能 ...
- C语言单元測试
C语言单元測试 对于敏捷开发来说,单元測试不可缺少,对于Java开发来说,JUnit非常好,对于C++开发,也有CPPUnit可供使用,而对于传统的C语言开发,就没有非常好的工具可供使用,能够找到的有 ...
- OpenStack中给wsgi程序写单元測试的方法
在 OpenStack 中, 针对web应用, 有三种方法来写单元測试 1) 使用webob生成模拟的request from __future__ import print_function imp ...
- Android单元測试之JUnit
随着近期几年測试方面的工作慢慢火热起来.常常看见有招聘測试project师的招聘信息.在Java中有单元測试这么一个JUnit 方式,Android眼下主要编写的语言是Java,所以在Android开 ...
- 让你提前认识软件开发(19):C语言中的协议及单元測试演示样例
第1部分 又一次认识C语言 C语言中的协议及单元測试演示样例 [文章摘要] 在实际的软件开发项目中.常常要实现多个模块之间的通信.这就须要大家约定好相互之间的通信协议,各自依照协议来收发和解析消息. ...
随机推荐
- 【8.14校内测试】【DP专题】
nlogn做法,dp[i]表示当前长度为i的最长上升子序列末尾元素的值. 不会写lower_bound(qwq,贴一个以前的好看点的代码 #include<iostream>//使用low ...
- UVA 11947 Cancer or Scorpio 水题
Cancer or Scorpio Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://uva.onlinejudge.org/index.php? ...
- 读书笔记_Effective_C++_条款三十六:绝不重新定义继承而来的non-virtual函数
这个条款的内容很简单,见下面的示例: class BaseClass { public: void NonVirtualFunction() { cout << "BaseCla ...
- EXPLAIN 用法
重点是第二种用法,需要深入的了解. 先看一个例子: mysql> explain select * from t_order; +----+-------------+---------+--- ...
- C#访问修饰符总结[转]
http://blog.csdn.net/tjvictor/article/details/4293354 C#共有五种访问修饰符:public.private.protected.internal. ...
- 制作MACOSX10.10.3/10.9安装启动盘U盘的教程
下载MACOSX 10.10.3/10.9镜像文件,下载地址http://www.chinamac.com/download/mac14032.html1.准备好你需要的大于等于6G以上的U盘或者移动 ...
- 安装arcgis10.5不能启动服务的解决方案转
柚子的二分口粮地 来自:http://www.cnblogs.com/youzi-xuchongyou/p/7218422.html 安装arcgis10.5不能启动服务的解决方案 最近由于公司需要, ...
- CATransform3D的m34值动画
CATransform3D的m34值动画 效果 源码 https://github.com/YouXianMing/Animations // // CATransform3DM34Controlle ...
- tomcat7.0.8的高级应用-apr1.4.2安装
一 windows下安装 直接拷贝tcnative-1.dll到TOMCAT_HOME/bin目录下,启动即可 下载地址 http://archive.apache.org/dist/tomcat/t ...
- wifiphisher使用介绍
1.github地址:https://github.com/sophron/wifiphisher 2.需要安装在kali linux下面 3.需要两个无线网卡 4.安装方法是使用介绍,参考githu ...