Having worked with Objective C to write Cocoa-based applications for Mac OS X, I was already familiar with Unit Tests for Objective C using OCUnit. Those of you who need a introduction to test driven development in Objective C shall take a look at Apple's documentation for OCUnit. When it comes to writing Unit Tests for iPhone applications I was wondering about its support in XCode, the iPhone Simulator and unit testing on the iPhone itself.
Check out the Google Toolbox For Mac
iPhone SDK projects are not setup to be compatible with OCUnit out of the box. Somehow OCUnit does not play nicely with the Cocoa Touch framework. Luckily the Mac Unit of Google took some time and extended the SenTestingKit Framework of OCUnit not only to provide more functionality, but also to be compatible with the iPhone SDK.
To write unit tests you first need the Google Toolbox For Mac. Then setting up your project just involves just a few steps:
- Create a new iPhone Target.
- Add several files of the Google Toolbox For Mac.
- Add a new build phase as the last step of your target.
- Edit the Run Script Build Phase and finally run the Unit Tests.
The cool thing is, that the Google Toolbox For Mac has several enhancement to the standard SenTestingKit allowing you to do UI unit testing, automated binding unit testing, log tracking, and unit testing on the iPhone, as well as tools for doing static and dynamic testing of your code.
It supports all of the features described above in the Simulator. In Device mode Google Toolbox For Mac does not support binding unit testing on the iPhone as NSBinding is not supported on the iPhone. To have your tests executed on the phone, make sure your iPhone application's delegate is GTMIPhoneUnitTestDelegate, or calls [GTMIPhoneUnitTestDelegate runTests] when you want your tests to execute. All of your tests should execute as you would expect.
Google Toolbox For Mac has code coverage targets that build with full gcov settings on. You can then use CoverStory to look at the code coverage. CoverStory allows you to easily view the code coverage of your unit tests. The tool has also been created by Google.
Conclusion
I'm happy that it's possible to do TDD when creating an iPhone application. Sad to see that Apple doesn't support OCUnit out of the box for Cocoa Touch - but at least Google provided a very nice extension to OCUnit that allows you to do TDD.
Recent Comments