Beginning iOS unit testing

58
Beginning iOS Unit Testing Mark Struzinski Monday, April 22, 13

description

A guide to getting started to unit testing on iOS for beginners

Transcript of Beginning iOS unit testing

Page 1: Beginning iOS unit testing

Beginning iOS Unit Testing

Mark Struzinski

Monday, April 22, 13

Page 2: Beginning iOS unit testing

What is Unit Testing?

Monday, April 22, 13

Page 3: Beginning iOS unit testing

What is Unit Testing?

Test small pieces of your code (units) in isolation.

Monday, April 22, 13

Page 4: Beginning iOS unit testing

What is Unit Testing?

Test small pieces of your code (units) in isolation.

Usually, this means testing methods.

Monday, April 22, 13

Page 5: Beginning iOS unit testing

Why Unit Testing?

Monday, April 22, 13

Page 6: Beginning iOS unit testing

Why Unit Testing?

Verify your code does what you expect.

Monday, April 22, 13

Page 7: Beginning iOS unit testing

Why Unit Testing?

Verify your code does what you expect.

Makes refactoring much less painful

Monday, April 22, 13

Page 8: Beginning iOS unit testing

Why Unit Testing?

Verify your code does what you expect.

Makes refactoring much less painful

Forces you to write smaller, more concise methods

Monday, April 22, 13

Page 9: Beginning iOS unit testing

2 Kinds of Unit Test Bundles

Monday, April 22, 13

Page 10: Beginning iOS unit testing

2 Kinds of Unit Test Bundles

Application Test BundleTests fully bootstrapped application

Monday, April 22, 13

Page 11: Beginning iOS unit testing

2 Kinds of Unit Test Bundles

Application Test BundleTests fully bootstrapped application

Logic Test BundleTests classes in isolation

Monday, April 22, 13

Page 12: Beginning iOS unit testing

Setting Up

Monday, April 22, 13

Page 13: Beginning iOS unit testing

Setting Up

New projects offer an option to create unit tests immediately

Monday, April 22, 13

Page 14: Beginning iOS unit testing

Setting Up

New projects offer an option to create unit tests immediately

Creates an Application unit test bundle

Monday, April 22, 13

Page 15: Beginning iOS unit testing

Setting Up

New projects offer an option to create unit tests immediately

If at all possible, do this

Creates an Application unit test bundle

Monday, April 22, 13

Page 16: Beginning iOS unit testing

Setting Up

Monday, April 22, 13

Page 17: Beginning iOS unit testing

Setting Up

⌘ + U is automatically wired up to run tests

Monday, April 22, 13

Page 18: Beginning iOS unit testing

Setting Up

⌘ + U is automatically wired up to run tests

Can also use Product => Test from the menu

Monday, April 22, 13

Page 19: Beginning iOS unit testing

Setting Up

⌘ + U is automatically wired up to run tests

Builds and runs test bundle right from your app’s scheme

Can also use Product => Test from the menu

Monday, April 22, 13

Page 20: Beginning iOS unit testing

Setting Up

Monday, April 22, 13

Page 21: Beginning iOS unit testing

Setting UpA unit test bundle is created alongside your main target

Monday, April 22, 13

Page 22: Beginning iOS unit testing

Setting UpA unit test bundle is created alongside your main target

A separate Xcode group is created for your unit tests

Monday, April 22, 13

Page 23: Beginning iOS unit testing

Setting UpA unit test bundle is created alongside your main target

All tests should go inside the unit test group

A separate Xcode group is created for your unit tests

Monday, April 22, 13

Page 24: Beginning iOS unit testing

Setting UpA unit test bundle is created alongside your main target

All tests should go inside the unit test group

A separate Xcode group is created for your unit tests

Usually, 1 unit test class per class under test

Monday, April 22, 13

Page 25: Beginning iOS unit testing

Adding New Tests

Monday, April 22, 13

Page 26: Beginning iOS unit testing

Adding New Tests

Add new files to unit testing group

Monday, April 22, 13

Page 27: Beginning iOS unit testing

Adding New Tests

Add new files to unit testing group

New file is under Cocoa Touch => Objective-C test case class

Monday, April 22, 13

Page 28: Beginning iOS unit testing

Running Tests

Monday, April 22, 13

Page 29: Beginning iOS unit testing

Running Tests

Initial test run will fail

