44
Christopher M. Judd Unit Testing for the iPhone

Unit Testing for the iPhone - s3.amazonaws.com · Christopher M. Judd President/Consultant of leader Columbus Developer User Group (CIDUG)

Embed Size (px)

Citation preview

Page 1: Unit Testing for the iPhone - s3.amazonaws.com · Christopher M. Judd President/Consultant of leader Columbus Developer User Group (CIDUG)

Christopher M. Judd

Unit Testing for the iPhone

Page 2: Unit Testing for the iPhone - s3.amazonaws.com · Christopher M. Judd President/Consultant of leader Columbus Developer User Group (CIDUG)

Christopher M. JuddPresident/Consultant of

leader

Columbus Developer User Group (CIDUG)

Page 3: Unit Testing for the iPhone - s3.amazonaws.com · Christopher M. Judd President/Consultant of leader Columbus Developer User Group (CIDUG)

Remarkable Ohio

Free

Developed for eTech Ohio and Ohio Historical Center

Page 4: Unit Testing for the iPhone - s3.amazonaws.com · Christopher M. Judd President/Consultant of leader Columbus Developer User Group (CIDUG)

University System Of Ohio

FreeDeveloped for eTech Ohio and University System Of Ohio

Page 5: Unit Testing for the iPhone - s3.amazonaws.com · Christopher M. Judd President/Consultant of leader Columbus Developer User Group (CIDUG)

Chmod

Free Judd Solutions

Page 6: Unit Testing for the iPhone - s3.amazonaws.com · Christopher M. Judd President/Consultant of leader Columbus Developer User Group (CIDUG)

December 2008 issue

Page 7: Unit Testing for the iPhone - s3.amazonaws.com · Christopher M. Judd President/Consultant of leader Columbus Developer User Group (CIDUG)

Unit Testing Basics

Page 8: Unit Testing for the iPhone - s3.amazonaws.com · Christopher M. Judd President/Consultant of leader Columbus Developer User Group (CIDUG)

How many of you have ever unit tested an iPhone app?

Page 9: Unit Testing for the iPhone - s3.amazonaws.com · Christopher M. Judd President/Consultant of leader Columbus Developer User Group (CIDUG)

How many of you have ever unit tested on another platform or language?

Page 10: Unit Testing for the iPhone - s3.amazonaws.com · Christopher M. Judd President/Consultant of leader Columbus Developer User Group (CIDUG)

iPhone unit testing is neither straight forward or easy

Page 11: Unit Testing for the iPhone - s3.amazonaws.com · Christopher M. Judd President/Consultant of leader Columbus Developer User Group (CIDUG)

iPhone Unit Testing Challenges

Toooo many steps to get startedNo green bar/red barCan’t run individual testsUnit test template code contains to muchUnit tests can not be debugged out of the boxOnly includes very basic testing supportNo mock framework includedLittle documentationMultiple targets

Page 12: Unit Testing for the iPhone - s3.amazonaws.com · Christopher M. Judd President/Consultant of leader Columbus Developer User Group (CIDUG)

Unit Test Coverage

The Objective-C Programming Language

Page 13: Unit Testing for the iPhone - s3.amazonaws.com · Christopher M. Judd President/Consultant of leader Columbus Developer User Group (CIDUG)

Why am I talking about it?

Page 14: Unit Testing for the iPhone - s3.amazonaws.com · Christopher M. Judd President/Consultant of leader Columbus Developer User Group (CIDUG)

Why Unit Test?

Improves designFacility change and refactoringSimplifies integrationProvides executable documentation

Page 15: Unit Testing for the iPhone - s3.amazonaws.com · Christopher M. Judd President/Consultant of leader Columbus Developer User Group (CIDUG)

includes also known as

SenTestingKit

Page 16: Unit Testing for the iPhone - s3.amazonaws.com · Christopher M. Judd President/Consultant of leader Columbus Developer User Group (CIDUG)

Logic Test == Unit TestApplication Test == More Functional Test

Page 17: Unit Testing for the iPhone - s3.amazonaws.com · Christopher M. Judd President/Consultant of leader Columbus Developer User Group (CIDUG)

Getting Started

Page 18: Unit Testing for the iPhone - s3.amazonaws.com · Christopher M. Judd President/Consultant of leader Columbus Developer User Group (CIDUG)

