SlideShare a Scribd company logo
Beginning iOS Unit
Testing
Mark Struzinski
Monday, April 22, 13
What is Unit Testing?
Monday, April 22, 13
What is Unit Testing?
Test small pieces of your code (units)
in isolation.
Monday, April 22, 13
What is Unit Testing?
Test small pieces of your code (units)
in isolation.
Usually, this means testing methods.
Monday, April 22, 13
Why Unit Testing?
Monday, April 22, 13
Why Unit Testing?
Verify your code does what you
expect.
Monday, April 22, 13
Why Unit Testing?
Verify your code does what you
expect.
Makes refactoring much less painful
Monday, April 22, 13
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
2 Kinds of Unit Test
Bundles
Monday, April 22, 13
2 Kinds of Unit Test
Bundles
Application Test Bundle
Tests fully bootstrapped application
Monday, April 22, 13
2 Kinds of Unit Test
Bundles
Application Test Bundle
Tests fully bootstrapped application
Logic Test Bundle
Tests classes in isolation
Monday, April 22, 13
Setting Up
Monday, April 22, 13
Setting Up
New projects offer an option to create unit tests immediately
Monday, April 22, 13
Setting Up
New projects offer an option to create unit tests immediately
Creates an Application unit test bundle
Monday, April 22, 13
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
Setting Up
Monday, April 22, 13
Setting Up
⌘ + U is automatically wired up to run tests
Monday, April 22, 13
Setting Up
⌘ + U is automatically wired up to run tests
Can also use Product => Test from the menu
Monday, April 22, 13
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
Setting Up
Monday, April 22, 13
Setting Up
A unit test bundle is created
alongside your main target
Monday, April 22, 13
Setting Up
A unit test bundle is created
alongside your main target
A separate Xcode group is
created for your unit tests
Monday, April 22, 13
Setting Up
A 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
Setting Up
A 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
Adding New Tests
Monday, April 22, 13
Adding New Tests
Add new files to unit testing group
Monday, April 22, 13
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
Running Tests
Monday, April 22, 13
Running Tests
Initial test run will fail
Monday, April 22, 13
Running Tests
Initial test run will fail
This is how you know it’s working
Monday, April 22, 13
Unit Testing Conventions
Monday, April 22, 13
Unit Testing Conventions
All unit test methods must begin with the
word “test”
Monday, April 22, 13
Unit Testing Conventions
All unit test methods must begin with the
word “test”
Unit test methods must have a return
type of void
Monday, April 22, 13
Unit Testing Conventions
All 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
Unit Testing Conventions
All 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
Unit Testing Conventions
Monday, April 22, 13
Unit Testing Conventions
Can have as many unit test classes/files as
needed in a project
Monday, April 22, 13
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
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
A Small Example
Monday, April 22, 13
A Small Example
Monday, April 22, 13
A Small Example
Tests use assertion macros to determine pass/fail
Monday, April 22, 13
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
A Small Example
Anatomy of a Unit Test
Monday, April 22, 13
A Small Example
1. Set up object to be tested
Anatomy of a Unit Test
Monday, April 22, 13
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
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
3.Assert expected result
Monday, April 22, 13
A Small Example
Setup/Teardown Methods
Monday, April 22, 13
A Small Example
Run before and after each test
Setup/Teardown Methods
Monday, April 22, 13
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
A Small Example
Run before and after each test
Setup/Teardown Methods
Use to set up objects and move repetitive code out of tests
Good way to ensure state of the object under test
Monday, April 22, 13
Unit Test Failures
Monday, April 22, 13
Unit Test Failures
Failure messages appear in the issue navigator and the console
Monday, April 22, 13
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
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
Questions?
Monday, April 22, 13
Questions?
ThankYou!
mark@markstruzinski.com
@ski081 (Twitter/ADN)
http://markstruzinski.com
Monday, April 22, 13
Reference
https://developer.apple.com/library/ios/#documentation/DeveloperTools/
Conceptual/UnitTesting/00-About_Unit_Testing/about.html
Monday, April 22, 13

More Related Content

ODP
Testing In Java
PDF
Clean Unit Test Patterns
PPT
Automated Unit Testing
PPTX
Java Unit Test and Coverage Introduction
PDF
TestNG Annotations in Selenium | Edureka
PPTX
TestNG Session presented in PB
PDF
Test driven development - JUnit basics and best practices
PDF
Unit testing best practices with JUnit
Testing In Java
Clean Unit Test Patterns
Automated Unit Testing
Java Unit Test and Coverage Introduction
TestNG Annotations in Selenium | Edureka
TestNG Session presented in PB
Test driven development - JUnit basics and best practices
Unit testing best practices with JUnit

What's hot (20)

