9. Excuses
• “I don’t need it”
• “Deadline is too tight”
• “It’s not applicable for this [project|
class|method]”
• “Unit testing in Xcode sucks”
10. Reasons to unit test
• Fix bugs early
• Refine design
• Easier to make changes
• Instant gratification :)
• Useful documentation
• Reduce testing time
20. Writing tests with
OCUnit
• Each Test Suite is a class that inherits
from SenTestCase
• Each Test Case is a method with prefix
test
• setUp & tearDown are optional
22. Console output
2011-12-09 12:43:01.394 HelloWorld[2858:fb03] Applications are expected to have a
root view controller at the end of application launch
Test Suite 'All tests' started at 2011-12-09 11:43:01 +0000
Test Suite '/Users/hermespique/Library/Developer/Xcode/DerivedData/HelloWorld-
ezilismrgmrzecbbsndapbyeczre/Build/Products/Debug-iphonesimulator/
HelloWorldTests.octest(Tests)' started at 2011-12-09 11:43:01 +0000
Test Suite 'HelloWorldTests' started at 2011-12-09 11:43:01 +0000
Test Case '-[HelloWorldTests testExample]' started.
/Users/hermespique/Documents/workspace/HelloWorld/HelloWorldTests/
HelloWorldTests.m:33: error: -[HelloWorldTests testExample] : Unit tests are not
implemented yet in HelloWorldTests
Test Case '-[HelloWorldTests testExample]' failed (0.000 seconds).
Test Suite 'HelloWorldTests' finished at 2011-12-09 11:43:01 +0000.
Executed 1 test, with 1 failure (0 unexpected) in 0.000 (0.000) seconds
Test Suite '/Users/hermespique/Library/Developer/Xcode/DerivedData/HelloWorld-
ezilismrgmrzecbbsndapbyeczre/Build/Products/Debug-iphonesimulator/
HelloWorldTests.octest(Tests)' finished at 2011-12-09 11:43:01 +0000.
Executed 1 test, with 1 failure (0 unexpected) in 0.000 (0.000) seconds
Test Suite 'All tests' finished at 2011-12-09 11:43:01 +0000.
Executed 1 test, with 1 failure (0 unexpected) in 0.000 (0.001) seconds
27. GHUnit
• The other Unit Testing framework for
Obj-C
• Open-source: github.com/gabriel/gh-
unit
• GUI!
• No Xcode native support
• Compatible with OCUnit
28.
29. #import <GHUnitIOS/GHUnit.h>
@interface ExampleTest : GHTestCase
@end
@implementation ExampleTest
- (BOOL)shouldRunOnMainThread {
return NO;
}
- (void)setUpClass {
// Run at start of all tests in the class
}
- (void)setUp {
// Run before each test method
}
- (void)tearDown {
// Run after each test method
}
- (void)tearDownClass {
// Run at end of all tests in the class
}
- (void)testFoo {
NSString *a = @"foo";
GHAssertNotNil(a, nil);
}
@end
35. 2. Add
GHUnitiOS.framework
1. Download from github & unzip
2. > cd gh-unit/Project-iOS
3. > make
or
Download the latest stable framework from
github downloads
43. mock object
=
simulated object that
mimics the behavior
of a real object in
controlled ways
44. When to mock an
object?
• supplies non-deterministic results (ie:
sensors)
• has states that are difficult to create or
reproduce (ie: a network error)
• is slow (ie: database)
• does not yet exist or may change behavior
• to avoid writing “test code”
45. OCMock
• De-facto mocking framework for Obj-C
• Open-source: github.com/erikdoe/
ocmock
• mock objects on the fly via the
trampoline pattern
• Complementary with OCUnit & GHUnit