Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

iOS Unit Testing

6,583 views

Published on

A talk about unit testing for iOS apps. Part rambling introduction to test driven development, part examples of certain types of tests for iOS, and a brief mention of writing your tests using Kiwi.

Published in: Technology, Education
  • Be the first to comment

iOS Unit Testing

  1. 1. An introduction to unit testing for iOS applicationsSTEWART GLEADOW @stewgleadow Thoughtwor
  2. 2. UnitTesting
  3. 3. Unit TestingOCUnit
  4. 4. Unit TestingOCUnit Kiwi
  5. 5. Unit TestingOCUnit Kiwi Comma nd Line
  6. 6. Have we been here before?http://sgleadow.github.com/ talks.html#frank
  7. 7. http://sgleadow.github.com/ talks.html#agileios
  8. 8. UI Integration Unithttp://sgleadow.github.com/ talks.html#agileios
  9. 9. UI Integration OCUnit GHUnit GTM Unithttp://sgleadow.github.com/ talks.html#agileios
  10. 10. UI Integration Kiwi Cedar OCUnit GHUnit GTM Unithttp://sgleadow.github.com/ talks.html#agileios
  11. 11. Why test in units?We only care if thewhole app works.
  12. 12. Write your tests first. Use tests to design your code
  13. 13. Write your tests first. Use tests to design your code “Classes typically resist the transition from one user to two, then the rest are easy” - Kent Beck, c2.wiki
  14. 14. Fast feedback
  15. 15. RedGreen Refact
  16. 16. but we’re making iOS apps
  17. 17. THE TOOLS AREN’T GOOD ENOUGH
  18. 18. THE TOOLS AREN’T GOOD ENOUGH there are too many frameworks to fit here
  19. 19. NO ONE PAYS YOU TO WRITE TESTS
  20. 20. NO ONE PAYS YOU TO WRITE TESTS they pay you to write software that works
  21. 21. SHOULD YOU ALWAYS TEST EVERYTHING ?
  22. 22. SHOULD YOU ALWAYS TEST EVERYTHING ? “it depends”, says the consultant
  23. 23. Fibonacci, really?
  24. 24. Apple designaward here we come
  25. 25. GettingStarted
  26. 26. Getting Started or, if you’ve already started...http://sgleadow.github.com/blog/2011/10/30/adding-unit-tests-to-an-existing-ios-project
  27. 27. OCUnit
  28. 28. OCUnitApplication Logic
  29. 29. #import <SenTestingKit/SenTestingKit.h>
  30. 30. @interface FibCounterTests : SenTestCase@end
  31. 31. - (void)testExample { STFail(@"epic fail");}
  32. 32. - (void)setUp {}- (void)tearDown {}
  33. 33. Controller Tests
  34. 34. - (void)test_the_fibonacci_number_is_zero_after_reset {}
  35. 35. - (void)test_the_fibonacci_number_is_zero_after_reset { ourCounter = [[UILabel alloc] init];}
  36. 36. - (void)test_the_fibonacci_number_is_zero_after_reset { ourCounter = [[UILabel alloc] init]; controller = [[SGViewController alloc] initWithNibName:nil bundle:nil];}
  37. 37. - (void)test_the_fibonacci_number_is_zero_after_reset { ourCounter = [[UILabel alloc] init]; controller = [[SGViewController alloc] initWithNibName:nil bundle:nil]; controller.counter = ourCounter;}
  38. 38. - (void)test_the_fibonacci_number_is_zero_after_reset { ourCounter = [[UILabel alloc] init]; controller = [[SGViewController alloc] initWithNibName:nil bundle:nil]; controller.counter = ourCounter; [controller resetCounter];}
  39. 39. - (void)test_the_fibonacci_number_is_zero_after_reset { ourCounter = [[UILabel alloc] init]; controller = [[SGViewController alloc] initWithNibName:nil bundle:nil]; controller.counter = ourCounter; [controller resetCounter]; STAssertEqualObjects(ourCounter.text, @"0", nil);}
  40. 40. - (void)test_the_fibonacci_number_is_zero_after_reset { ourCounter = [[UILabel alloc] init]; controller = [[SGViewController alloc] initWithNibName:nil bundle:nil]; controller.counter = ourCounter; [controller resetCounter]; STAssertEqualObjects(ourCounter.text, @"0", nil);}
  41. 41. (null) should be equal to 0
  42. 42. WRITE THESIMPLEST CODE TO MAKE THE TEST PASS
  43. 43. WRITE THE SIMPLEST CODE TO MAKE THE TEST PASS- (void)resetCounter { self.counter.text = @"0";}
  44. 44. WRITE THE SIMPLEST CODE TO MAKE THE TEST PASS- (void)resetCounter { self.counter.text = [NSStringstringWithFormat:@"%u", current];}
  45. 45. TEST DRIVE OUR FIBONACCI ALGORITHM
  46. 46. TEST DRIVE OUR FIBONACCI ALGORITHM 210, 1, 1, 2, 3, 5, 8, 13,
  47. 47. TEST DRIVE OUR FIBONACCI ALGORITHM 21 0, 1, 1, 2, 3, 5, 8, 13,test_the_first_fibonacci_number_is_zerotest_the_seond_fibonacci_number_is_onetest_the_third_fibonacci_number_is_one
  48. 48. TEST DRIVE OUR FIBONACCI ALGORITHM 21 0, 1, 1, 2, 3, 5, 8, 13,test_the_first_fibonacci_number_is_zero -test_the_seond_fibonacci_number_is_onetest_the_third_fibonacci_number_is_one
  49. 49. TEST DRIVE OUR FIBONACCI ALGORITHM 21 0, 1, 1, 2, 3, 5, 8, 13,test_the_first_fibonacci_number_is_zero -test_the_seond_fibonacci_nu [controllermber_is_one next];test_the_third_fibonacci_number_is_one
  50. 50. TEST DRIVE OUR FIBONACCI ALGORITHM 21 0, 1, 1, 2, 3, 5, 8, 13,test_the_first_fibonacci_number_is_zero -test_the_seond_fibonacci_nu [controllermber_is_one next]; [controllertest_the_third_fibonacci_nu next]; [controllermber_is_one next];
  51. 51. 0 should be equal to 1
  52. 52. DO THESIMPLESTTHING TO MAKE IT
  53. 53. KEEP BUILDING YOUR TESTS UNTIL YOU HAVE THE ENTIRE
  54. 54. MAKE IT AWESOME
  55. 55. MAKE IT AWESOME (aka - there is a step 3)
  56. 56. MAKE IT AWESOME (aka - there is a step 3)
  57. 57. (defun fib (n) (if (< n 2) n (+ (fib (- n 1)) (fib (- n 2))))) MAKE IT AWESOME (aka - there is a step 3)
  58. 58. SGViewController- (void)next;- (void)resetCounter;
  59. 59. SGViewController- (void)next;- (void)resetCounter; Fibber - (NSUInteger)current; - (NSUInteger)next; - (NSUInteger)reset;
  60. 60. UIButton SGViewController- (void)next; UILabel- (void)resetCounter; Fibber - (NSUInteger)current; - (NSUInteger)next; - (NSUInteger)reset;
  61. 61. MoreController Tests
  62. 62. It is possible to test yourUIViewControllerlifecycle and XIBfrom your “unit” tests
  63. 63. controller. view* check for non-nil outlets *
  64. 64. [button actionsForTarget:controllforControlEvent:UIControlEventTochUpInside] for correct action * check selectors *
  65. 65. [controllerdidReceiveMemoryWarning] * check for nil outlets *
  66. 66. WHAT ABOUT KIWI?
  67. 67. RSpec Style vs xUnit Style
  68. 68. SPEC_BEGIN(SGViewControllerSpec)SPEC_END
  69. 69. SPEC_BEGIN(SGViewControllerSpec) describe(@"fibonacci number", ^{ context(@"when reset", ^{ }); });SPEC_END
  70. 70. SPEC_BEGIN(SGViewControllerSpec) describe(@"fibonacci number", ^{ context(@"when reset", ^{ beforeEach(^{ // set up code }); it(@"should be zero", ^{ [controller resetCounter]; [[[ourCounter text] should] equal:@"0"]; }); }); });SPEC_END
  71. 71. Is it just a change of wording?
  72. 72. Kiwi comes with mocksand stubs built in
  73. 73. UIButton SGViewController- (void)next; UILabel- (void)resetCounter; Fibber - (NSUInteger)current; - (NSUInteger)next; - (NSUInteger)reset;
  74. 74. TESTS AREEFFECTIVELYTESTING THE PLUMBING
  75. 75. TESTS ARE EFFECTIVELY TESTING THE PLUMBINGit(@"should update label using fibber", ^{ id ourFibber = [Fibber mock]; [[ourFibber should] receive:@selector(next)andReturn:theValue(3)]; controller.fibber = ourFibber; [controller next]; [[[ourCounter text] should] equal:@"3"];});
  76. 76. WHAT ABOUTRUNNING TESTS FROM THECOMMAND LINE
  77. 77. xcodebuild is your friend...
  78. 78. xcodebuild is your friend... until it unfriends you
  79. 79. xcodebuild -projectFibCounter.xcodeproj -targetFibCounterTests -configurationDebug -sdkiphonesimulator
  80. 80. and hack this file: "${SYSTEM_DEVELOPER_DIR}/Tools/RunUnitTests"
  81. 81. and hack this file: "${SYSTEM_DEVELOPER_DIR}/Tools/ RunUnitTests"http://longweekendmobile.com/2011/04/17/xcode4-running-application-tests-from-the-command-line-in-ios/
  82. 82. Resources
  83. 83. ResourcesiDeveloper TV Unit TestingCourse (Graham Lee) http://ideveloper.tv/video/ unittestingcourse.html
  84. 84. ResourcesiDeveloper TV Unit TestingCourse (Graham Lee) http://ideveloper.tv/video/ unittestingcourse.htmlTDD for iOS Tutorials (DougSjoquist) http://www.sunetos.com/items/ 2011/10/24/tdd-ios-part-1
  85. 85. ResourcesiDeveloper TV Unit TestingCourse (Graham Lee) http://ideveloper.tv/video/ unittestingcourse.htmlTDD for iOS Tutorials (DougSjoquist) http://www.sunetos.com/items/ 2011/10/24/tdd-ios-part-1Prag Prog Magazine http://pragprog.com/magazines/2010-07/ tdd-on-iphone-diy
  86. 86. ResourcesiDeveloper TV Unit TestingCourse (Graham Lee) http://ideveloper.tv/video/ unittestingcourse.htmlTDD for iOS Tutorials (DougSjoquist) http://www.sunetos.com/items/ 2011/10/24/tdd-ios-part-1Prag Prog Magazine http://pragprog.com/magazines/2010-07/ tdd-on-iphone-diyShameless Shameless Plug http://sgleadow.github.com
  87. 87. THANKS
  88. 88. STEWART GLEADOW Thoughtworks sgleadow.github.c Talks: om/talksTwitter: @stewgleadow Email: sgleadow@though

×