SlideShare a Scribd company logo
Unit Testing in Xcode 8.3.2
with Swift3
Allan Shih
Agenda
● What is Unit Testing?
● Unit Testing in Xcode
● Create a Unit Testing class
● Write a Unit Testing class
● Test Asynchronous Operations
● Faking Objects and Interactions
● Demo
● Reference
What is Unit Testing?
● Unit testing, 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 - Advantages
● Reduces Defects in the Newly developed features
● Reduces bugs when changing the existing functionality.
● Reduces Cost of Testing as defects are captured in very
early phase.
● Improves design and allows better refactoring of code.
● Unit Tests, when integrated with build gives the quality
of the build as well.
Unit Tests Main Properties
● Independent
○ Running a Unit Test should not affect another Unit Test run. Execution
order is not important.
● Fast
○ A test which takes several seconds to run is not a Unit Test and
probably needs a refactor.
● Isolated
○ Unit Test should not depend on external systems. They need to know
their collaborators answers.
Create a project with Unit Tests
Creating a Unit Test Target in an existing project
1. Click the test navigator
2. Click the + button
3. Select “New Unit Test
Target”
4. Create a Unit Test target
Select a host application
Build Settings > Deployment > iOS Deployment Target
Update CocoaPods
● Update a Podfile, and add test target dependencies:
● Run $ pod update in project directory.
● Open App.xcworkspace and build.
target 'ThingMakerMvc' do
use_frameworks!
pod 'Alamofire', '~> 4.3'
target 'ThingMakerMvcTests' do
inherit! :search_paths
end
end
Create a Unit Test Case Class
Select a test target
Default Unit Test Case Class
Run the test class
1. ProductTest or Command-U. This actually runs all test
classes.
2. Click the arrow button in the test navigator.
3. Click the diamond button in the gutter.
Unit Test Result
Unit Test Failed
Demo
Unit Testing in Xcode
Unit Test Class Structure
● Class Setup/TearDown
○ Executed once when the Unit Test class is initialized, not for every test
in the class. Useful to avoid test code duplication at class level.
● Test Setup/TearDown
○ Executed once before and after each executed Unit Test. Useful to
avoid test code duplication at test level.
● Test Methods
○ The Unit Test implementation itself. Here is where the test run and
validations happens.
Class Setup Example
Test Setup / Tear Down Example
@available(iOS 9.0, *) UIViewController.loadViewIfNeeded()
Test Method example
Tests Naming
● Use descriptive test names. It will provide quick
feedback when test fails.
● Choose names which in few words says:
○ Method under testing
○ Condition under testing
○ Expected result / behavior
Tests Naming Example
● Good examples
○ testSyncLullabyList_withLullabyManager_return3LullabyItems()
○ testSyncStationList_withIHeartRadioApi_return6StationItems()
● Bad examples
○ testExample1()
○ testExample2()
○ testExample3()
Test Method Organization
● Arrange / Setup
○ Prepare all needed objects/dependencies for test execution.
● Act / Test
○ Test itself it performed, calling one specific method in the system
under testing.
● Assert / Verify
○ Method result/behavior verification is performed at the end, using test
asserts.
Tests Verification Types
● Return Value / State Verification
○ Verify the result returned for the method under test, no matter which
interactions with other objects has been made. Useful and preferred
verification in most cases.
● Behavior Verification
○ Verify that all tested method interactions among other collaboration
objects are the expected one (generally done using mocks). Useful for
some cases where interactions are important (for example a cache
implementation).
Return Value Verification
Return Value Verification Example
State Verification
State Verification Example
Behavior Verification
Behavior Verification
Behavior Verification
Behavior Verification Example
XCTAssert Family in XCode 8.3.2
XCTAssert Family Description
XCTAssert Asserts that an expression is true.
XCTAssertTrue Asserts that an expression is true.
XCTAssertFalse Asserts that an expression is false.
XCTAssertNil Asserts that an expression is nil.
XCTAssertNotNil Asserts that an expression is not nil.
XCTAssertEqual Asserts that two expressions have the same value.
XCTAssertEqualObjects Asserts that two objects are considered equal.
XCTAssert Family in XCode 8.3.2
XCTAssert Family Description
XCTAssertNotEqual Asserts that two expressions do not have the same value.
XCTAssertNotEqualObjects Asserts that an expression is true.
XCTAssertGreaterThan Asserts that the value of one expression is greater than
another.
XCTAssertGreaterThanOrEqual Asserts that the value of one expression is greater than or
equal to another.
XCTAssertLessThan Asserts that the value of one expression is less than
another.
XCTAssertLessThanOrEqual Asserts that the value of one expression is less than or
equal to another.
XCTAssert Family in XCode 8.3.2
XCTAssert Family Description
XCTAssertThrowsError Asserts that an expression throws an error.
XCTAssertNoThrow Asserts that an expression does not throw an
NSException.
XCTAssertEqual example
XCTAssertNotNil Example
XCTAssertThrowsError Example
Demo
Write a Unit Testing class
XCTestExpectation
● Define an expectation with a meaningful description.
● Go on with the test setup and exercise phases, calling
the asynchronous method and fulfilling the expectation
at the end of the callback closure.
● Make the test runner wait for you expectation to be
fulfilled, so that the asynchronous operations can be
completed and you assertions verified.
XCTestExpectation Example
Fail Faster Example
Demo
Test Asynchronous Operations
Asynchronous Tests Problem
● Most apps interact with system or library objects, but
you don’t control these objects.
● Tests that interact with these objects can be slow and
unrepeatable.
● The singletons made our class hard to test.
Dependency Injection
Dependency Injection
● Extract and Override
○ Create a testing subclass and override the testing method.
● Method Injection
○ Pass a dependency object during a method.
● Property Injection
○ An internal or public property can be modified or replaced.
● Constructor Injection
○ Pass a dependency object during the constructor.
Sync Lullaby with LullabyManager
Extract and Override Example - Create a Mock Class
Extract and Override Example - Verifying Lullaby List
Method Injection Example - Create a property that holds a reference
Method Injection Example - Dependency object can be replaced
Method Injection Example - Use a mock LullabyManager object
Property Injection Example - Create a internal or public property
Property Injection Example - Use a mock LullabyManager object
Constructor Injection Example - Default Constructor
Constructor Injection Example - Dependency object can be replaced
Constructor Injection Example - Use a mock LullabyManager object
Demo
Dependency Injection
Test Doubles
Types of Test Doubles
● Stub
○ Fakes a response to method calls of an object.
● Mock
○ Let you check if a method call is performed or if a property is
set.
Stub
Stub Example
Mock
Mock Example - Create a Mock Class
Mock Example
Demo
Test Doubles
Reference
● About Testing with Xcode - Apple
● Unit Testing in Xcode 7 with Swift - AppCoda
● iOS Unit Testing and UI Testing Tutorial - RayWenderlich
● Test Doubles: Mocks, Stubs, and More - objc
● MCE 2014: Jon Reid - Test Driven Development for iOS
● Waiting in XCTest
Ad

