Testing in iOS - mobile-warsaw.pl · TDD • Test Driven ... Testing in iOS 21 Thursday, July 4, 13. Unit Tests 22 Thursday, July 4, 13. OCUnit 23

Post on 16-Oct-2020

0 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

Transcript

Testing in iOSPaweł Dudek

1

Thursday, July 4, 13

Why do we want to write tests?

2

Thursday, July 4, 13

Reasons for testing

• Striving for better software

• Faster development cycles

• Being “confident” about your code

• Leads to better, more modularized codebase

• Less code to write

3

Thursday, July 4, 13

Common misconceptions

4

Thursday, July 4, 13

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

Thursday, July 4, 13

Reasons for testing

• Striving for better software

• Faster development cycles

• Being “confident” about your code

• Leads to better, more modularized codebase

• Less code to write

6

Thursday, July 4, 13

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

do tests?

7

Thursday, July 4, 13

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

Thursday, July 4, 13

Now that we know that writing tests is a

good idea...

9

Thursday, July 4, 13

How can we do it?

10

Thursday, July 4, 13

• You will feel confused

• You won’t know how to start

• You will need help

• Conclusion: it’s not easy to start

11

Warning

Thursday, July 4, 13

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

12

Thursday, July 4, 13

Get on with it!How can we test?

13

Thursday, July 4, 13

TDD

• Test Driven Development

• Red, Green, Refactor

• Write failing test first

• Fix it

• Refactor

14

Thursday, July 4, 13

BDDBehavior Driven Development

15

Thursday, July 4, 13

How does BDD differ from TDD?

16

Thursday, July 4, 13

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

Matt Wynne,XP Evangelist

17

Thursday, July 4, 13

Good habits

18

• Work outside-in

• Use examples

• Use ubiquitous language

Thursday, July 4, 13

A little bit of terminology...

19

Thursday, July 4, 13

Terminology

20

• Mocking (mocks & stubs)

• Expecting

• Matching

• Faking

Thursday, July 4, 13

Testing in iOS

21

Thursday, July 4, 13

Unit Tests

22

Thursday, July 4, 13

OCUnit

23

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

• Integrated with XCode

• Built-in assertion macros

Thursday, July 4, 13

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

Thursday, July 4, 13

-(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

Thursday, July 4, 13

Behavior “Tests”

26

Thursday, July 4, 13

Kiwi and Cedar

27

• Nearly the same syntax

• Built-in stubs/mocks

• Built-in matchers

Thursday, July 4, 13

28

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

Kiwi and Cedar Syntax

Thursday, July 4, 13

29

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_ENDThursday, July 4, 13

Example

30

Thursday, July 4, 13

CedarTaptera Additions

31

Thursday, July 4, 13

The action block

32

Thursday, July 4, 13

The action block

33

• Syntax addition to previous blocks

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

• Really useful when chaining behavior tests

Thursday, July 4, 13

34

Describe

Describe

beforeEach

action

Describe

beforeEach

action

beforeEach

action

it

Thursday, July 4, 13

Example

35

Thursday, July 4, 13

Helper libraries

36

Thursday, July 4, 13

Helper libraries

37

• Mocking: OCMock, OCMockito, LRMockey

• Expecting: Expecta

• Matching: OCHamcrest

Thursday, July 4, 13

Most the presented libraries offer similar

functionalityIt all depends on syntax.

38

Thursday, July 4, 13

iOS Testing Tips

39

Thursday, July 4, 13

Testing UI Layout

40

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

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

Thursday, July 4, 13

System Singletons

41

• Makes hard to test if accessed directly

• Nice candidate for putting in a property

[UIDevice currentDevice][UIScreen mainScreen]

Thursday, July 4, 13

UIViewController transitions

42

• 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

Thursday, July 4, 13

Testing UIView animations

43

• Easiest way is to use the block-based API

• Helper class similar to transitions

• Tests - use fake to immediately call the animation block

Thursday, July 4, 13

Common caveats

44

• Don’t set mocks on [UIViewController view]

• Avoid using categories to override system properties or existing behavior

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

• iOS 5.x Simulator is broken for NSProxy subclasses in weak properties

Thursday, July 4, 13

Things worth talking about but cut due to time limitations

45

• Frank / KIF - Application Tests

• Specta - yet another BDD style testing framework

• Dependency injection

Thursday, July 4, 13

Summary

46

Thursday, July 4, 13

Summary

• Testing is a great way to help developers

• Better codebase, faster iterations

• Invaluable for larger projects

47

Thursday, July 4, 13

Resources & Contact

@eldudi

github.com/paweldudek

pawel@dudek.mobi

Code Examples

Contact

48

Thursday, July 4, 13

top related