教学文库网 - 权威文档分享云平台
您的当前位置:首页 > 文库大全 > 小学教育 >

ios单元测试工程建立(2)

来源:网络收集 时间:2026-08-14
导读: OCMock 下面我们来添加OCMock。 ios单元测试工程建立 1,我们只能以静态库的方式来添加OCMock。在GHUnitTest目录下新建 Libraries 目录,该目录是与 Tests 目录平级的。下载静态库文件,解压头文件至该目录下。 文
OCMock

下面我们来添加OCMock。

ios单元测试工程建立

1,我们只能以静态库的方式来添加OCMock。在GHUnitTest目录下新建 Libraries 目录,该目录是与 Tests 目录平级的。下载静态库文件,解压头文件至该目录下。

文件下载:头文件libOCMock.a,framework 文件:OCMock framework ,打开下载好的 ocmock-1.77.dmg,拷贝其中的 Release/Library/Headers/OCMock目录至 Libraries 下。最终目录结构如下:

2,在GHUnitTest工程中新建名为 Libraries 的 group,导入libOCMock.a和目录OCMock,注意 target 是 Tests。

3,设置 Tests 的 Build Setting。让Libray Search Paths 包含 $$(SRCROOT)/Libraries:

ios单元测试工程建立

1

在 Header Search Paths 中增加 $$(SRCROOT)/Libraries,并选中 Recursive 选择框。

2

4,编写OCMock测试。向 Tests 工程中添加名为OCMockSampleTest的 Objective C class。其内容如下:

ios单元测试工程建立

OCMockSampleTest.h

#import <GHUnitIOS/GHUnit.h>

@interface OCMockSampleTest :GHTestCase @end

OCMockSampleTest.m

#import "OCMockSampleTest.h" #import <OCMock/OCMock.h> @implementation OCMockSampleTest // simple test to ensure building, linking, // and running test case works in the project - (void)testOCMockPass {

id mock = [OCMockObjectmockForClass:NSString.class]; [[[mock stub] andReturn:@"mocktest"] lowercaseString]; NSString *returnValue = [mock lowercaseString]; GHAssertEqualObjects(@"mocktest", returnValue, @"Should have returned the expected string."); }

- (void)testOCMockFail {

id mock = [OCMockObjectmockForClass:NSString.class]; [[[mock stub] andReturn:@"mocktest"] lowercaseString]; NSString *returnValue = [mock lowercaseString];

GHAssertEqualObjects(@"thisIsTheWrongValueToCheck",

ios单元测试工程建立

returnValue, @"Should have returned the expected string."); } @end

编译运行,点击 Run,效果如下图。

3

至此,iOS下的OCUnit,GHUnit,OCMock单元测试介绍就到此结束了。当然还有其他一些测试框架,比如google出品的 GTM。 参考资料:

OCMock: /

Unit Testing in Xcode 4- use OCUnit and SenTest instead of GHUnit GHUnit Reference: /gh-unit/ 转自:飘飘白云

ios单元测试工程建立

方法2

Create Test Target

You’ll want to create a separate Test target. Select the project file for your app in the Project Navigator. From there, select the Add Target + symbol at the bottom of the window.

Select iOS, Application, Window-based Application. Select Next.

ios单元测试工程建立

Name it Tests or something similar. Select Finish.

ios单元测试工程建立

Configure the Test Target

Download and copy the GHUnitIOS.framework to your project. Command click on Frameworks in the Project Navigator and select: Add Files to “MyTestable”. (This should automatically add GHUnitIOS.framework to your Link Binary With Libraries

Build Phase for the Tests target.)

ios单元测试工程建立

Select GHUnitIOS.framework and make sure the only the Tests target is selected.

ios单元测试工程建立

We want to enable use of Objective-C categories, which isn’t enabled for static libraries by default. In the Tests target, Build Settings, under Other Linker Flags, add

-ObjC and -all_load.

ios单元测试工程建立

Select and delete the files from the existing Tests folder. Leave the Supporting Files

folder. GHUnit will provide the application delegate below.

…… 此处隐藏:804字,全部文档内容请下载后查看。喜欢就下载吧 ……
ios单元测试工程建立(2).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.jiaowen.net/wenku/41776.html(转载请注明文章来源)
Copyright © 2020-2025 教文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:78024566 邮箱:78024566@qq.com
苏ICP备19068818号-2
Top
× 游客快捷下载通道(下载后可以自由复制和排版)
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能出现无法下载或内容有问题,请联系客服协助您处理。
× 常见问题(客服时间:周一到周五 9:30-18:00)