More Related Content

What's hot (20)

Selenium Handbook
Selenium HandbookSelenium Handbook
Selenium Handbook
Suresh Thammishetty
 
Junit
JunitJunit
Junit
FAROOK Samath
 
Selenium (1)
Selenium (1)Selenium (1)
Selenium (1)
onlinemindq
 
How to go about testing in React?
How to go about testing in React? How to go about testing in React?
How to go about testing in React?
Lisa Gagarina
 
Selenium Concepts
Selenium ConceptsSelenium Concepts
Selenium Concepts
Swati Bansal
 
Test case development
Test case developmentTest case development
Test case development
Hrushikesh Wakhle
 
Testing Microservices
Testing MicroservicesTesting Microservices
Testing Microservices
Nagarro
 
Introduction of Xcode
Introduction of XcodeIntroduction of Xcode
Introduction of Xcode
Dhaval Kaneria
 
Unit tests & TDD
Unit tests & TDDUnit tests & TDD
Unit tests & TDD
Dror Helper
 
Selenium Grid
Selenium GridSelenium Grid
Selenium Grid
nirvdrum
 
Selenium introduction
Selenium introductionSelenium introduction
Selenium introduction
Deepak Kumar Digar
 
Tempest scenariotests 20140512
Tempest scenariotests 20140512Tempest scenariotests 20140512
Tempest scenariotests 20140512
Masayuki Igawa
 
Practica interprete de comandos de windows
Practica interprete de comandos de windowsPractica interprete de comandos de windows
Practica interprete de comandos de windows
Ery Kñz
 
Automation Testing by Selenium Web Driver
Automation Testing by Selenium Web DriverAutomation Testing by Selenium Web Driver
Automation Testing by Selenium Web Driver
Cuelogic Technologies Pvt. Ltd.
 
Introduction to jest
Introduction to jestIntroduction to jest
Introduction to jest
pksjce
 
Selenium IDE Tutorial For Beginners | Selenium IDE Tutorial | What Is Seleniu...
Selenium IDE Tutorial For Beginners | Selenium IDE Tutorial | What Is Seleniu...Selenium IDE Tutorial For Beginners | Selenium IDE Tutorial | What Is Seleniu...
Selenium IDE Tutorial For Beginners | Selenium IDE Tutorial | What Is Seleniu...
Simplilearn
 
Selenium
SeleniumSelenium
Selenium
Adam Goucher
 
Selenium WebDriver
Selenium WebDriverSelenium WebDriver
Selenium WebDriver
Yuriy Bezgachnyuk
 
Understanding Unit Testing
Understanding Unit TestingUnderstanding Unit Testing
Understanding Unit Testing
ikhwanhayat
 
QSpiders - Automation using Selenium
QSpiders - Automation using SeleniumQSpiders - Automation using Selenium
QSpiders - Automation using Selenium
Qspiders - Software Testing Training Institute
 
How to go about testing in React?
How to go about testing in React? How to go about testing in React?
How to go about testing in React?
Lisa Gagarina
 