Monday, April 22, 13

Page 30: Beginning iOS unit testing

Running Tests

Initial test run will fail

This is how you know it’s working

Monday, April 22, 13

Page 31: Beginning iOS unit testing

Unit Testing Conventions

Monday, April 22, 13

Page 32: Beginning iOS unit testing

Unit Testing ConventionsAll unit test methods must begin with the word “test”

Monday, April 22, 13

Page 33: Beginning iOS unit testing

Unit Testing ConventionsAll unit test methods must begin with the word “test”

Unit test methods must have a return type of void

Monday, April 22, 13

Page 34: Beginning iOS unit testing

Unit Testing ConventionsAll unit test methods must begin with the word “test”

Unit test methods must have a return type of void

Unit test methods cannot have parameters

Monday, April 22, 13

Page 35: Beginning iOS unit testing

Unit Testing ConventionsAll unit test methods must begin with the word “test”

Unit test classes are subclasses of SenTestCase

Unit test methods must have a return type of void

Unit test methods cannot have parameters

Monday, April 22, 13

Page 36: Beginning iOS unit testing

Unit Testing Conventions

Monday, April 22, 13

Page 37: Beginning iOS unit testing

Unit Testing ConventionsCan have as many unit test classes/files as needed in a project

Monday, April 22, 13

Page 38: Beginning iOS unit testing

Unit Testing Conventions

To run some code prior to each unit test, override the setup method

Can have as many unit test classes/files as needed in a project

Monday, April 22, 13

Page 39: Beginning iOS unit testing

Unit Testing Conventions

To run some code prior to each unit test, override the setup method

Can have as many unit test classes/files as needed in a project

To run some code after each unit test, override the tearDown method

Monday, April 22, 13

Page 40: Beginning iOS unit testing

A Small Example

Monday, April 22, 13

Page 41: Beginning iOS unit testing

A Small Example

Monday, April 22, 13

Page 42: Beginning iOS unit testing

A Small Example

Tests use assertion macros to determine pass/fail

Monday, April 22, 13

Page 43: Beginning iOS unit testing

Assertion Macros

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

(source:SenTestCase.h)

Monday, April 22, 13

Page 44: Beginning iOS unit testing

A Small Example

Anatomy of a Unit Test

Monday, April 22, 13

Page 45: Beginning iOS unit testing

A Small Example

1. Set up object to be tested

Anatomy of a Unit Test

Monday, April 22, 13

Page 46: Beginning iOS unit testing

A Small Example

1. Set up object to be tested

Anatomy of a Unit Test

2. Set up any values for use in testing outcome

Monday, April 22, 13

Page 47: Beginning iOS unit testing

A Small Example

1. Set up object to be tested

Anatomy of a Unit Test

2. Set up any values for use in testing outcome3. Assert expected result

Monday, April 22, 13

Page 48: Beginning iOS unit testing

A Small Example

Setup/Teardown Methods

Monday, April 22, 13

Page 49: Beginning iOS unit testing

A Small Example

Run before and after each test

Setup/Teardown Methods

Monday, April 22, 13

Page 50: Beginning iOS unit testing

A Small Example

Run before and after each test

Setup/Teardown Methods

Use to set up objects and move repetitive code out of tests

Monday, April 22, 13

Page 51: Beginning iOS unit testing

A Small Example

Run before and after each test

Setup/Teardown Methods

Use to set up objects and move repetitive code out of testsGood way to ensure state of the object under test

Monday, April 22, 13

Page 52: Beginning iOS unit testing

Unit Test Failures

Monday, April 22, 13

Page 53: Beginning iOS unit testing

Unit Test Failures

Failure messages appear in the issue navigator and the console

Monday, April 22, 13

Page 54: Beginning iOS unit testing

Unit Test Failures

Failure messages appear in the issue navigator and the console

Good test log message go a long way to determining the issue

Monday, April 22, 13

Page 55: Beginning iOS unit testing

Unit Test Failures

Failure messages appear in the issue navigator and the console

Good test log message go a long way to determining the issue

This becomes even more important in apps with large test suites

Monday, April 22, 13

Page 56: Beginning iOS unit testing

Questions?

Monday, April 22, 13

Page 57: Beginning iOS unit testing

Questions?

Thank [email protected]

@ski081 (Twitter/ADN)

http://markstruzinski.com

Monday, April 22, 13