Swift testing ftw

Jorge Ortiz
Jorge OrtizOwner at PoWWaU
Swift Testing
FTW!
Jorge D. Ortiz-Fuentes
@jdortiz
#SwiftTesting
A Canonical
Examples
production
#SwiftTesting
#SwiftTesting
Agenda
★ Basics about unit testing
★ 4 challenges of Swift Testing
★ Proposed enhancements
Basics about Unit
Testing
But my code is
always awesome!
#SwiftTesting
Unit Tests
★ Prove correctness of different aspects of the
public interface.
• Prove instead of intuition
• Define contract and assumptions
• Document the code
• Easier refactoring or change
★ Reusable code = code + tests
#SwiftTesting
Use Unit Testing
Incrementally
★ You don’t have to write every unit test
★ Start with the classes that take care of the
logic
• If mixed apply SOLID
★ The easier entry point is fixing bugs
Time writing tests
< Time debugging
Ask for your
wishes
#SwiftTesting
Types of Unit Tests
★ Test return value
★ Test state
★ Test behavior
#SwiftTesting
The Rules of Testing
★ We only test our code
★ Only a level of abstraction
★ Only public methods
★ Only one assertion per test
★ Tests are independent of sequence or state
4 Challenges
of Swift
Testing
Lost
#SwiftTesting
New to Swift
★ Still learning the language
★ Functional Paradigm
★ Swift has bugs
#SwiftTesting
Implicitly unwrapped SUT
★ SUT cannot be created in init
★ Thus, it needs to be optional
★ But once set in setUp, it never becomes
nil
★ Syntax is clearer with an implicitly
unwrapped optional.
#SwiftTesting
XCTAssertEquals
★ Works with non custom objects
★ But requires objects to be equatable
★ Use reference comparison instead
#SwiftTesting
func createSut() {
interactor =
ShowAllSpeakersInteractorMock()
sut =
SpeakerListPresenter(interactor:
interactor)
view = SpeakersListViewMock()
sut.view = view
}
func testViewIsPersisted() {
if let persitedView = shut.view as?
SpeakersListViewMock {
XCTAssertTrue(persistedView ===
view, “Wrong view persisted”)
} else {
XCTFail(“View must be persisted”)
}
}
Example: Test
persistence
public class
SpeakersListPresenter {
let interactor:
ShowAllSpeakersInteractorPro
tocol
public weak var view
SpeakersListViewProtocol?
public init(interactor:
ShowAllSpeakersInteractorPro
tocol) {
self.interactor =
interaction
}
}
No Courage
#SwiftTesting
Room for improvement
★ Brian Gesiak: XCTest: The Good Parts:
• Replace/customize Testing frameworks
• XCTAssertThrows
• assert/precondition
• 1,000+ tests
★ I add:
• Run tests without the simulator
• Jon Reid provides a method to speed up AppDelegate launch,
but not for Swift
No Brains
#SwiftTesting
Access control NTFTC
★ It would be nice to have access to internal
properties, but you should only test the public
interface
★ Implicit constructors for structs are internal
★ However, mocks defined in the same test case
can be accessed (internal)
★ If not tested, view controllers may not be
public. But it makes things more complicated.
More on that later.
#SwiftTesting
Create your own
templates
import XCTest
import ___PACKAGENAMEASIDENTIFIER___
class ___FILEBASENAMEASIDENTIFIER___: ___VARIABLE_testSubclass___ {
// MARK: - Parameters & Constants
// MARK: - Test vatiables.
var sut: ___VARIABLE_classUnderTest___!
// MARK: - Set up and tear down
override func setUp() {
super.setUp()
createSut()
}
func createSut() {
sut = ___VARIABLE_classUnderTest___()
}
override func tearDown() {
releaseSut()
super.tearDown()
}
func releaseSut() {
sut = nil
}
No Heart
#SwiftTesting
Dependency Injection
★ Code of an object depends on other
objects.
★ Those are considered dependencies.
★ Dependencies must be controlled in order
to reproduce behavior properly.
#SwiftTesting
Dependency Injection
★ Extract and override: move to a method
and override in testing class (more fragile)
★ Method injection: change the signature of
the method to provide the dependency
★ Property injection: lazy instantiation
★ Constructor injection: not always possible
#SwiftTesting
Stubs & Mocks
★ Both are fake objects
★ Stubs provide desired responses to the
SUT
★ Mocks also expect certain behaviors
OCMock /
OCMockito
Not Available!
#SwiftTesting
Testing with dependency
class ViewController: UIViewController {
@IBOutlet weak var messageLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
let userDefaults = NSUserDefaults.standardUserDefaults()
let score = userDefaults.integerForKey("PreservedScore")
messageLabel.text = String(score)
}
}
#SwiftTesting
func testMessageLabelDisplaysStoredScore() {
var labelMock = LabelMock()
sut.messageLabel = labelMock
sut.userDefaults = UserDefaultsMock()
var view = sut.view
if let text = sut.messageLabel.text {
XCTAssertEqual(text, "1337", "Label must display the
preserved score.")
} else {
XCTFail("Label text must not be nil.")
}
}
class UserDefaultsMock: NSUserDefaults {
override func integerForKey(defaultName: String) ->
Int {
return 1337
}
}
class LabelMock: UILabel {
var presentedText: String?
override internal var text: String? {
get { return presentedText }
set { presentedText = newValue }
}
}
}
Dependency injection
import UIKit
public class ViewController:
UIViewController {
@IBOutlet public weak var
messageLabel: UILabel!
lazy public var userDefaults =
NSUserDefaults.standardUserDefaults()
override public func viewDidLoad()
{
super.viewDidLoad()
let score =
userDefaults.integerForKey("Score")
messageLabel.text =
String(score)
}
}
Let the Architecture
Help You
#SwiftTesting
Clean Architecture
View (VC) Presenter
Wireframe
Interactor Repository
Persistence
WSC
Follow the Clean
Brick Road
canonicalexamples.com
coupon:
APPSTERDAMERS
Thank
you!
@jdortiz
#SwiftTesting
1 of 35