Testing Microservices
Testing MicroservicesTesting Microservices
Testing Microservices
Nagarro
 
Unit tests & TDD
Unit tests & TDDUnit tests & TDD
Unit tests & TDD
Dror Helper
 
Selenium Grid
Selenium GridSelenium Grid
Selenium Grid
nirvdrum
 
Tempest scenariotests 20140512
Tempest scenariotests 20140512Tempest scenariotests 20140512
Tempest scenariotests 20140512
Masayuki Igawa
 
Practica interprete de comandos de windows
Practica interprete de comandos de windowsPractica interprete de comandos de windows
Practica interprete de comandos de windows
Ery Kñz
 
Introduction to jest
Introduction to jestIntroduction to jest
Introduction to jest
pksjce
 
Selenium IDE Tutorial For Beginners | Selenium IDE Tutorial | What Is Seleniu...
Selenium IDE Tutorial For Beginners | Selenium IDE Tutorial | What Is Seleniu...Selenium IDE Tutorial For Beginners | Selenium IDE Tutorial | What Is Seleniu...
Selenium IDE Tutorial For Beginners | Selenium IDE Tutorial | What Is Seleniu...
Simplilearn
 
Understanding Unit Testing
Understanding Unit TestingUnderstanding Unit Testing
Understanding Unit Testing
ikhwanhayat
 

Similar to Unit testing in xcode 8 with swift (20)

Unit test documentation
Unit test documentationUnit test documentation
Unit test documentation
Anjan Debnath
 
Java Unit Test - JUnit
Java Unit Test - JUnitJava Unit Test - JUnit
Java Unit Test - JUnit
Aktuğ Urun
 
Unit testing basic
Unit testing basicUnit testing basic
Unit testing basic
Yuri Anischenko
 
Unit testing
Unit testingUnit testing
Unit testing
Panos Pnevmatikatos
 
Software Testing
Software TestingSoftware Testing
Software Testing
AdroitLogic
 
Unit testing basics with NUnit and Visual Studio
Unit testing basics with NUnit and Visual StudioUnit testing basics with NUnit and Visual Studio
Unit testing basics with NUnit and Visual Studio
Amit Choudhary
 
Testing Spark and Scala
Testing Spark and ScalaTesting Spark and Scala
Testing Spark and Scala
datamantra
 
Unit Testing & TDD Training for Mobile Apps
Unit Testing & TDD Training for Mobile AppsUnit Testing & TDD Training for Mobile Apps
Unit Testing & TDD Training for Mobile Apps
Marcelo Busico
 
JUnit Test Case With Processminer modules.pptx
JUnit Test Case With Processminer modules.pptxJUnit Test Case With Processminer modules.pptx
JUnit Test Case With Processminer modules.pptx
pateljeel24
 
Unit Testing in Angular
Unit Testing in AngularUnit Testing in Angular
Unit Testing in Angular
Knoldus Inc.
 
Unit Testing Full@
Unit Testing Full@Unit Testing Full@
Unit Testing Full@
Alex Borsuk
 
Unit testing
Unit testingUnit testing
Unit testing
Vinod Wilson
 
Embrace Unit Testing
Embrace Unit TestingEmbrace Unit Testing
Embrace Unit Testing
alessiopace
 
Unit testing
Unit testingUnit testing
Unit testing
Murugesan Nataraj
 
Object Oriented Testing Strategy.pptx
Object Oriented Testing    Strategy.pptxObject Oriented Testing    Strategy.pptx
Object Oriented Testing Strategy.pptx
ZakriyaMalik2
 
Unit testing (Exploring the other side as a tester)
Unit testing (Exploring the other side as a tester)Unit testing (Exploring the other side as a tester)
Unit testing (Exploring the other side as a tester)
Abhijeet Vaikar
 
Unit Testing
Unit TestingUnit Testing
Unit Testing
Sergey Podolsky
 
junit-160729073220 eclipse software testing.pdf
junit-160729073220 eclipse software testing.pdfjunit-160729073220 eclipse software testing.pdf
junit-160729073220 eclipse software testing.pdf
KomalSinghGill
 
Unit testing in Unity
Unit testing in UnityUnit testing in Unity
Unit testing in Unity
Mikko McMenamin
 
Unit Testing in .NET Core 7.0 with XUnit.pptx
Unit Testing in .NET Core 7.0 with XUnit.pptxUnit Testing in .NET Core 7.0 with XUnit.pptx
Unit Testing in .NET Core 7.0 with XUnit.pptx
Knoldus Inc.
 
Unit test documentation
Unit test documentationUnit test documentation
Unit test documentation
Anjan Debnath
 
Java Unit Test - JUnit
Java Unit Test - JUnitJava Unit Test - JUnit
Java Unit Test - JUnit
Aktuğ Urun
 
Software Testing
Software TestingSoftware Testing
Software Testing
AdroitLogic
 
Unit testing basics with NUnit and Visual Studio
Unit testing basics with NUnit and Visual StudioUnit testing basics with NUnit and Visual Studio
Unit testing basics with NUnit and Visual Studio
Amit Choudhary
 
