unit testing in swift
TRANSCRIPT
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
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
11
1. Create dummy service classes
2. Replace original classes execution with dummy ones
Simple testing strategy
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
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
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
24
Service locator is a central registry which on request returns necessary information to
perform a certain task.
Service Locator
Definition
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