Recommended

Angular Unit Testing by
Angular Unit TestingAngular Unit Testing
Angular Unit TestingAvi Engelshtein
663 views12 slides
Intro to junit by
Intro to junitIntro to junit
Intro to junitRakesh Srivastava
162 views12 slides
Unit testing in xcode 8 with swift by
Unit testing in xcode 8 with swiftUnit testing in xcode 8 with swift
Unit testing in xcode 8 with swiftallanh0526
274 views68 slides
Testing React Applications by
Testing React ApplicationsTesting React Applications
Testing React Applicationsstbaechler
933 views21 slides
How and what to unit test by
How and what to unit testHow and what to unit test
How and what to unit testEugenio Lentini
1.4K views33 slides
Testing Legacy Rails Apps by
Testing Legacy Rails AppsTesting Legacy Rails Apps
Testing Legacy Rails AppsRabble .
4.7K views51 slides

More Related Content

What's hot

Angular Unit Testing from the Trenches by
Angular Unit Testing from the TrenchesAngular Unit Testing from the Trenches
Angular Unit Testing from the TrenchesJustin James
1.1K views190 slides
iOS Unit Testing by
iOS Unit TestingiOS Unit Testing
iOS Unit Testingsgleadow
5.1K views103 slides
Angular Unit Testing by
Angular Unit TestingAngular Unit Testing
Angular Unit TestingAlessandro Giorgetti
2.7K views71 slides
Working With Legacy Code by
Working With Legacy CodeWorking With Legacy Code
Working With Legacy CodeAndrea Polci
3.7K views33 slides
Front end unit testing using jasmine by
Front end unit testing using jasmineFront end unit testing using jasmine
Front end unit testing using jasmineGil Fink
1.2K views28 slides
Unit testing by
Unit testingUnit testing
Unit testingPrabhat Kumar
57 views17 slides

What's hot(20)

Angular Unit Testing from the Trenches by Justin James
Angular Unit Testing from the TrenchesAngular Unit Testing from the Trenches
Angular Unit Testing from the Trenches
Justin James1.1K views
iOS Unit Testing by sgleadow
iOS Unit TestingiOS Unit Testing
iOS Unit Testing
sgleadow5.1K views
Working With Legacy Code by Andrea Polci
Working With Legacy CodeWorking With Legacy Code
Working With Legacy Code
Andrea Polci3.7K views
Front end unit testing using jasmine by Gil Fink
Front end unit testing using jasmineFront end unit testing using jasmine
Front end unit testing using jasmine
Gil Fink1.2K views
Angular Unit Testing NDC Minn 2018 by Justin James
Angular Unit Testing NDC Minn 2018Angular Unit Testing NDC Minn 2018
Angular Unit Testing NDC Minn 2018
Justin James240 views
Quick Tour to Front-End Unit Testing Using Jasmine by Gil Fink
Quick Tour to Front-End Unit Testing Using JasmineQuick Tour to Front-End Unit Testing Using Jasmine
Quick Tour to Front-End Unit Testing Using Jasmine
Gil Fink1.1K views
Client side unit tests - using jasmine & karma by Adam Klein
Client side unit tests - using jasmine & karmaClient side unit tests - using jasmine & karma
Client side unit tests - using jasmine & karma
Adam Klein1K views
Unit Tests And Automated Testing by Lee Englestone
Unit Tests And Automated TestingUnit Tests And Automated Testing
Unit Tests And Automated Testing
Lee Englestone7.7K views
Benefit From Unit Testing In The Real World by Dror Helper
Benefit From Unit Testing In The Real WorldBenefit From Unit Testing In The Real World
Benefit From Unit Testing In The Real World
Dror Helper4.4K views
Functional programming principles and Java 8 by Dragos Balan
Functional programming principles and Java 8Functional programming principles and Java 8
Functional programming principles and Java 8
Dragos Balan1.5K views
An Introduction to Unit Testing by Joe Tremblay
An Introduction to Unit TestingAn Introduction to Unit Testing
An Introduction to Unit Testing
Joe Tremblay3.6K views
Unit-testing and E2E testing in JS by Michael Haberman
Unit-testing and E2E testing in JSUnit-testing and E2E testing in JS
Unit-testing and E2E testing in JS
Michael Haberman5.8K views
Embrace Unit Testing by alessiopace
Embrace Unit TestingEmbrace Unit Testing
Embrace Unit Testing
alessiopace4K views