Testing Spark and Scala
Testing Spark and ScalaTesting Spark and Scala
Testing Spark and Scala
datamantra
 
Unit Testing & TDD Training for Mobile Apps
Unit Testing & TDD Training for Mobile AppsUnit Testing & TDD Training for Mobile Apps
Unit Testing & TDD Training for Mobile Apps
Marcelo Busico
 
JUnit Test Case With Processminer modules.pptx
JUnit Test Case With Processminer modules.pptxJUnit Test Case With Processminer modules.pptx
JUnit Test Case With Processminer modules.pptx
pateljeel24
 
Unit Testing in Angular
Unit Testing in AngularUnit Testing in Angular
Unit Testing in Angular
Knoldus Inc.
 
Unit Testing Full@
Unit Testing Full@Unit Testing Full@
Unit Testing Full@
Alex Borsuk
 
Embrace Unit Testing
Embrace Unit TestingEmbrace Unit Testing
Embrace Unit Testing
alessiopace
 
Object Oriented Testing Strategy.pptx
Object Oriented Testing    Strategy.pptxObject Oriented Testing    Strategy.pptx
Object Oriented Testing Strategy.pptx
ZakriyaMalik2
 
Unit testing (Exploring the other side as a tester)
Unit testing (Exploring the other side as a tester)Unit testing (Exploring the other side as a tester)
Unit testing (Exploring the other side as a tester)
Abhijeet Vaikar
 
junit-160729073220 eclipse software testing.pdf
junit-160729073220 eclipse software testing.pdfjunit-160729073220 eclipse software testing.pdf
junit-160729073220 eclipse software testing.pdf
KomalSinghGill
 
Unit Testing in .NET Core 7.0 with XUnit.pptx
Unit Testing in .NET Core 7.0 with XUnit.pptxUnit Testing in .NET Core 7.0 with XUnit.pptx
Unit Testing in .NET Core 7.0 with XUnit.pptx
Knoldus Inc.
 
Ad

More from allanh0526 (18)

Webp
WebpWebp
Webp
allanh0526
 
Digital authentication
Digital authenticationDigital authentication
Digital authentication
allanh0526
 
Integration of slather and jenkins
Integration of slather and jenkinsIntegration of slather and jenkins
Integration of slather and jenkins
allanh0526
 
How to generate code coverage reports in xcode with slather
How to generate code coverage reports in xcode with slatherHow to generate code coverage reports in xcode with slather
How to generate code coverage reports in xcode with slather
allanh0526
 
Ui testing in xcode
Ui testing in xcodeUi testing in xcode
Ui testing in xcode
allanh0526
 
How to work with dates and times in swift 3
How to work with dates and times in swift 3How to work with dates and times in swift 3
How to work with dates and times in swift 3
allanh0526
 
Using a model view-view model architecture for iOS apps
Using a model view-view model architecture for iOS appsUsing a model view-view model architecture for iOS apps
Using a model view-view model architecture for iOS apps
allanh0526
 
iOS architecture patterns
iOS architecture patternsiOS architecture patterns
iOS architecture patterns
allanh0526
 
ThingMaker in Swift
ThingMaker in SwiftThingMaker in Swift
ThingMaker in Swift
allanh0526
 
Automatic reference counting in Swift
Automatic reference counting in SwiftAutomatic reference counting in Swift
Automatic reference counting in Swift
allanh0526
 
Core data in Swfit
Core data in SwfitCore data in Swfit
Core data in Swfit
allanh0526
 
From android/java to swift (3)
From android/java to swift (3)From android/java to swift (3)
From android/java to swift (3)
allanh0526
 
From android/ java to swift (2)
From android/ java to swift (2)From android/ java to swift (2)
From android/ java to swift (2)
allanh0526
 
From android/java to swift (1)
From android/java to swift (1)From android/java to swift (1)
From android/java to swift (1)
allanh0526
 
WebRTC
WebRTCWebRTC
WebRTC
allanh0526
 
Pipeline interface
Pipeline interfacePipeline interface
Pipeline interface
allanh0526
 
Deploying artifacts to archiva
Deploying artifacts to archivaDeploying artifacts to archiva
Deploying artifacts to archiva
allanh0526
 
Android httpclient
Android httpclientAndroid httpclient
Android httpclient
allanh0526
 
Digital authentication
Digital authenticationDigital authentication
Digital authentication
allanh0526
 
Integration of slather and jenkins
Integration of slather and jenkinsIntegration of slather and jenkins
Integration of slather and jenkins
allanh0526
 
How to generate code coverage reports in xcode with slather
How to generate code coverage reports in xcode with slatherHow to generate code coverage reports in xcode with slather
How to generate code coverage reports in xcode with slather
allanh0526
 
Ui testing in xcode
Ui testing in xcodeUi testing in xcode
Ui testing in xcode
allanh0526
 
How to work with dates and times in swift 3
How to work with dates and times in swift 3How to work with dates and times in swift 3
How to work with dates and times in swift 3
allanh0526
 
