Slide 1

Slide 1 text

Automated Testing with Objective-C Ben Scheirman

Slide 2

Slide 2 text

Ben Scheirman Director of Development at ChaiOne Houston,

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

nsscreencast.com

Slide 5

Slide 5 text

Automated Tests

Slide 6

Slide 6 text

Automated Tests

Slide 7

Slide 7 text

Does your application work?

Slide 8

Slide 8 text

˒ˑˑˑˑ

Slide 9

Slide 9 text

Application Code Automated Tests

Slide 10

Slide 10 text

Application Code Automated Tests

Slide 11

Slide 11 text

Application Code Automated Tests ✓ ✓ ✓ ✓ x x

Slide 12

Slide 12 text

Unit Tests Integration Tests Acceptance Tests

Slide 13

Slide 13 text

Unit Tests

Slide 14

Slide 14 text

Unit Tests

Slide 15

Slide 15 text

Integration Tests

Slide 16

Slide 16 text

Acceptance Tests

Slide 17

Slide 17 text

Acceptance Tests

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

What's in a test run?

Slide 21

Slide 21 text

What's in a test run? Setup Test Test Tear Down Setup Tear Down

Slide 22

Slide 22 text

What's in a test? Test Arrange Act Assert

Slide 23

Slide 23 text

What's in a test? Test

Slide 24

Slide 24 text

What's in a test? Test - (void)test_adding_numbers { Calculator *calc = [Calculator new]; int sum = [calc add:x to:y]; XCTAssertEquals(7, sum, @"math is hard :("); }

Slide 25

Slide 25 text

What's in a test? Test - (void)test_adding_numbers { Calculator *calc = [Calculator new]; int sum = [calc add:x to:y]; XCTAssertEquals(7, sum, @"math is hard :("); } Arrange

Slide 26

Slide 26 text

What's in a test? Test - (void)test_adding_numbers { Calculator *calc = [Calculator new]; int sum = [calc add:x to:y]; XCTAssertEquals(7, sum, @"math is hard :("); } Act

Slide 27

Slide 27 text

What's in a test? Test - (void)test_adding_numbers { Calculator *calc = [Calculator new]; int sum = [calc add:x to:y]; XCTAssertEquals(7, sum, @"math is hard :("); } Assert

Slide 28

Slide 28 text

1 assertion per test

Slide 29

Slide 29 text

No content

Slide 30

Slide 30 text

Unit Tests come default on Xcode 5

Slide 31

Slide 31 text

No content

Slide 32

Slide 32 text

⌘U

Slide 33

Slide 33 text

No content

Slide 34

Slide 34 text

Demo: Guess the number game

Slide 35

Slide 35 text

How do we know what number it will generate?

Slide 36

Slide 36 text

Game

Slide 37

Slide 37 text

Game RNG

Slide 38

Slide 38 text

Game RNG

Slide 39

Slide 39 text

Game RNG

Slide 40

Slide 40 text

Game RNG NG Stub

Slide 41

Slide 41 text

Demo: Stubbing with OCMock

Slide 42

Slide 42 text

What about... View Controllers? Interface Builder? Storyboards?

Slide 43

Slide 43 text

DETOUR AHEAD Behavior Driven Development

Slide 44

Slide 44 text

GIVEN WHEN THEN

Slide 45

Slide 45 text

GIVEN WHEN THEN Some Behavior

Slide 46

Slide 46 text

GIVEN WHEN THEN Some Behavior I do X

Slide 47

Slide 47 text

GIVEN WHEN THEN Some Behavior I do X Y should happen

Slide 48

Slide 48 text

SpecBegin(CalculatorSpec) describe(@"Calculator", ^{ __block Calculator *_calc; beforeEach(^{ _calc = [Calculator new]; }); it(@"should add two numbers", ^{ int sum = [_calc add:5 to:7]; expect(sum).to.equal(12); }); }); SpecEnd Specta / Expecta

Slide 49

Slide 49 text

Kiwi expect(sum).to.equal(12); [theValue(sum) should] equal:theValue(12)]; Expecta

Slide 50

Slide 50 text

Kiwi Leverage OCMock for mocks & stubs Built-in powerful mocking & stubbing functionality Expecta

Slide 51

Slide 51 text

UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil]; UINavigationController *nav = [mainStoryboard instantiateInitialViewController]; ViewController *vc = (ViewController *)[nav visibleViewController]; UIView *view = vc.view; expect(view).toNot.beNil(); Testing Storyboards

Slide 52