Viewers also liked

Testing iOS10 Apps with Appium and its new XCUITest backend by
Testing iOS10 Apps with Appium and its new XCUITest backendTesting iOS10 Apps with Appium and its new XCUITest backend
Testing iOS10 Apps with Appium and its new XCUITest backendTestplus GmbH
499 views13 slides
Unit testing in swift 2 - The before & after story by
Unit testing in swift 2 - The before & after storyUnit testing in swift 2 - The before & after story
Unit testing in swift 2 - The before & after storyJorge Ortiz
991 views23 slides
Protocol-Oriented Programming in Swift by
Protocol-Oriented Programming in SwiftProtocol-Oriented Programming in Swift
Protocol-Oriented Programming in SwiftGlobalLogic Ukraine
1K views92 slides
Testing in swift by
Testing in swiftTesting in swift
Testing in swifthugo lu
617 views15 slides
Generating test cases using UML Communication Diagram by
Generating test cases using UML Communication Diagram Generating test cases using UML Communication Diagram
Generating test cases using UML Communication Diagram Praveen Penumathsa
811 views24 slides
Unit Testing in Swift by
Unit Testing in SwiftUnit Testing in Swift
Unit Testing in SwiftGlobalLogic Ukraine
1.3K views34 slides

Viewers also liked(12)

Testing iOS10 Apps with Appium and its new XCUITest backend by Testplus GmbH
Testing iOS10 Apps with Appium and its new XCUITest backendTesting iOS10 Apps with Appium and its new XCUITest backend
Testing iOS10 Apps with Appium and its new XCUITest backend
Testplus GmbH499 views
Unit testing in swift 2 - The before & after story by Jorge Ortiz
Unit testing in swift 2 - The before & after storyUnit testing in swift 2 - The before & after story
Unit testing in swift 2 - The before & after story
Jorge Ortiz991 views
Testing in swift by hugo lu
Testing in swiftTesting in swift
Testing in swift
hugo lu617 views
Generating test cases using UML Communication Diagram by Praveen Penumathsa
Generating test cases using UML Communication Diagram Generating test cases using UML Communication Diagram
Generating test cases using UML Communication Diagram
Praveen Penumathsa811 views
7 Stages of Unit Testing in iOS by Jorge Ortiz
7 Stages of Unit Testing in iOS7 Stages of Unit Testing in iOS
7 Stages of Unit Testing in iOS
Jorge Ortiz805 views
iOS advanced architecture workshop 3h edition by Jorge Ortiz
iOS advanced architecture workshop 3h editioniOS advanced architecture workshop 3h edition
iOS advanced architecture workshop 3h edition
Jorge Ortiz583 views
Unit testing best practices by nickokiss
Unit testing best practicesUnit testing best practices
Unit testing best practices
nickokiss22.3K views
Unit Testing Concepts and Best Practices by Derek Smith
Unit Testing Concepts and Best PracticesUnit Testing Concepts and Best Practices
Unit Testing Concepts and Best Practices
Derek Smith31.8K views
UNIT TESTING PPT by suhasreddy1
UNIT TESTING PPTUNIT TESTING PPT
UNIT TESTING PPT
suhasreddy155.5K views

Similar to Swift testing ftw

Test Driven Development with JavaFX by
Test Driven Development with JavaFXTest Driven Development with JavaFX
Test Driven Development with JavaFXHendrik Ebbers
16.5K views64 slides
Unit Tesing in iOS by
Unit Tesing in iOSUnit Tesing in iOS
Unit Tesing in iOSCiklum Ukraine
1.6K views84 slides
Refactor your way forward by
Refactor your way forwardRefactor your way forward
Refactor your way forwardJorge Ortiz
477 views69 slides
How Testability Inspires AngularJS Design / Ran Mizrahi by
How Testability Inspires AngularJS Design / Ran MizrahiHow Testability Inspires AngularJS Design / Ran Mizrahi
How Testability Inspires AngularJS Design / Ran MizrahiRan Mizrahi
716 views31 slides
Breaking Dependencies to Allow Unit Testing by
Breaking Dependencies to Allow Unit TestingBreaking Dependencies to Allow Unit Testing
Breaking Dependencies to Allow Unit TestingSteven Smith
1.4K views53 slides
Developer Tests - Things to Know (Vilnius JUG) by
Developer Tests - Things to Know (Vilnius JUG)Developer Tests - Things to Know (Vilnius JUG)
Developer Tests - Things to Know (Vilnius JUG)vilniusjug
376 views103 slides