Using a model view-view model architecture for iOS apps
Using a model view-view model architecture for iOS appsUsing a model view-view model architecture for iOS apps
Using a model view-view model architecture for iOS apps
allanh0526
 
iOS architecture patterns
iOS architecture patternsiOS architecture patterns
iOS architecture patterns
allanh0526
 
ThingMaker in Swift
ThingMaker in SwiftThingMaker in Swift
ThingMaker in Swift
allanh0526
 
Automatic reference counting in Swift
Automatic reference counting in SwiftAutomatic reference counting in Swift
Automatic reference counting in Swift
allanh0526
 
Core data in Swfit
Core data in SwfitCore data in Swfit
Core data in Swfit
allanh0526
 
From android/java to swift (3)
From android/java to swift (3)From android/java to swift (3)
From android/java to swift (3)
allanh0526
 
From android/ java to swift (2)
From android/ java to swift (2)From android/ java to swift (2)
From android/ java to swift (2)
allanh0526
 
From android/java to swift (1)
From android/java to swift (1)From android/java to swift (1)
From android/java to swift (1)
allanh0526
 
Pipeline interface
Pipeline interfacePipeline interface
Pipeline interface
allanh0526
 
Deploying artifacts to archiva
Deploying artifacts to archivaDeploying artifacts to archiva
Deploying artifacts to archiva
allanh0526
 
Android httpclient
Android httpclientAndroid httpclient
Android httpclient
allanh0526
 
Ad

Recently uploaded (20)

Understanding SEO in the Age of AI.pdf
Understanding SEO in the Age of AI.pdfUnderstanding SEO in the Age of AI.pdf
Understanding SEO in the Age of AI.pdf
Fulcrum Concepts, LLC
 
MEMS IC Substrate Technologies Guide 2025.pptx
MEMS IC Substrate Technologies Guide 2025.pptxMEMS IC Substrate Technologies Guide 2025.pptx
MEMS IC Substrate Technologies Guide 2025.pptx
IC substrate Shawn Wang
 
How to Build an AI-Powered App: Tools, Techniques, and Trends
How to Build an AI-Powered App: Tools, Techniques, and TrendsHow to Build an AI-Powered App: Tools, Techniques, and Trends
How to Build an AI-Powered App: Tools, Techniques, and Trends
Nascenture
 
ICDCC 2025: Securing Agentic AI - Eryk Budi Pratama.pdf
ICDCC 2025: Securing Agentic AI - Eryk Budi Pratama.pdfICDCC 2025: Securing Agentic AI - Eryk Budi Pratama.pdf
ICDCC 2025: Securing Agentic AI - Eryk Budi Pratama.pdf
Eryk Budi Pratama
 
Kit-Works Team Study_팀스터디_김한솔_nuqs_20250509.pdf
Kit-Works Team Study_팀스터디_김한솔_nuqs_20250509.pdfKit-Works Team Study_팀스터디_김한솔_nuqs_20250509.pdf
Kit-Works Team Study_팀스터디_김한솔_nuqs_20250509.pdf
Wonjun Hwang
 
Unlocking Generative AI in your Web Apps
Unlocking Generative AI in your Web AppsUnlocking Generative AI in your Web Apps
Unlocking Generative AI in your Web Apps
Maximiliano Firtman
 
Top 5 Qualities to Look for in Salesforce Partners in 2025
Top 5 Qualities to Look for in Salesforce Partners in 2025Top 5 Qualities to Look for in Salesforce Partners in 2025
Top 5 Qualities to Look for in Salesforce Partners in 2025
Damco Salesforce Services
 
Reimagine How You and Your Team Work with Microsoft 365 Copilot.pptx
Reimagine How You and Your Team Work with Microsoft 365 Copilot.pptxReimagine How You and Your Team Work with Microsoft 365 Copilot.pptx
Reimagine How You and Your Team Work with Microsoft 365 Copilot.pptx
John Moore
 
Could Virtual Threads cast away the usage of Kotlin Coroutines - DevoxxUK2025
Could Virtual Threads cast away the usage of Kotlin Coroutines - DevoxxUK2025Could Virtual Threads cast away the usage of Kotlin Coroutines - DevoxxUK2025
Could Virtual Threads cast away the usage of Kotlin Coroutines - DevoxxUK2025
João Esperancinha
 
ACE Aarhus - Team'25 wrap-up presentation
ACE Aarhus - Team'25 wrap-up presentationACE Aarhus - Team'25 wrap-up presentation
ACE Aarhus - Team'25 wrap-up presentation
DanielEriksen5
 
AI x Accessibility UXPA by Stew Smith and Olivier Vroom
AI x Accessibility UXPA by Stew Smith and Olivier VroomAI x Accessibility UXPA by Stew Smith and Olivier Vroom
AI x Accessibility UXPA by Stew Smith and Olivier Vroom
UXPA Boston
 
Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...
Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...
Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...
Safe Software
 