ODP
Test ng
PPTX
TestNG vs JUnit: cease fire or the end of the war
PDF
Unit testing with JUnit
PPTX
Unit Testing Concepts and Best Practices
PDF
Unit Testing
PPTX
Junit4&testng presentation
PPTX
TestNG with selenium
PDF
Test ng for testers
PDF
Workshop unit test
PPT
Simple Unit Testing With Netbeans 6.1
PPTX
.Net Unit Testing with Visual Studio 2010
PDF
TestNg_Overview_Config
PPTX
TestNG Session presented in Xebia XKE
PPTX
Test NG Framework Complete Walk Through
PPT
PPTX
Test ng tutorial
PPTX
Introduction to JUnit
PPTX
Introduction of TestNG framework and its benefits over Junit framework
PPT
Xp Day 080506 Unit Tests And Mocks
PPSX
Test ng
TestNG vs JUnit: cease fire or the end of the war
Unit testing with JUnit
Unit Testing Concepts and Best Practices
Unit Testing
Junit4&testng presentation
TestNG with selenium
Test ng for testers
Workshop unit test
Simple Unit Testing With Netbeans 6.1
.Net Unit Testing with Visual Studio 2010
TestNg_Overview_Config
TestNG Session presented in Xebia XKE
Test NG Framework Complete Walk Through
Test ng tutorial
Introduction to JUnit
Introduction of TestNG framework and its benefits over Junit framework
Xp Day 080506 Unit Tests And Mocks
Ad

Similar to Beginning iOS unit testing (20)

PDF
Unit testing in xcode 8 with swift
PDF
Unit Tesing in iOS
PPT
Nguyenvandungb seminar
PDF
Unit testing, principles
PDF
Automated Testing on iOS
PPTX
Unit testing
PDF
Unit Testing in Software Development: Why It Matters and How to Do It Right
PDF
Becoming a better programmer - unit testing
PDF
Unit Testing Guide. Helps to understand the basics of unit testing .
PPTX
presentation des google test dans un contexte de tdd
PDF
Unit testing - An introduction
PPS
Unit testing_pps
PPT
unit testing pppttttttttttttttttttttttttttttttttttttttttttttttt
PDF
What Is Unit Testing_ A Complete Guide With Examples.pdf
PDF
Unit testing (Exploring the other side as a tester)
PDF
[Rakuten TechConf2014] [G-4] Beyond Agile Testing to Lean Development
PPTX
An Introduction to Unit Testing
PDF
What Is Unit Testing A Complete Guide With Examples.pdf
PPTX
Skillwise Unit Testing
PDF
How Unit Testing Strengthens Software Reliability
Unit testing in xcode 8 with swift
Unit Tesing in iOS
Nguyenvandungb seminar
Unit testing, principles
Automated Testing on iOS
Unit testing
Unit Testing in Software Development: Why It Matters and How to Do It Right
Becoming a better programmer - unit testing
Unit Testing Guide. Helps to understand the basics of unit testing .
presentation des google test dans un contexte de tdd
Unit testing - An introduction
Unit testing_pps
unit testing pppttttttttttttttttttttttttttttttttttttttttttttttt
What Is Unit Testing_ A Complete Guide With Examples.pdf
Unit testing (Exploring the other side as a tester)
[Rakuten TechConf2014] [G-4] Beyond Agile Testing to Lean Development
An Introduction to Unit Testing
What Is Unit Testing A Complete Guide With Examples.pdf
Skillwise Unit Testing
How Unit Testing Strengthens Software Reliability
Ad

Recently uploaded (20)

PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PPTX
Spectroscopy.pptx food analysis technology
PDF
Encapsulation theory and applications.pdf
PDF
cuic standard and advanced reporting.pdf
PPTX
Cloud computing and distributed systems.
PPTX
Big Data Technologies - Introduction.pptx
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Empathic Computing: Creating Shared Understanding
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Approach and Philosophy of On baking technology
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
KodekX | Application Modernization Development
PDF
NewMind AI Weekly Chronicles - August'25 Week I
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Unlocking AI with Model Context Protocol (MCP)
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Spectroscopy.pptx food analysis technology
Encapsulation theory and applications.pdf
cuic standard and advanced reporting.pdf
Cloud computing and distributed systems.
Big Data Technologies - Introduction.pptx
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Empathic Computing: Creating Shared Understanding
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Spectral efficient network and resource selection model in 5G networks
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Mobile App Security Testing_ A Comprehensive Guide.pdf
Approach and Philosophy of On baking technology
Per capita expenditure prediction using model stacking based on satellite ima...
KodekX | Application Modernization Development
NewMind AI Weekly Chronicles - August'25 Week I
The AUB Centre for AI in Media Proposal.docx
Review of recent advances in non-invasive hemoglobin estimation
Chapter 3 Spatial Domain Image Processing.pdf
Unlocking AI with Model Context Protocol (MCP)

Beginning iOS unit testing