Similar to Swift testing ftw(20)

Test Driven Development with JavaFX by Hendrik Ebbers
Test Driven Development with JavaFXTest Driven Development with JavaFX
Test Driven Development with JavaFX
Hendrik Ebbers16.5K views
Refactor your way forward by Jorge Ortiz
Refactor your way forwardRefactor your way forward
Refactor your way forward
Jorge Ortiz477 views
How Testability Inspires AngularJS Design / Ran Mizrahi by Ran Mizrahi
How Testability Inspires AngularJS Design / Ran MizrahiHow Testability Inspires AngularJS Design / Ran Mizrahi
How Testability Inspires AngularJS Design / Ran Mizrahi
Ran Mizrahi716 views
Breaking Dependencies to Allow Unit Testing by Steven Smith
Breaking Dependencies to Allow Unit TestingBreaking Dependencies to Allow Unit Testing
Breaking Dependencies to Allow Unit Testing
Steven Smith1.4K views
Developer Tests - Things to Know (Vilnius JUG) by vilniusjug
Developer Tests - Things to Know (Vilnius JUG)Developer Tests - Things to Know (Vilnius JUG)
Developer Tests - Things to Know (Vilnius JUG)
vilniusjug376 views
The Many Ways to Test Your React App by All Things Open
The Many Ways to Test Your React AppThe Many Ways to Test Your React App
The Many Ways to Test Your React App
All Things Open2.1K views
Unit testing in php by Sudar Muthu
Unit testing in phpUnit testing in php
Unit testing in php
Sudar Muthu2.1K views
Getting Started with Test-Driven Development at Longhorn PHP 2023 by Scott Keck-Warren
Getting Started with Test-Driven Development at Longhorn PHP 2023Getting Started with Test-Driven Development at Longhorn PHP 2023
Getting Started with Test-Driven Development at Longhorn PHP 2023
Test in action – week 1 by Yi-Huan Chan
Test in action – week 1Test in action – week 1
Test in action – week 1
Yi-Huan Chan1.3K views
java in Aartificial intelligent by virat andodariya by viratandodariya
java in Aartificial intelligent by virat andodariyajava in Aartificial intelligent by virat andodariya
java in Aartificial intelligent by virat andodariya
viratandodariya359 views
7 stages of unit testing by Jorge Ortiz
7 stages of unit testing7 stages of unit testing
7 stages of unit testing
Jorge Ortiz3.6K views
Unit testing legacy code by Lars Thorup
Unit testing legacy codeUnit testing legacy code
Unit testing legacy code
Lars Thorup2K views
Into The Box 2018 | Assert control over your legacy applications by Ortus Solutions, Corp
Into The Box 2018 | Assert control over your legacy applicationsInto The Box 2018 | Assert control over your legacy applications
Into The Box 2018 | Assert control over your legacy applications
Test and Behaviour Driven Development (TDD/BDD) by Lars Thorup
Test and Behaviour Driven Development (TDD/BDD)Test and Behaviour Driven Development (TDD/BDD)
Test and Behaviour Driven Development (TDD/BDD)
Lars Thorup16K views
Oh so you test? - A guide to testing on Android from Unit to Mutation by Paul Blundell
Oh so you test? - A guide to testing on Android from Unit to MutationOh so you test? - A guide to testing on Android from Unit to Mutation
Oh so you test? - A guide to testing on Android from Unit to Mutation
Paul Blundell5.6K views

More from Jorge Ortiz

Tell Me Quando - Implementing Feature Flags by
Tell Me Quando - Implementing Feature FlagsTell Me Quando - Implementing Feature Flags
Tell Me Quando - Implementing Feature FlagsJorge Ortiz
118 views34 slides
Unit Test your Views by
Unit Test your ViewsUnit Test your Views
Unit Test your ViewsJorge Ortiz
222 views24 slides
Control your Voice like a Bene Gesserit by
Control your Voice like a Bene GesseritControl your Voice like a Bene Gesserit
Control your Voice like a Bene GesseritJorge Ortiz
476 views47 slides
Kata gilded rose en Golang by
Kata gilded rose en GolangKata gilded rose en Golang
Kata gilded rose en GolangJorge Ortiz
391 views50 slides
CYA: Cover Your App by
CYA: Cover Your AppCYA: Cover Your App
CYA: Cover Your AppJorge Ortiz
343 views34 slides
201710 Fly Me to the View - iOS Conf SG by
201710 Fly Me to the View - iOS Conf SG201710 Fly Me to the View - iOS Conf SG
201710 Fly Me to the View - iOS Conf SGJorge Ortiz
699 views47 slides