Cybersecurity Tools and Technologies - Microsoft Certificate
Cybersecurity Tools and Technologies - Microsoft CertificateCybersecurity Tools and Technologies - Microsoft Certificate
Cybersecurity Tools and Technologies - Microsoft Certificate
VICTOR MAESTRE RAMIREZ
 
Harmonizing Multi-Agent Intelligence | Open Data Science Conference | Gary Ar...
Harmonizing Multi-Agent Intelligence | Open Data Science Conference | Gary Ar...Harmonizing Multi-Agent Intelligence | Open Data Science Conference | Gary Ar...
Harmonizing Multi-Agent Intelligence | Open Data Science Conference | Gary Ar...
Gary Arora
 
Shoehorning dependency injection into a FP language, what does it take?
Shoehorning dependency injection into a FP language, what does it take?Shoehorning dependency injection into a FP language, what does it take?
Shoehorning dependency injection into a FP language, what does it take?
Eric Torreborre
 
May Patch Tuesday
May Patch TuesdayMay Patch Tuesday
May Patch Tuesday
Ivanti
 
Digital Technologies for Culture, Arts and Heritage: Insights from Interdisci...
Digital Technologies for Culture, Arts and Heritage: Insights from Interdisci...Digital Technologies for Culture, Arts and Heritage: Insights from Interdisci...
Digital Technologies for Culture, Arts and Heritage: Insights from Interdisci...
Vasileios Komianos
 
Building the Customer Identity Community, Together.pdf
Building the Customer Identity Community, Together.pdfBuilding the Customer Identity Community, Together.pdf
Building the Customer Identity Community, Together.pdf
Cheryl Hung
 
Why Slack Should Be Your Next Business Tool? (Tips to Make Most out of Slack)
Why Slack Should Be Your Next Business Tool? (Tips to Make Most out of Slack)Why Slack Should Be Your Next Business Tool? (Tips to Make Most out of Slack)
Why Slack Should Be Your Next Business Tool? (Tips to Make Most out of Slack)
Cyntexa
 
Crazy Incentives and How They Kill Security. How Do You Turn the Wheel?
Crazy Incentives and How They Kill Security. How Do You Turn the Wheel?Crazy Incentives and How They Kill Security. How Do You Turn the Wheel?
Crazy Incentives and How They Kill Security. How Do You Turn the Wheel?
Christian Folini
 
Understanding SEO in the Age of AI.pdf
Understanding SEO in the Age of AI.pdfUnderstanding SEO in the Age of AI.pdf
Understanding SEO in the Age of AI.pdf
Fulcrum Concepts, LLC
 
MEMS IC Substrate Technologies Guide 2025.pptx
MEMS IC Substrate Technologies Guide 2025.pptxMEMS IC Substrate Technologies Guide 2025.pptx
MEMS IC Substrate Technologies Guide 2025.pptx
IC substrate Shawn Wang
 
How to Build an AI-Powered App: Tools, Techniques, and Trends
How to Build an AI-Powered App: Tools, Techniques, and TrendsHow to Build an AI-Powered App: Tools, Techniques, and Trends
How to Build an AI-Powered App: Tools, Techniques, and Trends
Nascenture
 
ICDCC 2025: Securing Agentic AI - Eryk Budi Pratama.pdf
ICDCC 2025: Securing Agentic AI - Eryk Budi Pratama.pdfICDCC 2025: Securing Agentic AI - Eryk Budi Pratama.pdf
ICDCC 2025: Securing Agentic AI - Eryk Budi Pratama.pdf
Eryk Budi Pratama
 
Kit-Works Team Study_팀스터디_김한솔_nuqs_20250509.pdf
Kit-Works Team Study_팀스터디_김한솔_nuqs_20250509.pdfKit-Works Team Study_팀스터디_김한솔_nuqs_20250509.pdf
Kit-Works Team Study_팀스터디_김한솔_nuqs_20250509.pdf
Wonjun Hwang
 
Unlocking Generative AI in your Web Apps
Unlocking Generative AI in your Web AppsUnlocking Generative AI in your Web Apps
Unlocking Generative AI in your Web Apps
Maximiliano Firtman
 
Top 5 Qualities to Look for in Salesforce Partners in 2025
Top 5 Qualities to Look for in Salesforce Partners in 2025Top 5 Qualities to Look for in Salesforce Partners in 2025
Top 5 Qualities to Look for in Salesforce Partners in 2025
Damco Salesforce Services
 
Reimagine How You and Your Team Work with Microsoft 365 Copilot.pptx
Reimagine How You and Your Team Work with Microsoft 365 Copilot.pptxReimagine How You and Your Team Work with Microsoft 365 Copilot.pptx
Reimagine How You and Your Team Work with Microsoft 365 Copilot.pptx
John Moore
 
Could Virtual Threads cast away the usage of Kotlin Coroutines - DevoxxUK2025
Could Virtual Threads cast away the usage of Kotlin Coroutines - DevoxxUK2025Could Virtual Threads cast away the usage of Kotlin Coroutines - DevoxxUK2025
Could Virtual Threads cast away the usage of Kotlin Coroutines - DevoxxUK2025
João Esperancinha
 