http://developer.apple.com/IPhone/library/documentation/Xcode/Conceptual/iphone_development/135-

Unit_Testing_Applications/unit_testing_applications.html#//apple_ref/doc/uid/TP40007959-CH20-SW3

Setup documentation

oriphone unit testing

Page 19: Unit Testing for the iPhone - s3.amazonaws.com · Christopher M. Judd President/Consultant of leader Columbus Developer User Group (CIDUG)

Unit Testing Setup Steps

1. Add unit test bundle target2. Make unit test target active3. Add group for test classes4. Add unit test class

Page 20: Unit Testing for the iPhone - s3.amazonaws.com · Christopher M. Judd President/Consultant of leader Columbus Developer User Group (CIDUG)

Add Unit Test Bundle Target

Page 21: Unit Testing for the iPhone - s3.amazonaws.com · Christopher M. Judd President/Consultant of leader Columbus Developer User Group (CIDUG)

Make Unit Test Target Active

Page 22: Unit Testing for the iPhone - s3.amazonaws.com · Christopher M. Judd President/Consultant of leader Columbus Developer User Group (CIDUG)

Add Group for Test Classes

Page 23: Unit Testing for the iPhone - s3.amazonaws.com · Christopher M. Judd President/Consultant of leader Columbus Developer User Group (CIDUG)

Add Unit Test Class

Should end in TestShould be in a separate directory

Must be have UnitTests target checked

Page 24: Unit Testing for the iPhone - s3.amazonaws.com · Christopher M. Judd President/Consultant of leader Columbus Developer User Group (CIDUG)

Generated Test Class#define USE_APPLICATION_UNIT_TEST 1

#import <SenTestingKit/SenTestingKit.h>#import <UIKit/UIKit.h>//#import "application_headers" as required

@interface SimpleTest : SenTestCase {}

#if USE_APPLICATION_UNIT_TEST- (void) testAppDelegate; // simple test on application#else- (void) testMath; // simple standalone test#endif

@end#import "SimpleTest.h"

@implementation SimpleTest

#if USE_APPLICATION_UNIT_TEST // all code under test is in the iPhone Application

- (void) testAppDelegate { id yourApplicationDelegate = [[UIApplication sharedApplication] delegate]; STAssertNotNil(yourApplicationDelegate,

@"UIApplication failed to find the AppDelegate"); }

#else // all code under test must be linked into the Unit Test bundle

- (void) testMath { STAssertTrue((1+1)==2, @"Compiler isn't feeling well today :-(" ); }

#endif@end

*Test.h

*Test.m

Page 25: Unit Testing for the iPhone - s3.amazonaws.com · Christopher M. Judd President/Consultant of leader Columbus Developer User Group (CIDUG)

Create Custom User Templates

#import <SenTestingKit/SenTestingKit.h>

@interface MyUnitTest : SenTestCase {

}

@end

#import "MyUnitTest.h"

@implementation MyUnitTest

- (void) testMethod { STAssertTrue(true, @"message"); }

@end

*Test.m

*Test.h

Page 26: Unit Testing for the iPhone - s3.amazonaws.com · Christopher M. Judd President/Consultant of leader Columbus Developer User Group (CIDUG)

Running Unit Tests

Page 27: Unit Testing for the iPhone - s3.amazonaws.com · Christopher M. Judd President/Consultant of leader Columbus Developer User Group (CIDUG)

⌘Bor

Build > Build

with unit test target selected

