Transcript
Page 1: Testing in iOSdudek.mobi/slides/(Unit) Testing iOS Apps.pdf · • Mocking (mocks & stubs)# ... • All tests begin with test# • Setup and teardown method# • Everything else is

(Unit) Testing iOS AppsPaweł Dudek

���1

Page 2: Testing in iOSdudek.mobi/slides/(Unit) Testing iOS Apps.pdf · • Mocking (mocks & stubs)# ... • All tests begin with test# • Setup and teardown method# • Everything else is

Why do we want to write tests?

���2

Page 3: Testing in iOSdudek.mobi/slides/(Unit) Testing iOS Apps.pdf · • Mocking (mocks & stubs)# ... • All tests begin with test# • Setup and teardown method# • Everything else is

Reasons for testing

• Striving for better software

• Leads to better, more modularized codebase

• Faster development cycles

• Being “confident” about your code

• Less code to write

• Saved time

���3

Page 4: Testing in iOSdudek.mobi/slides/(Unit) Testing iOS Apps.pdf · • Mocking (mocks & stubs)# ... • All tests begin with test# • Setup and teardown method# • Everything else is

Common misconceptions

���4

Page 5: Testing in iOSdudek.mobi/slides/(Unit) Testing iOS Apps.pdf · • Mocking (mocks & stubs)# ... • All tests begin with test# • Setup and teardown method# • Everything else is

Common misconceptions

• “It will take longer to write code” or “Time spent writing/refactoring tests is time lost”

• “It will take more time to modify existing system”

���5

Page 6: Testing in iOSdudek.mobi/slides/(Unit) Testing iOS Apps.pdf · • Mocking (mocks & stubs)# ... • All tests begin with test# • Setup and teardown method# • Everything else is

Reasons for testing

���6

• Striving for better software

• Leads to better, more modularized codebase

• Faster development cycles

• Being “confident” about your code

• Less code to write

• Saved time

Page 7: Testing in iOSdudek.mobi/slides/(Unit) Testing iOS Apps.pdf · • Mocking (mocks & stubs)# ... • All tests begin with test# • Setup and teardown method# • Everything else is

Am I going to write poor software if I don’t

do tests?

���7

Page 8: Testing in iOSdudek.mobi/slides/(Unit) Testing iOS Apps.pdf · • Mocking (mocks & stubs)# ... • All tests begin with test# • Setup and teardown method# • Everything else is

Are unit tests an invaluable tool for writing great software? Heck yes. Am I going to produce a poor product if I can’t unit test? Hell no.

Jonathan Rasmusson

���8

Page 9: Testing in iOSdudek.mobi/slides/(Unit) Testing iOS Apps.pdf · • Mocking (mocks & stubs)# ... • All tests begin with test# • Setup and teardown method# • Everything else is

Now that we know that writing tests is a

good idea...

���9

Page 10: Testing in iOSdudek.mobi/slides/(Unit) Testing iOS Apps.pdf · • Mocking (mocks & stubs)# ... • All tests begin with test# • Setup and teardown method# • Everything else is

How can we do it?

���10

Page 11: Testing in iOSdudek.mobi/slides/(Unit) Testing iOS Apps.pdf · • Mocking (mocks & stubs)# ... • All tests begin with test# • Setup and teardown method# • Everything else is

• You will feel confused

• You won’t know how to start

• You will need help

• Conclusion: it’s not easy to start

���11

Warning

Page 12: Testing in iOSdudek.mobi/slides/(Unit) Testing iOS Apps.pdf · • Mocking (mocks & stubs)# ... • All tests begin with test# • Setup and teardown method# • Everything else is

Tips

• Never think of tests as tests

• Think of a scenario, behavior, example

• Grab a mature project from github with tests included

• Find someone experienced and ask questions

• Program in pairs!

���12

Page 13: Testing in iOSdudek.mobi/slides/(Unit) Testing iOS Apps.pdf · • Mocking (mocks & stubs)# ... • All tests begin with test# • Setup and teardown method# • Everything else is