More from Jorge Ortiz(20)

Tell Me Quando - Implementing Feature Flags by Jorge Ortiz
Tell Me Quando - Implementing Feature FlagsTell Me Quando - Implementing Feature Flags
Tell Me Quando - Implementing Feature Flags
Jorge Ortiz118 views
Unit Test your Views by Jorge Ortiz
Unit Test your ViewsUnit Test your Views
Unit Test your Views
Jorge Ortiz222 views
Control your Voice like a Bene Gesserit by Jorge Ortiz
Control your Voice like a Bene GesseritControl your Voice like a Bene Gesserit
Control your Voice like a Bene Gesserit
Jorge Ortiz476 views
Kata gilded rose en Golang by Jorge Ortiz
Kata gilded rose en GolangKata gilded rose en Golang
Kata gilded rose en Golang
Jorge Ortiz391 views
CYA: Cover Your App by Jorge Ortiz
CYA: Cover Your AppCYA: Cover Your App
CYA: Cover Your App
Jorge Ortiz343 views
201710 Fly Me to the View - iOS Conf SG by Jorge Ortiz
201710 Fly Me to the View - iOS Conf SG201710 Fly Me to the View - iOS Conf SG
201710 Fly Me to the View - iOS Conf SG
Jorge Ortiz699 views
Home Improvement: Architecture & Kotlin by Jorge Ortiz
Home Improvement: Architecture & KotlinHome Improvement: Architecture & Kotlin
Home Improvement: Architecture & Kotlin
Jorge Ortiz461 views
Architectural superpowers by Jorge Ortiz
Architectural superpowersArchitectural superpowers
Architectural superpowers
Jorge Ortiz195 views
Architecting Alive Apps by Jorge Ortiz
Architecting Alive AppsArchitecting Alive Apps
Architecting Alive Apps
Jorge Ortiz537 views
Android clean architecture workshop 3h edition by Jorge Ortiz
Android clean architecture workshop 3h editionAndroid clean architecture workshop 3h edition
Android clean architecture workshop 3h edition
Jorge Ortiz743 views
To Protect & To Serve by Jorge Ortiz
To Protect & To ServeTo Protect & To Serve
To Protect & To Serve
Jorge Ortiz925 views
Clean architecture workshop by Jorge Ortiz
Clean architecture workshopClean architecture workshop
Clean architecture workshop
Jorge Ortiz732 views
Escape from Mars by Jorge Ortiz
Escape from MarsEscape from Mars
Escape from Mars
Jorge Ortiz350 views
Why the Dark Side should use Swift and a SOLID Architecture by Jorge Ortiz
Why the Dark Side should use Swift and a SOLID ArchitectureWhy the Dark Side should use Swift and a SOLID Architecture
Why the Dark Side should use Swift and a SOLID Architecture
Jorge Ortiz422 views
Dependence day insurgence by Jorge Ortiz
Dependence day insurgenceDependence day insurgence
Dependence day insurgence
Jorge Ortiz848 views
Architectural superpowers by Jorge Ortiz
Architectural superpowersArchitectural superpowers
Architectural superpowers
Jorge Ortiz333 views
TDD for the masses by Jorge Ortiz
TDD for the massesTDD for the masses
TDD for the masses
Jorge Ortiz706 views
Building for perfection by Jorge Ortiz
Building for perfectionBuilding for perfection
Building for perfection
Jorge Ortiz685 views
TDD by Controlling Dependencies by Jorge Ortiz
TDD by Controlling DependenciesTDD by Controlling Dependencies
TDD by Controlling Dependencies
Jorge Ortiz854 views
Core Data in Modern Times by Jorge Ortiz
Core Data in Modern TimesCore Data in Modern Times
Core Data in Modern Times
Jorge Ortiz1K views

Recently uploaded