No green bar :( Red bar

Page 28: Unit Testing for the iPhone - s3.amazonaws.com · Christopher M. Judd President/Consultant of leader Columbus Developer User Group (CIDUG)

False

Nothing to worry about

Real Failure

Page 29: Unit Testing for the iPhone - s3.amazonaws.com · Christopher M. Judd President/Consultant of leader Columbus Developer User Group (CIDUG)

Writing Unit Tests

Page 30: Unit Testing for the iPhone - s3.amazonaws.com · Christopher M. Judd President/Consultant of leader Columbus Developer User Group (CIDUG)

Basic Unit Test

#import <SenTestingKit/SenTestingKit.h>

@interface MyUnitTest : SenTestCase {

}

@end

#import "MyUnitTest.h"

@implementation MyUnitTest

- (void) setUp { // initialize tests}

- (void) testMethod { // Test code}

- (void) tearDown { // clean up after test}

@end

*Test.m

*Test.h

must extend SenTestCase

must return void and take no parameters

test methods must begin with test

Page 31: Unit Testing for the iPhone - s3.amazonaws.com · Christopher M. Judd President/Consultant of leader Columbus Developer User Group (CIDUG)

Assert Macros

STAssertNil(a1, description, ...)STAssertNotNil(a1, description, ...)STAssertTrue(expression, description, ...)STAssertFalse(expression, description, ...)STAssertEqualObjects(a1, a2, description, ...)STAssertEquals(a1, a2, description, ...)STAssertEqualsWithAccuracy(left, right, accuracy, description, ...)STAssertThrows(expression, description, ...)STAssertThrowsSpecific(expression, specificException, description, ...)STAssertThrowsSpecificNamed(expr, specificException, aName, description, ...)STAssertNoThrow(expression, description, ...)STAssertNoThrowSpecific(expression, specificException, description, ...)STAssertNoThrowSpecificNamed(expr, specificException, aName, description, ...)STFail(description, ...)STAssertTrueNoThrow(expression, description, ...)STAssertFalseNoThrow(expression, description, ...)

Page 32: Unit Testing for the iPhone - s3.amazonaws.com · Christopher M. Judd President/Consultant of leader Columbus Developer User Group (CIDUG)

Basic Unit Test Example#import <Foundation/Foundation.h>#import <SenTestingKit/SenTestingKit.h>

@interface MyUnitTest : SenTestCase { NSMutableArray* _array;}

@end

#import "MyUnitTest.h"

@implementation MyUnitTest

- (void) setUp { _array = [[NSMutableArray arrayWithObjects:@"1", @"2", @"3", nil] retain];}

- (void) testSize { STAssertEquals(3, (int)[_array count], @"Size must be three.");}

- (void) testIndex { STAssertEqualObjects(@"2", [_array objectAtIndex:1], @"Must retrieve object at index of 1.");}

- (void) tearDown { [_array release];}

@end

*Test.m

*Test.h

Page 33: Unit Testing for the iPhone - s3.amazonaws.com · Christopher M. Judd President/Consultant of leader Columbus Developer User Group (CIDUG)

- (void) testSimple { Simple* simple = [[[Simple alloc] init] retain]; STAssertEquals(3, [simple coolMethod], @""); [simple release];}

Test TargetClasses under test must also target Test target

Solutions

Page 34: Unit Testing for the iPhone - s3.amazonaws.com · Christopher M. Judd President/Consultant of leader Columbus Developer User Group (CIDUG)

Debugging Unit Tests

Page 35: Unit Testing for the iPhone - s3.amazonaws.com · Christopher M. Judd President/Consultant of leader Columbus Developer User Group (CIDUG)

NSLog does not even work by default

Page 37: Unit Testing for the iPhone - s3.amazonaws.com · Christopher M. Judd President/Consultant of leader Columbus Developer User Group (CIDUG)

Debugging Unit Test Steps

1. Delete the Run Script2. Create otest executable3. Configure arguments4. Run tests

Page 38: Unit Testing for the iPhone - s3.amazonaws.com · Christopher M. Judd President/Consultant of leader Columbus Developer User Group (CIDUG)

Delete the Run Script

Delete

Page 39: Unit Testing for the iPhone - s3.amazonaws.com · Christopher M. Judd President/Consultant of leader Columbus Developer User Group (CIDUG)

/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.1.sdk/Developer/usr/bin/otest

Create otest Executable

Page 40: Unit Testing for the iPhone - s3.amazonaws.com · Christopher M. Judd President/Consultant of leader Columbus Developer User Group (CIDUG)

Configure Argumentsname of unit test bundle

I don’t even know

Page 41: Unit Testing for the iPhone - s3.amazonaws.com · Christopher M. Judd President/Consultant of leader Columbus Developer User Group (CIDUG)

Run Testswith unit test target and

executable selected ⌘⏎or

Build > Build and Go

test resultsNo more Red bar

Page 42: Unit Testing for the iPhone - s3.amazonaws.com · Christopher M. Judd President/Consultant of leader Columbus Developer User Group (CIDUG)

Resources

The Objective-C Programming Language