Get on with it!How can we test?

���13

Page 14: Testing in iOSdudek.mobi/slides/(Unit) Testing iOS Apps.pdf · • Mocking (mocks & stubs)# ... • All tests begin with test# • Setup and teardown method# • Everything else is

TDD

• Test Driven Development

• Red, Green, Refactor

• Write failing test first

• Fix it

• Refactor

���14

Page 15: Testing in iOSdudek.mobi/slides/(Unit) Testing iOS Apps.pdf · • Mocking (mocks & stubs)# ... • All tests begin with test# • Setup and teardown method# • Everything else is

BDDBehavior Driven Development

���15

Page 16: Testing in iOSdudek.mobi/slides/(Unit) Testing iOS Apps.pdf · • Mocking (mocks & stubs)# ... • All tests begin with test# • Setup and teardown method# • Everything else is

How does BDD differ from TDD?

���16

Page 17: Testing in iOSdudek.mobi/slides/(Unit) Testing iOS Apps.pdf · • Mocking (mocks & stubs)# ... • All tests begin with test# • Setup and teardown method# • Everything else is

BDD builds upon TDD by formalising the good habits of the best TDD practitioners.

Matt Wynne, XP Evangelist

���17

Page 18: Testing in iOSdudek.mobi/slides/(Unit) Testing iOS Apps.pdf · • Mocking (mocks & stubs)# ... • All tests begin with test# • Setup and teardown method# • Everything else is

Good habits

���18

• Work outside-in

• Use examples

• Use ubiquitous language

Page 19: Testing in iOSdudek.mobi/slides/(Unit) Testing iOS Apps.pdf · • Mocking (mocks & stubs)# ... • All tests begin with test# • Setup and teardown method# • Everything else is

A little bit of terminology...

���19

Page 20: Testing in iOSdudek.mobi/slides/(Unit) Testing iOS Apps.pdf · • Mocking (mocks & stubs)# ... • All tests begin with test# • Setup and teardown method# • Everything else is

Terminology

���20

• Mocking (mocks & stubs)

• Expecting

• Matching

• Faking

Page 21: Testing in iOSdudek.mobi/slides/(Unit) Testing iOS Apps.pdf · • Mocking (mocks & stubs)# ... • All tests begin with test# • Setup and teardown method# • Everything else is

Testing in iOS

���21

Page 22: Testing in iOSdudek.mobi/slides/(Unit) Testing iOS Apps.pdf · • Mocking (mocks & stubs)# ... • All tests begin with test# • Setup and teardown method# • Everything else is

Unit Tests

���22

Page 23: Testing in iOSdudek.mobi/slides/(Unit) Testing iOS Apps.pdf · • Mocking (mocks & stubs)# ... • All tests begin with test# • Setup and teardown method# • Everything else is

OCUnit

���23

• Oldest Mac testing framework - officially supported by Apple since 2005

• Integrated with XCode

• Built-in assertion macros

Page 24: Testing in iOSdudek.mobi/slides/(Unit) Testing iOS Apps.pdf · • Mocking (mocks & stubs)# ... • All tests begin with test# • Setup and teardown method# • Everything else is

OCUnit Syntax

• All test classes inherit from SenTestCase

• All tests begin with test

• Setup and teardown method

• Everything else is ignored by testing framework

• Means you can use as additional setup methods!

���24

Page 25: Testing in iOSdudek.mobi/slides/(Unit) Testing iOS Apps.pdf · • Mocking (mocks & stubs)# ... • All tests begin with test# • Setup and teardown method# • Everything else is

-(void)testFullName { Person *person = [Person person]; person.firstName = @"Mariusz"; person.secondName = @"Testowniczek"; NSString *fullName = [person fullName]; NSString *expectedName = @"Mariusz Testowniczek"; STAssertTrue([fullName isEqualToString:expectedName], @""); }

