I've written a couple of posts on my weblog describing how to do unit
testing — and test-driven development — of user interfaces using the
Cocoa application framework that's part of Mac OS X, and the OCUnit
unit testing framework that's included with the Xcode IDE that's also
included with Mac OS X. I thought that at least some members of the
TDD, XP, and test-first UI communities would find them interesting.
Unit testing Cocoa user interfaces: Target-Action
http://chanson.livejournal.com/148204.html
Unit testing Cocoa user interfaces: Cocoa Bindings
http://chanson.livejournal.com/172390.html
(In the interest of full disclosure, I work on Xcode at Apple - though
I've been a member of all three of these communities for several times
as long as I've worked on Xcode or even at Apple, and I'm not here to
evangelize. I don't speak for my employer; I'm just a tools developer
who's also really passionate about agile development practices and the
ways they can be applied to improve quality and predictability.)
Cocoa has some relatively unique qualities as an application framework
that make it easy to do test-driven development with: You can follow
a "trust, but verify" approach to test-driving your user interface,
where can write tests to specify (and verify) that your views and
controllers are connected to your model correctly and trust that so
long as it's the case, the framework will behave correctly.
Furthermore, it's built on the Objective-C language, which supports a
lot of introspection features which you can leverage to make writing
your tests easier without violating encapsulation too much in the code
under test.
The upshot of all this is that you don't need to worry about injecting
events to ensure that the right thing happens when a button is
clicked, you don't have to worry about implementing "reveal" methods
to eyeball your interfaces since you're still doing all of your
interface design in Interface Builder (which isn't a code generator
and doesn't have their major drawbacks), or anything cumbersome like
that.
For anyone doing Mac development, I hope these posts provide a useful
starting point for writing tests for your user interfaces. For anyone
not doing Mac development, I hope these provide a useful perspective
on your own frameworks and ways that you may be able to approach test-
driving your user interface development in the future. I'm sure
Cocoa's not the only MVC-oriented framework where these techniques can
be applied; I'd love to hear from others who are doing similar things
with other frameworks and languages.
-- Chris