ACE Aarhus - Team'25 wrap-up presentation
ACE Aarhus - Team'25 wrap-up presentationACE Aarhus - Team'25 wrap-up presentation
ACE Aarhus - Team'25 wrap-up presentation
DanielEriksen5
 
AI x Accessibility UXPA by Stew Smith and Olivier Vroom
AI x Accessibility UXPA by Stew Smith and Olivier VroomAI x Accessibility UXPA by Stew Smith and Olivier Vroom
AI x Accessibility UXPA by Stew Smith and Olivier Vroom
UXPA Boston
 
Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...
Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...
Integrating FME with Python: Tips, Demos, and Best Practices for Powerful Aut...
Safe Software
 
Cybersecurity Tools and Technologies - Microsoft Certificate
Cybersecurity Tools and Technologies - Microsoft CertificateCybersecurity Tools and Technologies - Microsoft Certificate
Cybersecurity Tools and Technologies - Microsoft Certificate
VICTOR MAESTRE RAMIREZ
 
Harmonizing Multi-Agent Intelligence | Open Data Science Conference | Gary Ar...
Harmonizing Multi-Agent Intelligence | Open Data Science Conference | Gary Ar...Harmonizing Multi-Agent Intelligence | Open Data Science Conference | Gary Ar...
Harmonizing Multi-Agent Intelligence | Open Data Science Conference | Gary Ar...
Gary Arora
 
Shoehorning dependency injection into a FP language, what does it take?
Shoehorning dependency injection into a FP language, what does it take?Shoehorning dependency injection into a FP language, what does it take?
Shoehorning dependency injection into a FP language, what does it take?
Eric Torreborre
 
May Patch Tuesday
May Patch TuesdayMay Patch Tuesday
May Patch Tuesday
Ivanti
 
Digital Technologies for Culture, Arts and Heritage: Insights from Interdisci...
Digital Technologies for Culture, Arts and Heritage: Insights from Interdisci...Digital Technologies for Culture, Arts and Heritage: Insights from Interdisci...
Digital Technologies for Culture, Arts and Heritage: Insights from Interdisci...
Vasileios Komianos
 
Building the Customer Identity Community, Together.pdf
Building the Customer Identity Community, Together.pdfBuilding the Customer Identity Community, Together.pdf
Building the Customer Identity Community, Together.pdf
Cheryl Hung
 
Why Slack Should Be Your Next Business Tool? (Tips to Make Most out of Slack)
Why Slack Should Be Your Next Business Tool? (Tips to Make Most out of Slack)Why Slack Should Be Your Next Business Tool? (Tips to Make Most out of Slack)
Why Slack Should Be Your Next Business Tool? (Tips to Make Most out of Slack)
Cyntexa
 
Crazy Incentives and How They Kill Security. How Do You Turn the Wheel?
Crazy Incentives and How They Kill Security. How Do You Turn the Wheel?Crazy Incentives and How They Kill Security. How Do You Turn the Wheel?
Crazy Incentives and How They Kill Security. How Do You Turn the Wheel?
Christian Folini
 

