Unit Testing in iOS - Ninjava Talk

2,217 views

Published on

Talk I gave about unit testing for iPhone to the Ninjava group in Tokyo.

Published in: Technology, Education
0 Comments
6 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,217
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
51
Comments
0
Likes
6
Embeds 0
No embeds

No notes for slide

Unit Testing in iOS - Ninjava Talk

  1. 1. Presented by: Ross Sharrott Long Weekend KK Follow me/us: @rsharrott @long_weekend
  2. 2. <ul><li>IT & Sales Managers </li></ul><ul><li>Entrepreneurs </li></ul><ul><li>Geeks </li></ul><ul><li>Tokyo, Melbourne, New York </li></ul>
  3. 4. <ul><li>B2C Apps </li></ul><ul><li>Contract Development </li></ul><ul><ul><li>Specialize in iOS & Android </li></ul></ul><ul><li>Consulting </li></ul><ul><ul><li>Training </li></ul></ul><ul><ul><li>Mobile Strategy for Enterprise </li></ul></ul><ul><ul><li>App Development strategy & implementation </li></ul></ul>
  4. 5. <ul><li>Practical > Theoretical </li></ul><ul><li>The world is not perfect. </li></ul><ul><li>We are lazy. </li></ul>
  5. 6. <ul><li>Why Unit Test? </li></ul><ul><li>Why don’t iOS Devs Do It? </li></ul><ul><li>The Tools </li></ul><ul><li>Q&A </li></ul>
  6. 7. <ul><li>Why Unit Test? </li></ul><ul><li>Why don’t iOS Devs Do It? </li></ul><ul><li>The Tools </li></ul><ul><li>Q&A </li></ul>
  7. 9. <ul><li>It takes longer to write tests. </li></ul><ul><li>I’m the only developer (and I’m amazing) </li></ul><ul><li>That’s QA’s job (I’m a developer ) </li></ul><ul><li>My code takes input from someone else. </li></ul><ul><li>My boss doesn’t give me enough time to test. </li></ul>
  8. 11. <ul><li>Tests save time. </li></ul><ul><li>Your own code looks like new code after 6 months. </li></ul><ul><li>QA is there to help you find real / obscure problems, not regressions. </li></ul><ul><li>Unit testing is for what you do with inputs. </li></ul><ul><li>Your boss will give you some time to get started, just ask. </li></ul>
  9. 12. <ul><li>Why Unit Test? </li></ul><ul><li>Why don’t iOS Devs Do It? </li></ul><ul><li>The Tools </li></ul><ul><li>The future? </li></ul>
  10. 13. <ul><li>< SDK 2.2 had no unit testing at all. </li></ul><ul><li>Then Unit testing in Xcode, frankly, sucked. </li></ul><ul><ul><li>Sentest reported the unit tests at build time. </li></ul></ul><ul><ul><li>Application State was inaccessible. </li></ul></ul><ul><ul><li>Logic tests only. </li></ul></ul><ul><ul><li>Hard to debug tests. </li></ul></ul><ul><ul><li>IDE integration was poor – non-existent. </li></ul></ul>
  11. 14. <ul><li>Functional & Application testing basically did not exist. </li></ul><ul><li>Continuous Integration was nigh impossible. </li></ul><ul><li>We are all spoiled by the tools in most other worlds – selenium, _test, _cover, _mock </li></ul>
  12. 15. <ul><li>GHUnit </li></ul><ul><ul><li>Made testing on the device & with state possible </li></ul></ul><ul><li>FoneMonkey </li></ul><ul><ul><li>Made selenium like testing possible </li></ul></ul><ul><li>Various Others… </li></ul><ul><li>The problem: </li></ul><ul><ul><li>No IDE integration, somewhat annoying to run. </li></ul></ul>
  13. 17. <ul><li>Why Unit Test? </li></ul><ul><li>Why don’t iOS Devs Do It? </li></ul><ul><li>The Tools </li></ul><ul><li>Q&A </li></ul>
  14. 18. <ul><li>Unit Testing: </li></ul><ul><ul><li>GHUnit </li></ul></ul><ul><ul><li>OCUnit </li></ul></ul><ul><ul><li>Continuous Integration </li></ul></ul><ul><li>UI & Integration: </li></ul><ul><ul><li>Automator, FoneMonkey </li></ul></ul><ul><ul><li>iCuke, UISpec, etc. </li></ul></ul>
  15. 19. <ul><li>Pros </li></ul><ul><ul><li>Running a subset of tests. </li></ul></ul><ul><ul><li>Nice viewer to see the test results. </li></ul></ul><ul><ul><li>Setup/Teardown on class or methods </li></ul></ul><ul><ul><li>Can run on Continuous Integration </li></ul></ul><ul><ul><li>Can read Sentest so you can switch later </li></ul></ul><ul><li>Con </li></ul><ul><ul><li>Additional setup, required for every project </li></ul></ul><ul><ul><li>New versions of Xcode can break this setup </li></ul></ul>
  16. 21. <ul><li>Pros </li></ul><ul><ul><li>Easy to run with Xcode (CMD + U) </li></ul></ul><ul><ul><li>Easy to create new tests (CMD + N) </li></ul></ul><ul><ul><li>(Almost) no setup </li></ul></ul><ul><ul><li>Can be run on Continuous Integration </li></ul></ul><ul><li>Cons </li></ul><ul><ul><li>No class setup/teardown </li></ul></ul><ul><ul><li>Must run the entire suite every time </li></ul></ul>
  17. 22. <ul><li>New test: CMD+N </li></ul><ul><li>Objective-C Test Case </li></ul><ul><li>You get something like this: </li></ul><ul><li>- (void)testAppDelegate </li></ul><ul><li>{ </li></ul><ul><li>id yourApplicationDelegate = [[UIApplication sharedApplication] delegate]; </li></ul><ul><li>STAssertNotNil(yourApplicationDelegate, @&quot;UIApplication failed to find the AppDelegate&quot;); </li></ul><ul><li>} </li></ul>
  18. 23. <ul><li>Deinflector *bulldog = [[Deinflector alloc] init]; </li></ul><ul><li>NSArray *results; </li></ul><ul><li>NSString* msg = @&quot;expected deinflected string did not match!”; </li></ul><ul><li>// Check deinflection types </li></ul><ul><li>results = [bulldog go:@&quot; かけた &quot;]; </li></ul><ul><li>STAssertEqualStrings([[results objectAtIndex:0] word], @&quot; かけた &quot;, @&quot;1st %@&quot;); </li></ul><ul><li>STAssertEqualStrings([[results objectAtIndex:1] word], @&quot; かける &quot;, @&quot;2nd %@&quot;, msg); </li></ul><ul><li>STAssertEqualStrings([[results objectAtIndex:2] word], @&quot; かく &quot;, @&quot;3rd %@&quot;, msg); </li></ul>
  19. 24. <ul><li>CMD + U </li></ul><ul><li>Xcode will build & run the test suite </li></ul><ul><li>You get in console: </li></ul><ul><li>Test Case '-[CardTagTest testUpdateLevelCounts]' passed (0.345 seconds). </li></ul><ul><li>Test Suite 'CardTagTest' finished at 2011-10-20 05:44:39 +0000. </li></ul><ul><li>Executed 3 tests, with 0 failures (0 unexpected) in 1.528 (1.529) seconds </li></ul>
  20. 25. <ul><li>Good News: </li></ul><ul><ul><li>It can be done using xcodebuild & the simulator </li></ul></ul><ul><ul><li>Most of the popular CI’s can work </li></ul></ul><ul><li>Bad News: </li></ul><ul><ul><li>Must have xcode installed – so need a mac </li></ul></ul><ul><ul><li>OCUnit doesn’t put out junit output by default </li></ul></ul><ul><ul><ul><li>Tools exist for this </li></ul></ul></ul><ul><ul><li>Not a lot of Code Coverage tools available yet </li></ul></ul>
  21. 26. <ul><li>Automator </li></ul><ul><ul><li>Test automator, uses javascript </li></ul></ul><ul><ul><li>Included in Instruments </li></ul></ul><ul><li>iCuke </li></ul><ul><ul><li>New to the scene, seems like a good start but we haven’t jumped in yet. </li></ul></ul><ul><ul><li>Future (testing) proof your projects: Set accessibility tags in your apps </li></ul></ul>
  22. 27. Follow me/us: @rsharrott @long_weekend

×