Live Demo Showcase: Unveiling Dell PowerFlex’s IaaS Capabilities with Apache ... by
Live Demo Showcase: Unveiling Dell PowerFlex’s IaaS Capabilities with Apache ...Live Demo Showcase: Unveiling Dell PowerFlex’s IaaS Capabilities with Apache ...
Live Demo Showcase: Unveiling Dell PowerFlex’s IaaS Capabilities with Apache ...ShapeBlue
85 views10 slides
Developments to CloudStack’s SDN ecosystem: Integration with VMWare NSX 4 - P... by
Developments to CloudStack’s SDN ecosystem: Integration with VMWare NSX 4 - P...Developments to CloudStack’s SDN ecosystem: Integration with VMWare NSX 4 - P...
Developments to CloudStack’s SDN ecosystem: Integration with VMWare NSX 4 - P...ShapeBlue
154 views62 slides
Declarative Kubernetes Cluster Deployment with Cloudstack and Cluster API - O... by
Declarative Kubernetes Cluster Deployment with Cloudstack and Cluster API - O...Declarative Kubernetes Cluster Deployment with Cloudstack and Cluster API - O...
Declarative Kubernetes Cluster Deployment with Cloudstack and Cluster API - O...ShapeBlue
88 views13 slides
iSAQB Software Architecture Gathering 2023: How Process Orchestration Increas... by
iSAQB Software Architecture Gathering 2023: How Process Orchestration Increas...iSAQB Software Architecture Gathering 2023: How Process Orchestration Increas...
iSAQB Software Architecture Gathering 2023: How Process Orchestration Increas...Bernd Ruecker
50 views69 slides
CloudStack Managed User Data and Demo - Harikrishna Patnala - ShapeBlue by
CloudStack Managed User Data and Demo - Harikrishna Patnala - ShapeBlueCloudStack Managed User Data and Demo - Harikrishna Patnala - ShapeBlue
CloudStack Managed User Data and Demo - Harikrishna Patnala - ShapeBlueShapeBlue
94 views13 slides
Digital Personal Data Protection (DPDP) Practical Approach For CISOs by
Digital Personal Data Protection (DPDP) Practical Approach For CISOsDigital Personal Data Protection (DPDP) Practical Approach For CISOs
Digital Personal Data Protection (DPDP) Practical Approach For CISOsPriyanka Aash
153 views59 slides

Recently uploaded(20)

