18-UnitTestRuntimeLoc

18-UnitTestRuntimeLoc - CS193P Lecture 18 iPhone...

Info iconThis preview shows pages 1–15. Sign up to view the full content.

View Full Document Right Arrow Icon
CS193P - Lecture 18 iPhone Application Development Unit Testing Fun with Objective-C Localization Mailbag
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Unit Testing
Background image of page 2
What Are Unit Tests? Test specific areas of functionality Minimal external dependencies Run frequently during development
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Who Writes Unit Tests? You do! Ideally written along with new code Test-driven development ! Write tests first ! Fill in the implementation until tests pass ! Rinse & repeat
Background image of page 4
Running Unit Tests Automate so you don't have to explicitly run tests Many testing frameworks can run tests every time you build Just as compiler checks syntax , unit tests check semantics
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Why Unit Test? Fewer bugs ! More confidence that you're shipping a high quality product Find bugs early ! Bugs are easier (and cheaper) to fix early in development Avoid regressions ! Ensure that changing one piece of code doesn't break another Document your code ! How is a method intended to be used? Check out the tests. .. Encourage good design ! Spaghetti code is hard to test! Design with testability in mind
Background image of page 6
Unit Testing Frameworks Family of similar frameworks for testing various languages ! JUnit, NUnit, PyUnit. .. OCUnit for Objective-C ! Ships with Mac OS X developer tools, integrates with Xcode ! Included with iPhone SDK as of 2.2
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Basics of OCUnit SenTestCase is abstract test case superclass Automatically runs methods that begin with "test" Macros for asserting conditions during tests ! STAssertNotNil(someObject, @"Some object was nil"); ! See SenTestCase.h for more -setUp and -tearDown methods run before and after each test
Background image of page 8
Defining A New Test Case Class #import <SenTestingKit/SenTestingKit.h> @class Foo; @interface FooTests : SenTestCase { Foo *foo; } @end
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Preparing Tests @implementation FooTests - (void) setUp { // Every test will have its own Foo instance foo = [[Foo alloc] init]; } - (void) tearDown { [foo release]; } ... @end
Background image of page 10
Adding Tests @implementation FooTests ... - (void) testCreateFoo { STAssertNotNil (foo, @"Couldn't create Foo"); } - (void) testSetBar { Bar *bar = . ..; foo.bar = bar; STAssertEqualObjects (foo.bar, bar, @"Couldn't set foo.bar"); } ... @end
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Testing Error Conditions @implementation FooTests ... - (void) testOutOfBoundsAccess { STAssertNil ([foo barAtIndex:99], @"Index 99 should be nil"); } ... @end
Background image of page 12
Demo: Unit Testing an iPhone App
Background image of page 13

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Always be conscious of the return on investment ! Benefit of the test versus time to create and maintain? Some types of code are notoriously difficult to test
Background image of page 14
Image of page 15
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 63

18-UnitTestRuntimeLoc - CS193P Lecture 18 iPhone...

This preview shows document pages 1 - 15. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online