简体   繁体   中英

Implementing Simple Unit Tests with Objective-C

I've recently been loving unit testing after being introduced to the idea in Programming Ruby. I knew there was a preference, 'Include Unit Tests' in Xcode, so I tried it. First of all it was very hard to find documentation for the framework and secondly it's a hassle to add unit tests to a Foundation command-line tool. With all of it's separate schemes and targets etc.

This got me thinking about writing a extremely minimal and easy to include unit testing framework, mainly for educational purposes.

My idea is, you would write a startup and cleanup method and methods beginning with test. Then I would write 5-6 assert functions. (Like most frameworks) Then in main() you would say something like:

[MyAppTests test];

Or something similar. And then all the test methods would be run and the errors, or successes logged. Then I could build on that in future.

My questions are, how would one implement that you could run all methods beginning with test in Objective-C using Foundation and maybe the C standard library? And what would be an optimal / minimal design for such a thing?

Use the Objective-C Runtime Reference to see how to get lists of method names. You could then filter out the setup and cleanup methods or only include methods that start with "test", for example. I'm pretty sure that this is what the built in SenTest framework does internally.

http://developer.apple.com/library/mac/#documentation/cocoa/Reference/ObjCRuntimeRef/Reference/reference.html

When I make regular use of C functions like these I often prefer to wrap them in a utility class or, when appropriate, a category.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM