beginning ios unit testing
DESCRIPTION
A guide to getting started to unit testing on iOS for beginnersTRANSCRIPT
Beginning iOS Unit Testing
Mark Struzinski
Monday, April 22, 13
What is Unit Testing?
Monday, April 22, 13
What is Unit Testing?
Test small pieces of your code (units) in isolation.
Monday, April 22, 13
What is Unit Testing?
Test small pieces of your code (units) in isolation.
Usually, this means testing methods.
Monday, April 22, 13
Why Unit Testing?
Monday, April 22, 13
Why Unit Testing?
Verify your code does what you expect.
Monday, April 22, 13
Why Unit Testing?
Verify your code does what you expect.
Makes refactoring much less painful
Monday, April 22, 13
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
2 Kinds of Unit Test Bundles
Monday, April 22, 13
2 Kinds of Unit Test Bundles
Application Test BundleTests fully bootstrapped application
Monday, April 22, 13
2 Kinds of Unit Test Bundles
Application Test BundleTests fully bootstrapped application
Logic Test BundleTests classes in isolation
Monday, April 22, 13
Setting Up
Monday, April 22, 13
Setting Up
New projects offer an option to create unit tests immediately
Monday, April 22, 13
Setting Up
New projects offer an option to create unit tests immediately
Creates an Application unit test bundle
Monday, April 22, 13
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
Setting Up
Monday, April 22, 13
Setting Up
⌘ + U is automatically wired up to run tests
Monday, April 22, 13
Setting Up
⌘ + U is automatically wired up to run tests
Can also use Product => Test from the menu
Monday, April 22, 13
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
Setting Up
Monday, April 22, 13
Setting UpA unit test bundle is created alongside your main target
Monday, April 22, 13
Setting UpA unit test bundle is created alongside your main target
A separate Xcode group is created for your unit tests
Monday, April 22, 13
Setting UpA 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
Setting UpA 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
Adding New Tests
Monday, April 22, 13
Adding New Tests
Add new files to unit testing group
Monday, April 22, 13
Adding New Tests
Add new files to unit testing group
New file is under Cocoa Touch => Objective-C test case class
Monday, April 22, 13
Running Tests
Monday, April 22, 13
Running Tests
Initial test run will fail
Monday, April 22, 13
Running Tests
Initial test run will fail
This is how you know it’s working
Monday, April 22, 13
Unit Testing Conventions
Monday, April 22, 13
Unit Testing ConventionsAll unit test methods must begin with the word “test”
Monday, April 22, 13
Unit Testing ConventionsAll unit test methods must begin with the word “test”
Unit test methods must have a return type of void
Monday, April 22, 13
Unit Testing ConventionsAll 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
Unit Testing ConventionsAll 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
Unit Testing Conventions
Monday, April 22, 13
Unit Testing ConventionsCan have as many unit test classes/files as needed in a project
Monday, April 22, 13
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
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
A Small Example
Monday, April 22, 13
A Small Example
Monday, April 22, 13
A Small Example
Tests use assertion macros to determine pass/fail
Monday, April 22, 13
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
A Small Example
Anatomy of a Unit Test
Monday, April 22, 13
A Small Example
1. Set up object to be tested
Anatomy of a Unit Test
Monday, April 22, 13
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
A Small Example
1. Set up object to be tested
Anatomy of a Unit Test
2. Set up any values for use in testing outcome3. Assert expected result
Monday, April 22, 13
A Small Example
Setup/Teardown Methods
Monday, April 22, 13
A Small Example
Run before and after each test
Setup/Teardown Methods
Monday, April 22, 13
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
A Small Example
Run before and after each test
Setup/Teardown Methods
Use to set up objects and move repetitive code out of testsGood way to ensure state of the object under test
Monday, April 22, 13
Unit Test Failures
Monday, April 22, 13
Unit Test Failures
Failure messages appear in the issue navigator and the console
Monday, April 22, 13
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
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
Questions?
Monday, April 22, 13
Questions?
Thank [email protected]
@ski081 (Twitter/ADN)
http://markstruzinski.com
Monday, April 22, 13
Reference
https://developer.apple.com/library/ios/#documentation/DeveloperTools/Conceptual/UnitTesting/00-About_Unit_Testing/about.html
Monday, April 22, 13