Live Demo Showcase: Unveiling Dell PowerFlex’s IaaS Capabilities with Apache ... by ShapeBlue
Live Demo Showcase: Unveiling Dell PowerFlex’s IaaS Capabilities with Apache ...Live Demo Showcase: Unveiling Dell PowerFlex’s IaaS Capabilities with Apache ...
Live Demo Showcase: Unveiling Dell PowerFlex’s IaaS Capabilities with Apache ...
ShapeBlue85 views
Developments to CloudStack’s SDN ecosystem: Integration with VMWare NSX 4 - P... by ShapeBlue
Developments to CloudStack’s SDN ecosystem: Integration with VMWare NSX 4 - P...Developments to CloudStack’s SDN ecosystem: Integration with VMWare NSX 4 - P...
Developments to CloudStack’s SDN ecosystem: Integration with VMWare NSX 4 - P...
ShapeBlue154 views
Declarative Kubernetes Cluster Deployment with Cloudstack and Cluster API - O... by ShapeBlue
Declarative Kubernetes Cluster Deployment with Cloudstack and Cluster API - O...Declarative Kubernetes Cluster Deployment with Cloudstack and Cluster API - O...
Declarative Kubernetes Cluster Deployment with Cloudstack and Cluster API - O...
ShapeBlue88 views
iSAQB Software Architecture Gathering 2023: How Process Orchestration Increas... by Bernd Ruecker
iSAQB Software Architecture Gathering 2023: How Process Orchestration Increas...iSAQB Software Architecture Gathering 2023: How Process Orchestration Increas...
iSAQB Software Architecture Gathering 2023: How Process Orchestration Increas...
Bernd Ruecker50 views
CloudStack Managed User Data and Demo - Harikrishna Patnala - ShapeBlue by ShapeBlue
CloudStack Managed User Data and Demo - Harikrishna Patnala - ShapeBlueCloudStack Managed User Data and Demo - Harikrishna Patnala - ShapeBlue
CloudStack Managed User Data and Demo - Harikrishna Patnala - ShapeBlue
ShapeBlue94 views
Digital Personal Data Protection (DPDP) Practical Approach For CISOs by Priyanka Aash
Digital Personal Data Protection (DPDP) Practical Approach For CISOsDigital Personal Data Protection (DPDP) Practical Approach For CISOs
Digital Personal Data Protection (DPDP) Practical Approach For CISOs
Priyanka Aash153 views
Centralized Logging Feature in CloudStack using ELK and Grafana - Kiran Chava... by ShapeBlue
Centralized Logging Feature in CloudStack using ELK and Grafana - Kiran Chava...Centralized Logging Feature in CloudStack using ELK and Grafana - Kiran Chava...
Centralized Logging Feature in CloudStack using ELK and Grafana - Kiran Chava...
ShapeBlue101 views
The Power of Heat Decarbonisation Plans in the Built Environment by IES VE
The Power of Heat Decarbonisation Plans in the Built EnvironmentThe Power of Heat Decarbonisation Plans in the Built Environment
The Power of Heat Decarbonisation Plans in the Built Environment
IES VE69 views
Migrating VMware Infra to KVM Using CloudStack - Nicolas Vazquez - ShapeBlue by ShapeBlue
Migrating VMware Infra to KVM Using CloudStack - Nicolas Vazquez - ShapeBlueMigrating VMware Infra to KVM Using CloudStack - Nicolas Vazquez - ShapeBlue
Migrating VMware Infra to KVM Using CloudStack - Nicolas Vazquez - ShapeBlue
ShapeBlue176 views
GDG Cloud Southlake 28 Brad Taylor and Shawn Augenstein Old Problems in the N... by James Anderson
GDG Cloud Southlake 28 Brad Taylor and Shawn Augenstein Old Problems in the N...GDG Cloud Southlake 28 Brad Taylor and Shawn Augenstein Old Problems in the N...
GDG Cloud Southlake 28 Brad Taylor and Shawn Augenstein Old Problems in the N...
James Anderson156 views
What’s New in CloudStack 4.19 - Abhishek Kumar - ShapeBlue by ShapeBlue
What’s New in CloudStack 4.19 - Abhishek Kumar - ShapeBlueWhat’s New in CloudStack 4.19 - Abhishek Kumar - ShapeBlue
What’s New in CloudStack 4.19 - Abhishek Kumar - ShapeBlue
ShapeBlue222 views
Extending KVM Host HA for Non-NFS Storage - Alex Ivanov - StorPool by ShapeBlue
Extending KVM Host HA for Non-NFS Storage -  Alex Ivanov - StorPoolExtending KVM Host HA for Non-NFS Storage -  Alex Ivanov - StorPool
Extending KVM Host HA for Non-NFS Storage - Alex Ivanov - StorPool
ShapeBlue84 views
The Role of Patterns in the Era of Large Language Models by Yunyao Li
The Role of Patterns in the Era of Large Language ModelsThe Role of Patterns in the Era of Large Language Models
The Role of Patterns in the Era of Large Language Models
Yunyao Li80 views
Business Analyst Series 2023 - Week 4 Session 7 by DianaGray10
Business Analyst Series 2023 -  Week 4 Session 7Business Analyst Series 2023 -  Week 4 Session 7
Business Analyst Series 2023 - Week 4 Session 7
DianaGray10126 views
"Surviving highload with Node.js", Andrii Shumada by Fwdays
"Surviving highload with Node.js", Andrii Shumada "Surviving highload with Node.js", Andrii Shumada
"Surviving highload with Node.js", Andrii Shumada
Fwdays53 views
Keynote Talk: Open Source is Not Dead - Charles Schulz - Vates by ShapeBlue
Keynote Talk: Open Source is Not Dead - Charles Schulz - VatesKeynote Talk: Open Source is Not Dead - Charles Schulz - Vates
Keynote Talk: Open Source is Not Dead - Charles Schulz - Vates
ShapeBlue210 views
Why and How CloudStack at weSystems - Stephan Bienek - weSystems by ShapeBlue
Why and How CloudStack at weSystems - Stephan Bienek - weSystemsWhy and How CloudStack at weSystems - Stephan Bienek - weSystems
Why and How CloudStack at weSystems - Stephan Bienek - weSystems
ShapeBlue197 views
NTGapps NTG LowCode Platform by Mustafa Kuğu
NTGapps NTG LowCode Platform NTGapps NTG LowCode Platform
NTGapps NTG LowCode Platform
Mustafa Kuğu365 views
TrustArc Webinar - Managing Online Tracking Technology Vendors_ A Checklist f... by TrustArc
TrustArc Webinar - Managing Online Tracking Technology Vendors_ A Checklist f...TrustArc Webinar - Managing Online Tracking Technology Vendors_ A Checklist f...
TrustArc Webinar - Managing Online Tracking Technology Vendors_ A Checklist f...
TrustArc160 views
KVM Security Groups Under the Hood - Wido den Hollander - Your.Online by ShapeBlue
KVM Security Groups Under the Hood - Wido den Hollander - Your.OnlineKVM Security Groups Under the Hood - Wido den Hollander - Your.Online
KVM Security Groups Under the Hood - Wido den Hollander - Your.Online
ShapeBlue181 views