���25

OCUnit

Page 26: Testing in iOSdudek.mobi/slides/(Unit) Testing iOS Apps.pdf · • Mocking (mocks & stubs)# ... • All tests begin with test# • Setup and teardown method# • Everything else is

OCUnit vs XCTest

���26

Page 27: Testing in iOSdudek.mobi/slides/(Unit) Testing iOS Apps.pdf · • Mocking (mocks & stubs)# ... • All tests begin with test# • Setup and teardown method# • Everything else is

Behavior “Tests”

���27

Page 28: Testing in iOSdudek.mobi/slides/(Unit) Testing iOS Apps.pdf · • Mocking (mocks & stubs)# ... • All tests begin with test# • Setup and teardown method# • Everything else is

Kiwi and Cedar

���28

• Nearly the same syntax

• Built-in stubs/mocks

• Built-in matchers

Page 29: Testing in iOSdudek.mobi/slides/(Unit) Testing iOS Apps.pdf · • Mocking (mocks & stubs)# ... • All tests begin with test# • Setup and teardown method# • Everything else is

���29

Kiwi and Cedar Syntax

Page 30: Testing in iOSdudek.mobi/slides/(Unit) Testing iOS Apps.pdf · • Mocking (mocks & stubs)# ... • All tests begin with test# • Setup and teardown method# • Everything else is

SPEC_BEGIN(PersonSpec) !describe(@"Person", ^{ __block Person *person; ! beforeEach(^{ person = [[Person alloc] init]; person.firstName = @"Mariusz"; person.lastName = @"Fixture Last Name"; }); ! describe(@"full name", ^{ ! __block NSString *fullName; ! beforeEach(^{ fullName = [person fullName]; }); ! it(@"should return the full name", ^{ expect(fullName).to(equal(@"Mariusz Testowniczek")); }); }); }); !SPEC_END ���30

Page 31: Testing in iOSdudek.mobi/slides/(Unit) Testing iOS Apps.pdf · • Mocking (mocks & stubs)# ... • All tests begin with test# • Setup and teardown method# • Everything else is

Example

���31

Page 32: Testing in iOSdudek.mobi/slides/(Unit) Testing iOS Apps.pdf · • Mocking (mocks & stubs)# ... • All tests begin with test# • Setup and teardown method# • Everything else is

CedarTaptera Additions

���32

Page 33: Testing in iOSdudek.mobi/slides/(Unit) Testing iOS Apps.pdf · • Mocking (mocks & stubs)# ... • All tests begin with test# • Setup and teardown method# • Everything else is

The action block

���33

Page 34: Testing in iOSdudek.mobi/slides/(Unit) Testing iOS Apps.pdf · • Mocking (mocks & stubs)# ... • All tests begin with test# • Setup and teardown method# • Everything else is

The action block

���34

• Syntax addition to previous blocks

• Executed after all beforeEach’s for given example are run

• Really useful when chaining behavior tests

Page 35: Testing in iOSdudek.mobi/slides/(Unit) Testing iOS Apps.pdf · • Mocking (mocks & stubs)# ... • All tests begin with test# • Setup and teardown method# • Everything else is

���35

Describe

Describe

beforeEach

action

Describe

beforeEach

action

beforeEach

action

it

Page 36: Testing in iOSdudek.mobi/slides/(Unit) Testing iOS Apps.pdf · • Mocking (mocks & stubs)# ... • All tests begin with test# • Setup and teardown method# • Everything else is

Example

���36

Page 37: Testing in iOSdudek.mobi/slides/(Unit) Testing iOS Apps.pdf · • Mocking (mocks & stubs)# ... • All tests begin with test# • Setup and teardown method# • Everything else is

Helper libraries

���37

Page 38: Testing in iOSdudek.mobi/slides/(Unit) Testing iOS Apps.pdf · • Mocking (mocks & stubs)# ... • All tests begin with test# • Setup and teardown method# • Everything else is

