`

iOS + XCode 4 + GHUnit = Mobile TDD+Continuous testing

 
阅读更多

Last time I explained why I think doing TDD for mobile is imperative, and why I do it. But now it’s time to get technical, and explain to you how to set up, GHUnit in XCode 4 and run unit tests, not only in the iPhone and iPad simulator but also on your own physical device!, it’s in text and images but also in video form on YouTube.

 

But wait….

Before I begin, I want to make one thing very clear, the difference between code unit testing and UItesting. Unfortunately, UI development can be hard to do in a TDD fashion. Especially when you want to test UI components. e.g. When I send a TouchEvent will the View respond and trigger my method in my controller.
My advice; don’t do UI testing with a Unit testing framework (OCUnit, JUnit, GHUnit), do it with for example the iOS automation API, which has been created specifically to test those UI components. I’ll also get back to you in a later post on how to do UI testing on Android.

What do you test with Unit testing frameworks? Well you test just the code, nothing more. Model and Controller code, not the View! You might need the help of a mocking framework to make it testable, because the view is missing and needs to be wired up for the controller and model to work properly, or it might need other controllers etc..

Let’s begin setting up!

With that in mind, let’s set up our own iPhone XCode 4 project! add GHunit, create a test and run it in the simulator or your own iOS device, iPhone, iPod Touch or iPad.

1. First of all download the GHUnitIOS version (0.4.28 at this time) at https://github.com/gabriel/gh-unit/archives/master
2. Unpack the downloaded zip file somewhere in your home directory, you should get the GHUnitIOS.framework directory

NOTE; I first placed it in /Developer/Library/Frameworks but XCode 4 didn’t like it and when compiling it could not find the header files, therefore I placed them somewhere in my home directory (e.g. /Users/rvanloghem/Development/Frameworks/GHUnitIOS.framework)



3. For example purposes, I’m choosing a normal, navigation-based application but you might have an existing project.
- NOTE; Don’t check the Include Unit Tests, because we are going to supply our own unit testing framework and not rely on OCUnit, which is supplied by default in XCode



 4. In your XCode project settings (blue root icon in the tree browser) add a Target which you can call Tests, i usually base the project on a simple View-based Application (a Target named Tests will be added + a folder named Tests with all the Tests target files in them)


 5. Next go back to the Tests target (click on the Tests Target) and add the GHUnitIOS.framework which you downloaded and unpacked in step 1 and 2. (click on the Build Phases tab, open up the Link Binary with Libraries, hit the + button, click Add Other and navigate+select the GHUnitIOS.framework directory on your filesystem)

6. Optional, but nice, move the GHUnitIOS.framework in your Tree to the Frameworks folder, to tidy things up



 7. Set the -ObjC and -all_load in the other linker flags on the Tests Target (Select the Tests Target, select the Build Settings, search for other linker flags, and add the 2 flags)



 8. Now you can delete some files which are not necessary, all the files in the Tests folder. (Note, Not! the Supporting Files folder as well, just the files!)

9. Delete the main.m file in the Supporting Files folder



 10. In the Tests-Info.plist file (again, in the Supporting Files folder), clear out the Main nib file base name value



 Time to create the GHUnit test runner, which will scan for our Unit Test Cases and run them.

11. Create an Objective-C class in the Tests folder named GHUnitIOSTestMain and make sure it is only! added to the Tests target



 12. You can delete the GHUnitIOSTestMain.h file

13. Copy and paste source code from (http://github.com/gabriel/gh-unit/blob/master/Project-IPhone/GHUnitIOSTestMain.m) in your GHUnitIOSTestMain.m file

And now it’s time to create our own test case which will fail ;)

14. Again create an Objective-C class in the Tests folder and as this is an example, name it ExampleTest, also make sure it is added to the Tests Target only!
15. You can delete the ExampleTest.h file
15. Copy and paste the next piece of code which is 99% copy/pasted from the example code from GHUnit (http://gabriel.github.com/gh-unit/_examples.html)

 

#import <GHUnitIOS/GHUnit.h>
// For Mac OS X
//#import <GHUnit/GHUnit.h>
 
@interface ExampleTest : GHTestCase { }
@end
 
@implementation ExampleTest
 
- (BOOL)shouldRunOnMainThread {
    // By default NO, but if you have a UI test or test dependent on running on the main thread return YES
    return NO;
}
 
- (void)setUpClass {
    // Run at start of all tests in the class
}
 
- (void)tearDownClass {
    // Run at end of all tests in the class
}
 
- (void)setUp {
    // Run before each test method
}
 
- (void)tearDown {
    // Run after each test method
}  
 
- (void)testFoo {
    NSString *a = @"foo";
    GHTestLog(@"I can log to the GHUnit test console: %@", a);
 
    // Assert a is not NULL, with no custom error description
    GHAssertNotNULL(a, nil);
 
    // Assert equal objects, add custom error description
    NSString *b = @"bar";
    GHAssertEqualObjects(a, b, @"A custom error message. a should be equal to: %@.", b);
}
 
- (void)testBar {
    GHAssertTrue(TRUE, @"Yes it worked");
}
 
@end

 Right, ready to run!

16. Launch your Tests target (iTunes-like play button arrow) and run it against the simulator-scheme and your Unit test app should start



 17. Now in the app in the simulator hit the blue run button and your tests will execute. (and testFoo will fail!, you can click on it to see why it failed)



 And now it’s time to fix it…

18. Change theNSString *b = @"bar";in the testFoo method toNSString *b = @"foo";
19. Run the Test app again and re-run the test and they should be green or in this case black, which means your tests are ok

Showing the power of GHUnit

20. Run the app against your own iOS device-scheme. (Select iOS-device-scheme and click the iTunes like run button)



 Why i chose GHUnit over OCUnit

IMHO, this shows the real power of the GHUnit testing framework, not only does it run in the simulator but it also runs on your own iPhone, iPad, etc. Whereas OCUnit can only run as part of your build on your own machine, not on your phone and not in the simulator, this for me, is a big dealbreaker.
The closer you can get your unit tests running against a real-world environment the better it will be. Why? Because you are making use of the real devices processor (not the intel x86), real memory management, or lack there-of, the real API’s etc. If my Unit tests run on my phone, i’m 99.999% certain that the code under test will actually run on, yes, you guessed it, my phone.

There is of-course a downside to GHUnit, OCUnit (bundled with XCode) can be run automatically prior to you compiling your own app, this makes getting feedback about regression a lot faster, GHUnit is something which you have to run manually, in this case. But to solve that problem, or at least make it a whole lot better, we can use continuous integration aka build server to do the auto-running-of-unit-tests for us. There is a very nice blog post which compares various iOS unit testing frameworks.

So what is next? Well, the topic for my next blog and video in this series is hooking up a XCode project + GHUnit to Jenkins (or Hudson for the Oracle minded people out there).

转载:http://blog.xebia.com/2011/03/23/ios-xcode-4-ghunit-mobile-tddcontinuous-testing-part-2-of-n/

 

  • 大小: 37.3 KB
  • 大小: 257.8 KB
  • 大小: 175.7 KB
  • 大小: 451.2 KB
  • 大小: 99.9 KB
  • 大小: 100.5 KB
  • 大小: 73.8 KB
  • 大小: 79 KB
  • 大小: 174.9 KB
  • 大小: 235.9 KB
  • 大小: 271.8 KB
分享到:
评论
1 楼 jlees 2017-11-03  
Best mobile app testing tool pcloudy (www.pcloudy.com).

相关推荐

    全新版本全新工具-进击Apple IOS 13开发实战 SwiftUI 5.1+Xcode11 SwiftUI实战教程

    全新版本全新工具-进击Apple IOS 13的SwiftUI开发实战,使用最新的Mac OS X集成开发工具Xcode11进行SwiftUI构建用户界面,让同学们最近的距离接触IOS与用户界面开发。课程分为了SwiftUI开发的基础部分与进阶部分,...

    macos-lion-dp4-10.8+ios6+xcode4.5.zip

    标题中的"macos-lion-dp4-10.8+ios6+xcode4.5.zip"揭示了这个压缩包包含的是苹果操作系统Mac OS X Lion的第四个开发者预览版(DP4)以及iOS 6和Xcode 4.5的相关内容。Mac OS X Lion是苹果在2011年推出的操作系统,它...

    最新iOS11开发教程swift4+xcode9

    最新iOS11开发教程swift4+xcode9,iOS 11开发概述 iOS 11新特性 编写第一个iOS 11应用 4 创建iOS11项目 4 运行iOS11程序 7 iOS11模拟器介绍 iOS11Main.storyboard文件编辑界面 编写第一个iOS11代码Hello,World 15 ...

    Windows+VMware+MacOS10.8.2+Xcode环境搭建

    Xcode是Apple官方的开发工具,包含了iOS和macOS的集成开发环境,对于iOS开发尤为重要。你可以从App Store下载Xcode,但由于我们使用的是较旧的MacOS版本,可能需要下载特定版本的Xcode,这可以通过Apple Developer...

    12.1的ios的xcode包

    【标题】"12.1的iOS的Xcode包"主要指的是针对苹果操作系统iOS 12.1版本的开发工具Xcode的一个特定版本。Xcode是Apple官方为开发者设计的一款集成开发环境(IDE),用于创建iOS、macOS、watchOS以及tvOS的应用程序。...

    iOS 13.6 xcode 真机包iOS 13.6 xcode 真机包

    4. **Xcode真机包**: 这个术语可能指的是一个包含了特定iOS版本(如13.6)的开发者配置文件或固件更新,用于使Xcode支持在这些版本的设备上进行真机测试。有时,开发者可能需要这样的包来解决版本兼容性问题或者获取...

    xcode快捷键大全

    xcode快捷键大全:偏好设置 command+, 清空缓存 可设 隐藏xcode command+h 隐藏其它 command+option+h 显示全部 可设 退出xcode command+q ===============文件相关================ 新建项目 command+shift+n 新建...

    iOS xcode -lstdc++.6.0.9 lib包下载

    临时解决方案:因为libstdc++/libstdc++.6/libstdc++.6.0.9是从Xcode10,ios12开始移除的,所以可以从Xcode之前版本(如Xcode9.4.1)的Xcode中复制迁移到Xcode10中,开发者只需要将Xcode9.4.1中的真机和模拟器两个...

    ios离线打包 +xcode11.4文件打包.docx

    在iOS应用开发中,离线打包是一种常见的方法,特别是对于H5+工程,它允许开发者在没有网络连接的情况下构建和部署应用。本文主要探讨如何在Xcode 11.4中进行离线打包,以及相关的配置步骤。 首先,离线打包主要分为...

    iOS11.2 Xcode真机支持文件

    在iOS开发过程中,Xcode是苹果官方推荐的集成开发环境(IDE),用于编写、调试和发布iOS应用程序。本文将深入探讨“iOS11.2 Xcode真机支持文件”这一主题,以及它对开发者的重要性。 首先,让我们了解什么是Xcode...

    ios+opencv 图像卡通化

    iOS+opencv+xcode实现摄像头视频流的卡通化

    iOS15.4 Xcode13.4 DevelopDiskImage

    iOS15.4是苹果公司针对iPhone和iPad设备推出的一个重要更新,而Xcode13.4则是配合这个系统版本的开发者工具,用于构建、测试和发布iOS应用程序。这两个组件的组合为开发者带来了许多新的特性和改进,下面我们将详细...

    iOS 15.1 xcode包

    **iOS 15.1 Xcode包详解** iOS 15.1是Apple在其iOS操作系统中的一个重要更新,它带来了多项新特性和改进,为开发者和用户提供了更好的体验。Xcode作为Apple官方的集成开发环境(IDE),是开发iOS、macOS、watchOS...

    IOS11SDK xcode8调试IOS11

    xcode8调试IOS11 Shift+Command+G进入 /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/DeviceSupport 把解压后的文件复制进去。

    iOS 15.3 Xcode 开发包

    **iOS 15.3 Xcode 开发包详解** iOS 15.3是Apple针对其移动操作系统的一次重大更新,旨在提供更好的性能、增强的安全性以及一系列新特性和改进。Xcode作为苹果官方的集成开发环境(IDE),是开发者进行iOS应用开发...

    win7+虚拟机9+mac lion 10.8+XCODE 4.5 安装说明

    这几天安装好了IOS开发软件,所以分享下安装过程。 win7+虚拟机9+mac lion 10.8+XCODE 4.5 安装说明 非常详细 里面包含所有安装软件的下载地址和说明。

    iOS 9.0 Xcode开发包

    在iOS应用开发中,Xcode是苹果官方提供的集成开发环境(IDE),用于编写、调试和发布iOS和macOS应用程序。当遇到"Could not find developer disk image"这样的错误时,这通常意味着Xcode无法找到适用于特定iOS版本...

    Xcode缺失库 libstdc++.zip

    在iOS和macOS开发中,Xcode是Apple官方提供的集成开发环境(IDE),用于编写Objective-C、Swift以及C++等语言的应用程序。然而,有时在使用Xcode进行项目开发时,可能会遇到“缺失库”的问题,例如“libstdc++”就是...

Global site tag (gtag.js) - Google Analytics