Unit testing in xcode 8 with swift

  • 1. Unit Testing in Xcode 8.3.2 with Swift3 Allan Shih
  • 2. Agenda ● What is Unit Testing? ● Unit Testing in Xcode ● Create a Unit Testing class ● Write a Unit Testing class ● Test Asynchronous Operations ● Faking Objects and Interactions ● Demo ● Reference
  • 3. What is Unit Testing? ● Unit testing, 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.
  • 4. Unit Testing - Advantages ● Reduces Defects in the Newly developed features ● Reduces bugs when changing the existing functionality. ● Reduces Cost of Testing as defects are captured in very early phase. ● Improves design and allows better refactoring of code. ● Unit Tests, when integrated with build gives the quality of the build as well.
  • 5. Unit Tests Main Properties ● Independent ○ Running a Unit Test should not affect another Unit Test run. Execution order is not important. ● Fast ○ A test which takes several seconds to run is not a Unit Test and probably needs a refactor. ● Isolated ○ Unit Test should not depend on external systems. They need to know their collaborators answers.
  • 6. Create a project with Unit Tests
  • 7. Creating a Unit Test Target in an existing project 1. Click the test navigator 2. Click the + button 3. Select “New Unit Test Target” 4. Create a Unit Test target
  • 8. Select a host application
  • 9. Build Settings > Deployment > iOS Deployment Target
  • 10. Update CocoaPods ● Update a Podfile, and add test target dependencies: ● Run $ pod update in project directory. ● Open App.xcworkspace and build. target 'ThingMakerMvc' do use_frameworks! pod 'Alamofire', '~> 4.3' target 'ThingMakerMvcTests' do inherit! :search_paths end end
  • 11. Create a Unit Test Case Class
  • 12. Select a test target
  • 13. Default Unit Test Case Class
  • 14. Run the test class 1. ProductTest or Command-U. This actually runs all test classes. 2. Click the arrow button in the test navigator. 3. Click the diamond button in the gutter.
  • 18. Unit Test Class Structure ● Class Setup/TearDown ○ Executed once when the Unit Test class is initialized, not for every test in the class. Useful to avoid test code duplication at class level. ● Test Setup/TearDown ○ Executed once before and after each executed Unit Test. Useful to avoid test code duplication at test level. ● Test Methods ○ The Unit Test implementation itself. Here is where the test run and validations happens.
  • 20. Test Setup / Tear Down Example @available(iOS 9.0, *) UIViewController.loadViewIfNeeded()
  • 22. Tests Naming ● Use descriptive test names. It will provide quick feedback when test fails. ● Choose names which in few words says: ○ Method under testing ○ Condition under testing ○ Expected result / behavior
  • 23. Tests Naming Example ● Good examples ○ testSyncLullabyList_withLullabyManager_return3LullabyItems() ○ testSyncStationList_withIHeartRadioApi_return6StationItems() ● Bad examples ○ testExample1() ○ testExample2() ○ testExample3()
  • 24. Test Method Organization ● Arrange / Setup ○ Prepare all needed objects/dependencies for test execution. ● Act / Test ○ Test itself it performed, calling one specific method in the system under testing. ● Assert / Verify ○ Method result/behavior verification is performed at the end, using test asserts.
  • 25. Tests Verification Types ● Return Value / State Verification ○ Verify the result returned for the method under test, no matter which interactions with other objects has been made. Useful and preferred verification in most cases. ● Behavior Verification ○ Verify that all tested method interactions among other collaboration objects are the expected one (generally done using mocks). Useful for some cases where interactions are important (for example a cache implementation).
  • 34. XCTAssert Family in XCode 8.3.2 XCTAssert Family Description XCTAssert Asserts that an expression is true. XCTAssertTrue Asserts that an expression is true. XCTAssertFalse Asserts that an expression is false. XCTAssertNil Asserts that an expression is nil. XCTAssertNotNil Asserts that an expression is not nil. XCTAssertEqual Asserts that two expressions have the same value. XCTAssertEqualObjects Asserts that two objects are considered equal.
  • 35. XCTAssert Family in XCode 8.3.2 XCTAssert Family Description XCTAssertNotEqual Asserts that two expressions do not have the same value. XCTAssertNotEqualObjects Asserts that an expression is true. XCTAssertGreaterThan Asserts that the value of one expression is greater than another. XCTAssertGreaterThanOrEqual Asserts that the value of one expression is greater than or equal to another. XCTAssertLessThan Asserts that the value of one expression is less than another. XCTAssertLessThanOrEqual Asserts that the value of one expression is less than or equal to another.
  • 36. XCTAssert Family in XCode 8.3.2 XCTAssert Family Description XCTAssertThrowsError Asserts that an expression throws an error. XCTAssertNoThrow Asserts that an expression does not throw an NSException.
  • 40. Demo Write a Unit Testing class
  • 41. XCTestExpectation ● Define an expectation with a meaningful description. ● Go on with the test setup and exercise phases, calling the asynchronous method and fulfilling the expectation at the end of the callback closure. ● Make the test runner wait for you expectation to be fulfilled, so that the asynchronous operations can be completed and you assertions verified.
  • 45. Asynchronous Tests Problem ● Most apps interact with system or library objects, but you don’t control these objects. ● Tests that interact with these objects can be slow and unrepeatable. ● The singletons made our class hard to test.
  • 47. Dependency Injection ● Extract and Override ○ Create a testing subclass and override the testing method. ● Method Injection ○ Pass a dependency object during a method. ● Property Injection ○ An internal or public property can be modified or replaced. ● Constructor Injection ○ Pass a dependency object during the constructor.
  • 48. Sync Lullaby with LullabyManager
  • 49. Extract and Override Example - Create a Mock Class
  • 50. Extract and Override Example - Verifying Lullaby List
  • 51. Method Injection Example - Create a property that holds a reference
  • 52. Method Injection Example - Dependency object can be replaced
  • 53. Method Injection Example - Use a mock LullabyManager object
  • 54. Property Injection Example - Create a internal or public property
  • 55. Property Injection Example - Use a mock LullabyManager object
  • 56. Constructor Injection Example - Default Constructor
  • 57. Constructor Injection Example - Dependency object can be replaced
  • 58. Constructor Injection Example - Use a mock LullabyManager object
  • 61. Types of Test Doubles ● Stub ○ Fakes a response to method calls of an object. ● Mock ○ Let you check if a method call is performed or if a property is set.
  • 62. Stub
  • 64. Mock
  • 65. Mock Example - Create a Mock Class
  • 68. Reference ● About Testing with Xcode - Apple ● Unit Testing in Xcode 7 with Swift - AppCoda ● iOS Unit Testing and UI Testing Tutorial - RayWenderlich ● Test Doubles: Mocks, Stubs, and More - objc ● MCE 2014: Jon Reid - Test Driven Development for iOS ● Waiting in XCTest
  翻译: