7. Why Unit Testing?
Verify your code does what you
expect.
Makes refactoring much less painful
Monday, April 22, 13
8. Why Unit Testing?
Verify your code does what you
expect.
Makes refactoring much less painful
Forces you to write smaller, more
concise methods
Monday, April 22, 13
9. 2 Kinds of Unit Test
Bundles
Monday, April 22, 13
10. 2 Kinds of Unit Test
Bundles
Application Test Bundle
Tests fully bootstrapped application
Monday, April 22, 13
11. 2 Kinds of Unit Test
Bundles
Application Test Bundle
Tests fully bootstrapped application
Logic Test Bundle
Tests classes in isolation
Monday, April 22, 13
13. Setting Up
New projects offer an option to create unit tests immediately
Monday, April 22, 13
14. Setting Up
New projects offer an option to create unit tests immediately
Creates an Application unit test bundle
Monday, April 22, 13
15. Setting Up
New projects offer an option to create unit tests immediately
If at all possible, do this
Creates an Application unit test bundle
Monday, April 22, 13
17. Setting Up
⌘ + U is automatically wired up to run tests
Monday, April 22, 13
18. Setting Up
⌘ + U is automatically wired up to run tests
Can also use Product => Test from the menu
Monday, April 22, 13
19. Setting Up
⌘ + U is automatically wired up to run tests
Builds and runs test bundle right from your app’s scheme
Can also use Product => Test from the menu
Monday, April 22, 13
21. Setting Up
A unit test bundle is created
alongside your main target
Monday, April 22, 13
22. Setting Up
A unit test bundle is created
alongside your main target
A separate Xcode group is
created for your unit tests
Monday, April 22, 13
23. Setting Up
A unit test bundle is created
alongside your main target
All tests should go inside the unit
test group
A separate Xcode group is
created for your unit tests
Monday, April 22, 13
24. Setting Up
A unit test bundle is created
alongside your main target
All tests should go inside the unit
test group
A separate Xcode group is
created for your unit tests
Usually, 1 unit test class per class
under test
Monday, April 22, 13
33. Unit Testing Conventions
All unit test methods must begin with the
word “test”
Unit test methods must have a return
type of void
Monday, April 22, 13
34. Unit Testing Conventions
All unit test methods must begin with the
word “test”
Unit test methods must have a return
type of void
Unit test methods cannot have
parameters
Monday, April 22, 13
35. Unit Testing Conventions
All unit test methods must begin with the
word “test”
Unit test classes are subclasses of
SenTestCase
Unit test methods must have a return
type of void
Unit test methods cannot have
parameters
Monday, April 22, 13
38. Unit Testing Conventions
To run some code prior to each unit test,
override the setup method
Can have as many unit test classes/files as
needed in a project
Monday, April 22, 13
39. Unit Testing Conventions
To run some code prior to each unit test,
override the setup method
Can have as many unit test classes/files as
needed in a project
To run some code after each unit test,
override the tearDown method
Monday, April 22, 13
45. A Small Example
1. Set up object to be tested
Anatomy of a Unit Test
Monday, April 22, 13
46. A Small Example
1. Set up object to be tested
Anatomy of a Unit Test
2. Set up any values for use in testing outcome
Monday, April 22, 13
47. A Small Example
1. Set up object to be tested
Anatomy of a Unit Test
2. Set up any values for use in testing outcome
3.Assert expected result
Monday, April 22, 13
49. A Small Example
Run before and after each test
Setup/Teardown Methods
Monday, April 22, 13
50. A Small Example
Run before and after each test
Setup/Teardown Methods
Use to set up objects and move repetitive code out of tests
Monday, April 22, 13
51. A Small Example
Run before and after each test
Setup/Teardown Methods
Use to set up objects and move repetitive code out of tests
Good way to ensure state of the object under test
Monday, April 22, 13
54. Unit Test Failures
Failure messages appear in the issue navigator and the console
Good test log message go a long way to determining the issue
Monday, April 22, 13
55. Unit Test Failures
Failure messages appear in the issue navigator and the console
Good test log message go a long way to determining the issue
This becomes even more important in apps with large test
suites
Monday, April 22, 13