Unit Testing in Swift

34
1 Unit Testing in Swift Andrii Gavrysh

Transcript of Unit Testing in Swift

1

Unit Testing in Swift

Andrii Gavrysh

2

1. Testing and its levels

2. Unit testing

3. Benefits

4. Key characteristics of unit test

5. Mocks and Stubs

6. Dependency injection

7. DI patterns

8. Common mistakes

Agenda

3

Testing and it’s levels

4

Testing and it’s levels

Integration testing answers the question “How do components/modules/systems work

together?”

Unit testing helps ensure that every single unit of our program always works correctly.

Unit testing

Integration testing

5

Unit testing is a testing technique using

which individual modules are tested to

determine if there are any issues by the

developer himself. It is concerned with

functional correctness of the standalone

modules.

Unit testing

• It is a technique

• Performs testing of individual modules

• Using to determine issues in application

units

• Written by developers for developers

Definition Something simpler…

6

• Captures the current state of code

• Reduces bugs when changing the existing functionality

• Improves design and allows better refactoring of code

• Helps to understand code for which tests were written

• Shows the quality of build

Benefits

7

• Independence

• Fast execution

• Test only logic of one unit

Key characteristics of unit test

8

OK

Enough for boring theory!!!

Let’s test something!

9

Guinea pig

Any ideas how to test this?

10

Prepare your mind and code for

testing

11

1. Create dummy service classes

2. Replace original classes execution with dummy ones

Simple testing strategy

12

1. Create dummy service classes

13

There are two types of classes that are used in unit testing for preventing real

code to be executed - mocks and stubs. Both of them have the same interface as

replacing object.

Stub – replaces real class logic with predefined results.

Mock – mimics the behavior of real object and is pre-programmed with some

expectations.

Mocks and Stubs

14

Example

Stub Mock

15

• Setup - Prepare object that is being tested

and its stubs collaborators.

• Exercise - Test the functionality.

• Verify state - Use asserts to check

object's state.

• Teardown - Clean up resources.

Flow during test execution

• Setup - Prepare object that is being

tested.

• Setup expectations - Prepare

expectations in mock that is being used

by primary object.

• Exercise - Test the functionality.

• Verify expectations - Verify that correct

methods have been invoked in mock.

• Verify state - Use asserts to check

object's state.

• Teardown - Clean up resources.

Stub Mock

16

2. Replace original classes

execution with dummy ones

17

Dependency injection is an answer to the question “How?”

Two simple rules:

• Each class-service(dependency) should be passed to your class from outside

• Class must depend on abstraction, not on specific classes.

Dependency injection

18

Three base patterns for Dependency Injection

Constructor Setter

Interface

19

Results

20

Do you remember the initial

implementation?

21

Do you remember the initial implementation?

22

If make it more realistic then…

23

24

Service locator is a central registry which on request returns necessary information to

perform a certain task.

Service Locator

Definition

25

Example

26

OK

What about DBProvider?

27

• Type resolver represents a registry not for class objects, but for class types.

Type resolver

28

Example

29

Usage

30

In tests

31

Common mistakes

32

• Asynchronous calls

• Calls to persistence

• Not clearing singletons state

• Running production ApplicationDelegate

• Focusing on code coverage

• Do not run tests

• Ignore test results

• Commented tests

Bad things list

33

Questions?

34

Thank you