Beginning iOS unit testing

  • 220 views
Uploaded on

A guide to getting started to unit testing on iOS for beginners

A guide to getting started to unit testing on iOS for beginners

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
220
On Slideshare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
7
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Beginning iOS UnitTestingMark StruzinskiMonday, April 22, 13
  • 2. What is Unit Testing?Monday, April 22, 13
  • 3. What is Unit Testing?Test small pieces of your code (units)in isolation.Monday, April 22, 13
  • 4. What is Unit Testing?Test small pieces of your code (units)in isolation.Usually, this means testing methods.Monday, April 22, 13
  • 5. Why Unit Testing?Monday, April 22, 13
  • 6. Why Unit Testing?Verify your code does what youexpect.Monday, April 22, 13
  • 7. Why Unit Testing?Verify your code does what youexpect.Makes refactoring much less painfulMonday, April 22, 13
  • 8. Why Unit Testing?Verify your code does what youexpect.Makes refactoring much less painfulForces you to write smaller, moreconcise methodsMonday, April 22, 13
  • 9. 2 Kinds of Unit TestBundlesMonday, April 22, 13
  • 10. 2 Kinds of Unit TestBundlesApplication Test BundleTests fully bootstrapped applicationMonday, April 22, 13
  • 11. 2 Kinds of Unit TestBundlesApplication Test BundleTests fully bootstrapped applicationLogic Test BundleTests classes in isolationMonday, April 22, 13
  • 12. Setting UpMonday, April 22, 13
  • 13. Setting UpNew projects offer an option to create unit tests immediatelyMonday, April 22, 13
  • 14. Setting UpNew projects offer an option to create unit tests immediatelyCreates an Application unit test bundleMonday, April 22, 13
  • 15. Setting UpNew projects offer an option to create unit tests immediatelyIf at all possible, do thisCreates an Application unit test bundleMonday, April 22, 13
  • 16. Setting UpMonday, April 22, 13
  • 17. Setting Up⌘ + U is automatically wired up to run testsMonday, April 22, 13
  • 18. Setting Up⌘ + U is automatically wired up to run testsCan also use Product => Test from the menuMonday, April 22, 13
  • 19. Setting Up⌘ + U is automatically wired up to run testsBuilds and runs test bundle right from your app’s schemeCan also use Product => Test from the menuMonday, April 22, 13
  • 20. Setting UpMonday, April 22, 13
  • 21. Setting UpA unit test bundle is createdalongside your main targetMonday, April 22, 13
  • 22. Setting UpA unit test bundle is createdalongside your main targetA separate Xcode group iscreated for your unit testsMonday, April 22, 13
  • 23. Setting UpA unit test bundle is createdalongside your main targetAll tests should go inside the unittest groupA separate Xcode group iscreated for your unit testsMonday, April 22, 13
  • 24. Setting UpA unit test bundle is createdalongside your main targetAll tests should go inside the unittest groupA separate Xcode group iscreated for your unit testsUsually, 1 unit test class per classunder testMonday, April 22, 13
  • 25. Adding New TestsMonday, April 22, 13
  • 26. Adding New TestsAdd new files to unit testing groupMonday, April 22, 13
  • 27. Adding New TestsAdd new files to unit testing groupNew file is under Cocoa Touch=> Objective-C test case classMonday, April 22, 13
  • 28. Running TestsMonday, April 22, 13
  • 29. Running TestsInitial test run will failMonday, April 22, 13
  • 30. Running TestsInitial test run will failThis is how you know it’s workingMonday, April 22, 13
  • 31. Unit Testing ConventionsMonday, April 22, 13
  • 32. Unit Testing ConventionsAll unit test methods must begin with theword “test”Monday, April 22, 13
  • 33. Unit Testing ConventionsAll unit test methods must begin with theword “test”Unit test methods must have a returntype of voidMonday, April 22, 13
  • 34. Unit Testing ConventionsAll unit test methods must begin with theword “test”Unit test methods must have a returntype of voidUnit test methods cannot haveparametersMonday, April 22, 13
  • 35. Unit Testing ConventionsAll unit test methods must begin with theword “test”Unit test classes are subclasses ofSenTestCaseUnit test methods must have a returntype of voidUnit test methods cannot haveparametersMonday, April 22, 13
  • 36. Unit Testing ConventionsMonday, April 22, 13
  • 37. Unit Testing ConventionsCan have as many unit test classes/files asneeded in a projectMonday, April 22, 13
  • 38. Unit Testing ConventionsTo run some code prior to each unit test,override the setup methodCan have as many unit test classes/files asneeded in a projectMonday, April 22, 13
  • 39. Unit Testing ConventionsTo run some code prior to each unit test,override the setup methodCan have as many unit test classes/files asneeded in a projectTo run some code after each unit test,override the tearDown methodMonday, April 22, 13
  • 40. A Small ExampleMonday, April 22, 13
  • 41. A Small ExampleMonday, April 22, 13
  • 42. A Small ExampleTests use assertion macros to determine pass/failMonday, April 22, 13
  • 43. Assertion Macros#define STAssertNil(a1, description, ...)#define STAssertNotNil(a1, description, ...)#define STAssertTrue(expression, description, ...)#define STAssertFalse(expression, description, ...)#define STAssertEqualObjects(a1, a2, description, ...)#define STAssertEquals(a1, a2, description, ...)#define STAssertEqualsWithAccuracy(left, right, accuracy, description, ...)#define STAssertThrows(expression, description, ...)#define STAssertThrowsSpecific(expression, specificException, description, ...)#define STAssertThrowsSpecificNamed(expr, specificException, aName, description, ...)#define STAssertNoThrow(expression, description, ...)#define STAssertNoThrowSpecific(expression, specificException, description, ...)#define STAssertNoThrowSpecificNamed(expr, specificException, aName, description, ...)#define STFail(description, ...)#define STAssertTrueNoThrow(expression, description, ...)#define STAssertFalseNoThrow(expression, description, ...)(source:SenTestCase.h)Monday, April 22, 13
  • 44. A Small ExampleAnatomy of a Unit TestMonday, April 22, 13
  • 45. A Small Example1. Set up object to be testedAnatomy of a Unit TestMonday, April 22, 13
  • 46. A Small Example1. Set up object to be testedAnatomy of a Unit Test2. Set up any values for use in testing outcomeMonday, April 22, 13
  • 47. A Small Example1. Set up object to be testedAnatomy of a Unit Test2. Set up any values for use in testing outcome3.Assert expected resultMonday, April 22, 13
  • 48. A Small ExampleSetup/Teardown MethodsMonday, April 22, 13
  • 49. A Small ExampleRun before and after each testSetup/Teardown MethodsMonday, April 22, 13
  • 50. A Small ExampleRun before and after each testSetup/Teardown MethodsUse to set up objects and move repetitive code out of testsMonday, April 22, 13
  • 51. A Small ExampleRun before and after each testSetup/Teardown MethodsUse to set up objects and move repetitive code out of testsGood way to ensure state of the object under testMonday, April 22, 13
  • 52. Unit Test FailuresMonday, April 22, 13
  • 53. Unit Test FailuresFailure messages appear in the issue navigator and the consoleMonday, April 22, 13
  • 54. Unit Test FailuresFailure messages appear in the issue navigator and the consoleGood test log message go a long way to determining the issueMonday, April 22, 13
  • 55. Unit Test FailuresFailure messages appear in the issue navigator and the consoleGood test log message go a long way to determining the issueThis becomes even more important in apps with large testsuitesMonday, April 22, 13
  • 56. Questions?Monday, April 22, 13
  • 57. Questions?ThankYou!mark@markstruzinski.com@ski081 (Twitter/ADN)http://markstruzinski.comMonday, April 22, 13
  • 58. Referencehttps://developer.apple.com/library/ios/#documentation/DeveloperTools/Conceptual/UnitTesting/00-About_Unit_Testing/about.htmlMonday, April 22, 13