Helper libraries

���38

• Mocking: OCMock, OCMockito, LRMockey

• Expecting: Expecta

• Matching: OCHamcrest

Page 39: Testing in iOSdudek.mobi/slides/(Unit) Testing iOS Apps.pdf · • Mocking (mocks & stubs)# ... • All tests begin with test# • Setup and teardown method# • Everything else is

Most the presented libraries offer similar

functionalityIt all depends on syntax.

���39

Page 40: Testing in iOSdudek.mobi/slides/(Unit) Testing iOS Apps.pdf · • Mocking (mocks & stubs)# ... • All tests begin with test# • Setup and teardown method# • Everything else is

iOS Testing Tips

���40

Page 41: Testing in iOSdudek.mobi/slides/(Unit) Testing iOS Apps.pdf · • Mocking (mocks & stubs)# ... • All tests begin with test# • Setup and teardown method# • Everything else is

Testing UI Layout

���41

• Hard to maintain (as can change rapidly when GD goes on a rampage)

• Gives little value (quickly noticed by QA if something is off)

Page 42: Testing in iOSdudek.mobi/slides/(Unit) Testing iOS Apps.pdf · • Mocking (mocks & stubs)# ... • All tests begin with test# • Setup and teardown method# • Everything else is

System Singletons

���42

• Makes hard to test if accessed directly

• Nice candidate for putting in a property

[UIDevice currentDevice][UIScreen mainScreen]

Page 43: Testing in iOSdudek.mobi/slides/(Unit) Testing iOS Apps.pdf · • Mocking (mocks & stubs)# ... • All tests begin with test# • Setup and teardown method# • Everything else is

UIViewController transitions

���43

• Pushing new view controllers on nav controller stack or using transitions API

• Use helper class

• Tests - check if a given method was called on the helper class

Page 44: Testing in iOSdudek.mobi/slides/(Unit) Testing iOS Apps.pdf · • Mocking (mocks & stubs)# ... • All tests begin with test# • Setup and teardown method# • Everything else is

Testing UIView animations

���44

• Easiest way is to use the block-based API

• Helper class similar to transitions

• Tests - use fake to immediately call the animation block

Page 45: Testing in iOSdudek.mobi/slides/(Unit) Testing iOS Apps.pdf · • Mocking (mocks & stubs)# ... • All tests begin with test# • Setup and teardown method# • Everything else is

Common caveats

���45

• Don’t set mocks on [UIViewController view]

• Avoid using categories to override system properties or existing behaviour

• Keychain and most of system objects are unavailable when tests are run from command line w/o simulator

• Don’t test objects that are fakes or partial mocks

Page 46: Testing in iOSdudek.mobi/slides/(Unit) Testing iOS Apps.pdf · • Mocking (mocks & stubs)# ... • All tests begin with test# • Setup and teardown method# • Everything else is

Things worth talking about but cut due to time limitations

���46

• Frank / KIF - Application Tests

• Specta - yet another BDD style testing framework

• Dependency injection - Objection, Typhoon

Page 47: Testing in iOSdudek.mobi/slides/(Unit) Testing iOS Apps.pdf · • Mocking (mocks & stubs)# ... • All tests begin with test# • Setup and teardown method# • Everything else is

Summary

���47

Page 48: Testing in iOSdudek.mobi/slides/(Unit) Testing iOS Apps.pdf · • Mocking (mocks & stubs)# ... • All tests begin with test# • Setup and teardown method# • Everything else is

Summary

• Testing is a great way to help developers

• Better codebase, faster iterations

• Invaluable for larger projects

���48

Page 49: Testing in iOSdudek.mobi/slides/(Unit) Testing iOS Apps.pdf · • Mocking (mocks & stubs)# ... • All tests begin with test# • Setup and teardown method# • Everything else is

Resources & Contact

@eldudi

github.com/paweldudek

[email protected]

Code Examples

Contact

���49


Top Related