Swift testing ftw

  • 1. Swift Testing FTW! Jorge D. Ortiz-Fuentes @jdortiz #SwiftTesting
  • 3. #SwiftTesting Agenda ★ Basics about unit testing ★ 4 challenges of Swift Testing ★ Proposed enhancements
  • 5. But my code is always awesome!
  • 6. #SwiftTesting Unit Tests ★ Prove correctness of different aspects of the public interface. • Prove instead of intuition • Define contract and assumptions • Document the code • Easier refactoring or change ★ Reusable code = code + tests
  • 7. #SwiftTesting Use Unit Testing Incrementally ★ You don’t have to write every unit test ★ Start with the classes that take care of the logic • If mixed apply SOLID ★ The easier entry point is fixing bugs
  • 8. Time writing tests < Time debugging
  • 10. #SwiftTesting Types of Unit Tests ★ Test return value ★ Test state ★ Test behavior
  • 11. #SwiftTesting The Rules of Testing ★ We only test our code ★ Only a level of abstraction ★ Only public methods ★ Only one assertion per test ★ Tests are independent of sequence or state
  • 13. Lost
  • 14. #SwiftTesting New to Swift ★ Still learning the language ★ Functional Paradigm ★ Swift has bugs
  • 15. #SwiftTesting Implicitly unwrapped SUT ★ SUT cannot be created in init ★ Thus, it needs to be optional ★ But once set in setUp, it never becomes nil ★ Syntax is clearer with an implicitly unwrapped optional.
  • 16. #SwiftTesting XCTAssertEquals ★ Works with non custom objects ★ But requires objects to be equatable ★ Use reference comparison instead
  • 17. #SwiftTesting func createSut() { interactor = ShowAllSpeakersInteractorMock() sut = SpeakerListPresenter(interactor: interactor) view = SpeakersListViewMock() sut.view = view } func testViewIsPersisted() { if let persitedView = shut.view as? SpeakersListViewMock { XCTAssertTrue(persistedView === view, “Wrong view persisted”) } else { XCTFail(“View must be persisted”) } } Example: Test persistence public class SpeakersListPresenter { let interactor: ShowAllSpeakersInteractorPro tocol public weak var view SpeakersListViewProtocol? public init(interactor: ShowAllSpeakersInteractorPro tocol) { self.interactor = interaction } }
  • 19. #SwiftTesting Room for improvement ★ Brian Gesiak: XCTest: The Good Parts: • Replace/customize Testing frameworks • XCTAssertThrows • assert/precondition • 1,000+ tests ★ I add: • Run tests without the simulator • Jon Reid provides a method to speed up AppDelegate launch, but not for Swift
  • 21. #SwiftTesting Access control NTFTC ★ It would be nice to have access to internal properties, but you should only test the public interface ★ Implicit constructors for structs are internal ★ However, mocks defined in the same test case can be accessed (internal) ★ If not tested, view controllers may not be public. But it makes things more complicated. More on that later.
  • 22. #SwiftTesting Create your own templates import XCTest import ___PACKAGENAMEASIDENTIFIER___ class ___FILEBASENAMEASIDENTIFIER___: ___VARIABLE_testSubclass___ { // MARK: - Parameters & Constants // MARK: - Test vatiables. var sut: ___VARIABLE_classUnderTest___! // MARK: - Set up and tear down override func setUp() { super.setUp() createSut() } func createSut() { sut = ___VARIABLE_classUnderTest___() } override func tearDown() { releaseSut() super.tearDown() } func releaseSut() { sut = nil }
  • 24. #SwiftTesting Dependency Injection ★ Code of an object depends on other objects. ★ Those are considered dependencies. ★ Dependencies must be controlled in order to reproduce behavior properly.
  • 25. #SwiftTesting Dependency Injection ★ Extract and override: move to a method and override in testing class (more fragile) ★ Method injection: change the signature of the method to provide the dependency ★ Property injection: lazy instantiation ★ Constructor injection: not always possible
  • 26. #SwiftTesting Stubs & Mocks ★ Both are fake objects ★ Stubs provide desired responses to the SUT ★ Mocks also expect certain behaviors
  • 28. #SwiftTesting Testing with dependency class ViewController: UIViewController { @IBOutlet weak var messageLabel: UILabel! override func viewDidLoad() { super.viewDidLoad() let userDefaults = NSUserDefaults.standardUserDefaults() let score = userDefaults.integerForKey("PreservedScore") messageLabel.text = String(score) } }
  • 29. #SwiftTesting func testMessageLabelDisplaysStoredScore() { var labelMock = LabelMock() sut.messageLabel = labelMock sut.userDefaults = UserDefaultsMock() var view = sut.view if let text = sut.messageLabel.text { XCTAssertEqual(text, "1337", "Label must display the preserved score.") } else { XCTFail("Label text must not be nil.") } } class UserDefaultsMock: NSUserDefaults { override func integerForKey(defaultName: String) -> Int { return 1337 } } class LabelMock: UILabel { var presentedText: String? override internal var text: String? { get { return presentedText } set { presentedText = newValue } } } } Dependency injection import UIKit public class ViewController: UIViewController { @IBOutlet public weak var messageLabel: UILabel! lazy public var userDefaults = NSUserDefaults.standardUserDefaults() override public func viewDidLoad() { super.viewDidLoad() let score = userDefaults.integerForKey("Score") messageLabel.text = String(score) } }
  • 31. #SwiftTesting Clean Architecture View (VC) Presenter Wireframe Interactor Repository Persistence WSC