Slide 52 text

UIButton *button = _vc.loginButton; NSArray *actions = [button actionsForTarget:_vc forControlEvent:UIControlEventTouchUpInside ]; expect(actions[0]).to.equal(@"loginTapped:"); Testing Wired Actions

Slide 53

Slide 53 text

Testing Block Based interaction VC Login Service External API

Slide 54

Slide 54 text

Testing Block Based interaction VC Login Service External API

Slide 55

Slide 55 text

Testing Block Based interaction VC Login Service External API ^{ } ^(User *user){ if (user) { NSLog(@"Logged in!"); } else { NSLog(@"Invalid login..."); } }

Slide 56

Slide 56 text

Testing Block Based interaction VC Login Service External API ^{ } ^(User *user){ if (user) { NSLog(@"Logged in!"); } else { NSLog(@"Invalid login..."); } }

Slide 57

Slide 57 text

VC Login Service External API ^{ }

Slide 58

Slide 58 text

VC Login Service External API ^{ } System

Slide 59

Slide 59 text

VC Login Service External API ^{ } System

Slide 60

Slide 60 text

Step 1: Verify Login Service is called

Slide 61

Slide 61 text

describe(@"logging in", ^{ it(@"should verify username & password w/ login svc", ^{ id mockLoginService = [OCMockObject mockForClass:[LoginService class]]; [[mockLoginService expect] verifyUsername:@"user1" password:@"password1" completion: [OCMArg any]]; _vc.loginService = mockLoginService; _vc.usernameTextField.text = @"user1"; _vc.passwordTextField.text = @"password1"; [_vc loginTapped:nil]; [mockLoginService verify]; }); });

Slide 62

Slide 62 text

describe(@"logging in", ^{ it(@"should verify username & password w/ login svc", ^{ id mockLoginService = [OCMockObject mockForClass:[LoginService class]]; [[mockLoginService expect] verifyUsername:@"user1" password:@"password1" completion: [OCMArg any]]; _vc.loginService = mockLoginService; _vc.usernameTextField.text = @"user1"; _vc.passwordTextField.text = @"password1"; [_vc loginTapped:nil]; [mockLoginService verify]; }); });

Slide 63

Slide 63 text

Step 2: Define the behavior when the login service calls back

Slide 64

Slide 64 text

Step 2: Define the behavior when the login service calls back 2 cases: success & failure

Slide 65

Slide 65 text

describe(@"logging in", ^{ it(@"should welcome the user when the login is valid", ^{ id mockLoginService = [OCMockObject mockForClass:[LoginService class]]; [[mockLoginService expect] verifyUsername:[OCMArg any] password:[OCMArg any] completion:... ]; _vc.loginService = mockLoginService; [_vc loginTapped:nil]; [mockLoginService verify]; }); });

Slide 66

Slide 66 text

describe(@"logging in", ^{ it(@"should welcome the user when the login is valid", ^{ id mockLoginService = [OCMockObject mockForClass:[LoginService class]]; [[mockLoginService expect] verifyUsername:[OCMArg any] password:[OCMArg any] completion:... ]; _vc.loginService = mockLoginService; [_vc loginTapped:nil]; [mockLoginService verify]; }); });

Slide 67

Slide 67 text

[[mockLoginService expect] verifyUsername:[OCMArg any] password:[OCMArg any] completion:[OCMArg checkWithBlock:^BOOL(id completionBlock) { // fake login successful for a user id mockUser = [OCMockObject mockForClass:[User class]]; completionBlock(mockUser); return YES; // signify that this argument is valid }]];

Slide 68

Slide 68 text

Where to go from here? Explore Acceptance Test Frameworks: KIF Frank UIAutomation

Slide 69

Slide 69 text

Some Parting Tips... If it's hard to test, improve your design

Slide 70

Slide 70 text

Some Parting Tips... If it's hard to test, improve your design Try to test 1 thing

Slide 71

Slide 71 text

Some Parting Tips... If it's hard to test, improve your design Try to test 1 thing Write tests first to avoid creating untestable designs

Slide 72

Slide 72 text

Some Parting Tips... If it's hard to test, improve your design Try to test 1 thing Write tests first to avoid creating untestable designs Use a continuous integration server

Slide 73

Slide 73 text

Some Parting Tips... If it's hard to test, improve your design Try to test 1 thing Write tests first to avoid creating untestable designs Use a continuous integration server PRACTICE!

Slide 74

Slide 74 text

Thank you! Ben Scheirman @subdigital benscheirman.com