SlideShare a Scribd company logo
iOS Test-Driven 
Development 
Experiencing a new way of working
Iโ€™m going to talk aboutโ€ฆ 
โ€ข Some definitions 
โ€ข Some concepts 
โ€ข Unit Tests 
โ€ข The TDD cycle 
โ€ข Some principles 
! 
โ€ข TDD: Pros & Cons 
โ€ข 3 kind of verifications 
โ€ข External dependencies 
โ€ข iOS Tests (practice part!) 
โ€ข References
Some definitions
Test-driven development (TDD) 
is a software development process that relies on the 
repetition of a very short development cycle. 
! 
- 
! 
Kent Beck, who is credited with having developed or 
โ€˜rediscovered' the technique, stated in 2003 that TDD 
encourages simple designs and inspires confidence. 
โ€“Wikipedia
Test-driven development, or TDD for short, is a simple 
software development practice where unit tests, small 
focused test cases, drive the development forward. 
! 
- 
! 
Test cases are written before any production code. 
! 
Not all the tests are written up front, itโ€™s rather that a small 
test is written, then a small piece of production code is 
written that only allows that test to pass. 
โ€“jayway.com
TDD is an approach to development that uses automated 
software tests to drive the design and development of an 
application iteratively. 
! 
- 
! 
It pushes the developer to focus on one thing at a time, 
and usually one fairly small thing. 
โ€“Sunetos, Inc.
Some concepts
The idea behind test-driven development is that it 
makes you think about what the code you are writing 
needs to do while you are designing it.
As a side effect, we get some safety against breaking 
things in the future. 
OUR work, 
designing & coding
TDD motivates rapid addition of new functionality 
to your code, because you are free to make any 
change you want to your code and can rapidly 
get feedback on whether the change introduced any 
problemsโ€ฆ
TDD motivates rapid addition of new functionality 
to your code, because you are free to make any 
change you want to your code and can rapidly 
get feedback on whether the change introduced any 
problemsโ€ฆ 
โ€ฆ or not!
A common mistake in any discipline of software 
engineering is to only ask about code and test the 
โ€œhappy pathโ€ without specifying or discovering what 
happens in case of error.
A common mistake in any discipline of software 
engineering is to only ask about code and test the 
โ€œhappy pathโ€ without specifying or discovering what 
happens in case of error. 
That IS NOT GOOD.
A common mistake in any discipline of software 
engineering is to only ask about code and test the 
โ€œhappy pathโ€ without specifying or discovering what 
happens in case of error. 
That IS NOT GOOD. 
And, guess what? TDD discourages that.
What is the cost of fixing a defect vs. preventing it? 
! 
! 
! 
Learning and implementing TDD at the beginning is 
hard. 
! 
! 
! 
However, once you make it over the learning curve of 
TDD, your time-to-market will be the same, but with 
many more benefits.
TDD changes design from a process of invention 
to a process of discovery.
where the developer thinks hard about 
what a unit code should do and then 
implements it 
TDD changes design from a process of invention 
to a process of discovery.
where the developer thinks hard about 
what a unit code should do and then 
implements it 
TDD changes design from a process of invention 
to a process of discovery. 
where the developer adds small increments or 
functionality and then extracts structure 
from the working code
โ€œPreventing bugs is not a TDD goal. Itโ€™s more like a 
side effect.โ€
Unit Tests
What is a Unit Test? 
A unit test is a piece of code (usually a method) that 
invokes another piece of code and checks the 
correctness of some assumptions afterwards. 
A unit test must test a single unit of code, which is 
usually a method or function within a class.
What is a Unit Test? 
If assumptions turn out to be wrong, the test has failed. 
Otherwise, the test has passed. 
Unit testing will be performed against 
a System Under Test (SUT). 
Tests SUT
Properties of a Unit Test 
Any unit test should have the following properties: 
โ– It should be automated and repeatable. 
โ– It should be easy to implement. 
โ– Once itโ€™s written, it should remain for future use. 
โ– Anyone should be able to run it. 
โ– It should run quickly.
UNIT 
TESTS INTEGRATION 
TESTS 
โ– Verify 
that 
a 
relatively 
small 
piece 
of 
code 
is 
doing 
what 
is 
intended 
to 
do 
! 
โ– Narrow 
in 
scope 
! 
โ– Easy 
to 
write 
and 
execute 
! 
โ– Intended 
for 
the 
use 
of 
the 
programmer 
! 
โ– Testers 
and 
users 
downstream 
should 
benefit 
from 
seeing 
less 
bugs 
! 
โ– Done 
in 
total 
isolation 
โ– Demonstrate 
that 
different 
pieces 
of 
the 
system 
work 
together 
! 
โ– Can 
cover 
whole 
applications 
! 
โ– Require 
more 
effort 
to 
put 
together 
! 
โ– Intended 
for 
the 
use 
of 
non-ยญโ€programmers 
! 
โ– Their 
output 
is 
the 
integrated 
system 
ready 
for 
System 
Testing 
! 
โ– Done 
altogether 
(taken from my previous talk about KIF Integration Tests)
Unit tests are usually composed by 3 parts: 
1. Preparation 
// given 
these initial conditionsโ€ฆ 
// when 
I execute this codeโ€ฆ 
// then 
I expect this to happenโ€ฆ 
2. Execution 
3. Verification
Unit tests are usually composed by 3 parts: 
// given 
Counter *counter = [[Counter alloc] init]; 
[counter setCount:3]; 
// when 
[counter increment]; 
// then 
XCTAssertTrue(counter.count == 4, @โ€œ3 incremented should 
give us 4โ€);
The TDD cycle
TDD cycle
Red-Green-Refactor 
approach
Write a 
failing test
Write a 
failing test 
Write 
the simplest 
code to pass 
the test
Write a 
failing test 
Write 
the simplest 
code to pass 
the test 
Refactor! 
to make code 
clean
Write a 
failing test 
Write 
the simplest 
code to pass 
the test 
Refactor! 
to make code 
clean
To make sure we are adding 
Write a 
failing test 
something new 
Write 
the simplest 
code to pass 
the test 
Refactor! 
to make code 
clean 
Both testing and 
production code 
KISS
1.
Write a 
failing test 
To make sure we are adding something 
new 
- (void)testOneIncrementedShouldYieldTwo 
{ 
// given 
Counter *counter = [[Counter alloc] init]; 
[counter setCount:1]; 
! 
// when 
[counter increment]; 
! 
// then 
XCTAssertEqual(counter.count, 2, nil); 
} 
1. 
Tests SUT
Write a 
failing test 
To make sure we are adding something 
new 
@implementation Counter 
! 
- (void)increment 
{ 
! 
} 
! 
@end 
1. 
Tests SUT
Write a 
failing test 
To make sure we are adding something 
new 
@implementation Counter 
! 
- (void)increment 
{ 
! 
} 
! 
@end 
Leaving this method empty we will 
make the test fail. 
1. 
Tests SUT
Write 
the simplest 
code to pass 
the test 
KISS - Keep It Simple, Stupid 
@implementation Counter 
! 
- (void)increment 
{ 
! 
} 
! 
@end 
2. 
Tests SUT
Write 
the simplest 
code to pass 
the test 
KISS - Keep It Simple, Stupid 
@implementation Counter 
! 
- (void)increment 
{ 
! 
_count = 2; 
} 
! 
@end 
2. 
Tests SUT
Write 
the simplest 
code to pass 
the test 
KISS - Keep It Simple, Stupid 
@implementation Counter 
! 
- (void)increment 
{ 
! 
} 
! 
@end With this simple line of code, 
we are making the test pass. 
_count = 2; 
2. 
Tests SUT
Now, you may complain โ€œThis implementation is really 
stupidโ€ฆโ€ 
And, it is!!! โ€ฆIt has to be! ! 
This is because it satisfies in the cleanest way all the tests 
we have so far.
Now, you may complain โ€œThis implementation is really 
stupidโ€ฆโ€ 
And, it is!!! โ€ฆIt has to be! ! 
This is because it satisfies in the cleanest way all the tests 
we have so far.
WAIT A SECONDโ€ฆ
Wouldnโ€™t it be better if we made the method 
contain something like โ€œ_count ++;โ€ 
instead?! 
! 
So that weโ€™d be making sure itโ€™ll pass some 
future tests weโ€™d have to implement?
iOS Test-Driven Development
Come on, why not??? 
! 
Explain yourself!
โ€œWe want to live in the present. Not in the future.โ€ 
! 
Because the present will guide us in ways we might 
not expect. 
And that IS GOOD!
4 questions that will help you 
create the simplest thing that could possibly work
4 questions that will help you 
create the simplest thing that could possibly work 
The situation: 
โ€ข You had a failing test. 
โ€ข You went to production code and made the test 
pass in the simplest way you thought possible. 
โ€ข Butโ€ฆ Was it really the simplest way?
create the simplest thing that could possibly work 
The answer: 
4 questions that will help you 
โ€ข We need to define what we consider being simple. 
โ€ข For that, weโ€™ll look at the code weโ€™ve written and 
ask ourselves the followingโ€ฆ
4 questions that will help you 
create the simplest thing that could possibly work 
Can I implement the same solution in a way that isโ€ฆ
4 questions that will help you 
create the simplest thing that could possibly work 
Can I implement the same solution in a way that isโ€ฆ 
โ€ข โ€ฆmore hardcodedโ€ฆ
4 questions that will help you 
create the simplest thing that could possibly work 
Can I implement the same solution in a way that isโ€ฆ 
โ€ข โ€ฆmore hardcodedโ€ฆ 
โ€ข โ€ฆcloser to the beginning of the method I wrote it inโ€ฆ
4 questions that will help you 
create the simplest thing that could possibly work 
Can I implement the same solution in a way that isโ€ฆ 
โ€ข โ€ฆmore hardcodedโ€ฆ 
โ€ข โ€ฆcloser to the beginning of the method I wrote it inโ€ฆ 
โ€ข โ€ฆless indented (in as โ€œlessโ€ scopes as possible, 
like ifโ€™s, loops, try-catch, etc)โ€ฆ
4 questions that will help you 
create the simplest thing that could possibly work 
Can I implement the same solution in a way that isโ€ฆ 
โ€ข โ€ฆmore hardcodedโ€ฆ 
โ€ข โ€ฆcloser to the beginning of the method I wrote it inโ€ฆ 
โ€ข โ€ฆless indented (in as โ€œlessโ€ scopes as possible, 
like ifโ€™s, loops, try-catch, etc)โ€ฆ 
โ€ข โ€ฆshorter (literally less characters to write) yet still 
readableโ€ฆ
4 questions that will help you 
create the simplest thing that could possibly work 
Can I implement the same solution in a way that isโ€ฆ 
โ€ข โ€ฆmore hardcodedโ€ฆ 
โ€ข โ€ฆcloser to the beginning of the method I wrote it inโ€ฆ 
โ€ข โ€ฆless indented (in as โ€œlessโ€ scopes as possible, 
like ifโ€™s, loops, try-catch, etc)โ€ฆ 
โ€ข โ€ฆshorter (literally less characters to write) yet still 
readableโ€ฆ 
โ€ฆAND STILL MAKE ALL THE TESTS PASS?
4 questions that will help you 
create the simplest thing that could possibly work 
Example 
@implementation Counter 
! 
- (void)increment 
{ 
! 
} 
! 
@end 
Tests 
test 1 incr = 2
4 questions that will help you 
create the simplest thing that could possibly work 
Example 
@implementation Counter 
! 
- (void)increment 
{ 
! 
} 
! 
@end 
Tests 
test 1 incr = 2 
_count = 2;
4 questions that will help you 
create the simplest thing that could possibly work 
Example 
@implementation Counter 
! 
- (void)increment 
{ 
! 
} 
! 
@end 
Tests 
test 1 incr = 2 
test 2 incr = 3 
_count = 2;
test 1 incr = 2 
test 2 incr = 3 
4 questions that will help you 
create the simplest thing that could possibly work 
Example Tests 
@implementation Counter 
! 
- (void)increment 
{ 
! 
_count ++; 
} 
! 
@end
test 1 incr = 2 
test 2 incr = 3 
4 questions that will help you 
create the simplest thing that could possibly work 
Example Tests 
_count ++; 
if (_count == 1) 
{ 
_count = 2; 
} 
else if (_count == 2) 
{ 
_count = 3; 
} 
@implementation Counter 
! 
- (void)increment 
{ 
! 
} 
! 
@end
test 1 incr = 2 
test 2 incr = 3 
4 questions that will help you 
create the simplest thing that could possibly work 
Example Tests 
@implementation Counter 
! 
- (void)increment 
{ 
! 
_count ++; 
} 
! 
@end 
if (_count == 1) 
{ 
_count = 2; 
} 
else if (_count == 2) 
{ 
_count = 3; 
Simpler than }
3.
Refactor! 
to make code 
clean 
Both testing and production code. 
3.
Refactor! 
to make code 
clean 
Both testing and production code. 
We have nothing to refactor so far because our code 
remains cleanโ€ฆ 
3.
Refactor! 
to make code 
clean 
Both testing and production code. 
We have nothing to refactor so far because our code 
remains cleanโ€ฆ 
So, we can skip this step. 
3.
Donโ€™t refactor when you have failing tests.
Always start refactoring from a clean state.
Make sure all your tests keep passing after refactoring.
Some principles
The pillars of good tests 
Every test you write must be: 
1. Trustworthy 
2. Maintainable 
3. Readable
Tips to have trustworthy tests 
โ˜… Decide when to remove or change tests. 
โ˜… Avoid test logic. 
โ˜… Test only one thing. 
โ˜… Make tests easy to run. 
โ˜… Assure code coverage.
Ya Ainโ€™t Gonna Need It
Ya Ainโ€™t Gonna Need It 
The "YAGNI" principle
The "YAGNI" principle 
If you write tests that describe what's needed of your app 
code, and you only write code that passes those tests, 
you will never write any code you donโ€™t need. 
This encourages production code to be simple, and 
avoids wasting time writing code that wonโ€™t have any 
effect later. 
A test-driven app should have no unused code, and no 
(or very little) untested code.
The "YAGNI" principle 
Remember: If you find yourself thinking during the 
refactoring stage that there are some changes you could 
make to have the code support more conditions, stop. 
! 
Why arenโ€™t those conditions tested for in the test cases? 
Because those conditions donโ€™t arise in the app. 
! 
So, donโ€™t waste time adding the support, becauseโ€ฆ 
! 
!
The "YAGNI" principle 
Remember: If you find yourself thinking during the 
refactoring stage that there are some changes you could 
make to have the code support more conditions, stop. 
! 
Why arenโ€™t those conditions tested for in the test cases? 
Because those conditions donโ€™t arise in the app. 
! 
So, donโ€™t waste time adding the support, becauseโ€ฆ 
! 
! 
YA AINโ€™T 
GONNA 
NEED IT
The "YAGNI" principle 
Remember: If you find yourself thinking during the 
refactoring stage that there are some changes you could 
make to have the code support more conditions, stop. 
! 
Why arenโ€™t those conditions tested for in the test cases? 
Because those conditions donโ€™t arise in the app. 
! 
So, donโ€™t waste time adding the support, becauseโ€ฆ 
! 
! 
YA AINโ€™T 
GONNA 
NEED IT
Should we test private methods?
Should we test 
private methodsโ€ฆ? 
To answer that, let's observe the following fact: 
! 
You have already tested your private methods.
By following the TDD's red-green-refactor approach, you 
designed your objectsโ€™ public APIs to do the work those 
objects need to do. 
! 
With that work specified by the tests, you are free to organize 
the internal plumbing of your classes as you see fit.
Your private methods have already been tested because all 
you are doing is refactoring behavior that you already have 
tests for.
You should never end up in a situation where a private 
method is untested or incompletely tested, because you 
create them only when you see an opportunity to clean up 
the implementation of public methods.
This ensures that the private methods exist only to support 
the classesโ€™ public behavior, and that they must be invoked 
during testing because they are definitely being called 
from public methods.
TDD: Pros & Cons
TDD benefits 
โœ“ A 2005 study found that using TDD meant writing more 
tests and, in turn, developers who wrote more tests 
tended to be more productive. 
โœ“ Programmers using pure TDD reported they only rarely 
felt the need to invoke a debugger. 
โœ“ TDD offers more than just simple validation of 
correctness, but can also drive the design of a program. 
โœ“ TDD gives the development team, and subsequent users, 
a greater level of confidence in the code.
TDD benefits 
โœ“ Despite the fact that more code is required with TDD 
because of the testing code, the total code 
implementation time could be shorter. 
โœ“ TDD can lead to more modularized, flexible and 
extensible code. 
โœ“ TDD suggests a better modularization, easier reuse and 
testing of the developed software products. 
โœ“ TDD raises the overall code quality in the projects, which 
saves time due to a better maintainability and an easier 
bug-fixing process.
Some TDD goals 
เน Increase the amount of test code coverage. 
เน Increase the amount of test code coverage relative to the 
amount of code churn. 
เน Reduce the amount of bug reopening. 
เน Reduce the average bug-fixing time (the time from "bug 
opened" to "bug closed").
TDD shortcomings 
TDD does not perform sufficient testing in situations where 
full functional tests are required to determine success or 
failure due to extensive use of unit tests. 
If the developer misinterprets the requirements 
specification for the module being developed, both the 
tests and the code will be wrong, as giving a false sense 
of correctness. 
A high number of passing unit tests may bring a false 
sense of security, result in in fewer additional software 
testing activities, such as integration testing and 
compliance testing.
TDD shortcomings 
Overtesting can consume time both to write the excessive 
tests, and later, to rewrite the tests when requirements 
change. 
Hard learning curve. Very difficult at the beginning due to 
drastic changes which are involved in the process.
Real case of a Pilot Project 
Team progress and output measured with and without tests 
Thatโ€™s why itโ€™s important to emphasize that, although unit testing can increase the 
amount of time it takes to implement a feature, the time balances out over the productโ€™s 
release cycle because of increased quality and maintainability. 
source: The Art Of Unit Testing - Roy Osherove
Is TDD worth it? 
(taken from Jon Reidโ€™s blog โ€œQuality Codingโ€)
3 kind of verifications
System 
Under Test
SUT
message 
SUT
message 
return value 
SUT
- (id)calculateSomethingFromArg:(id)arg; 
message 
return value 
SUT
- (id)calculateSomethingFromArg:(id)arg; 
message 
return value 
SUT 
Return Value Verification
- (void)doSomething; 
SUT
- (void)doSomething; 
SUT 
initial state
- (void)doSomething; 
SUT 
initial state 
message
- (void)doSomething; 
SUT 
initial state 
message 
state accessor 
result state
- (void)doSomething; 
SUT 
State Verification 
initial state 
message 
state accessor 
result state
State-based testing (also called state verification) determines whether the exercised 
method worked correctly by examining the SUT after the method is exercised. 
- (void)doSomething; 
SUT 
State Verification 
initial state 
message 
state accessor 
result state
SUT 
Dependent 
object
message Dependent 
SUT 
object
message Dependent 
SUT 
object
message Fake 
SUT 
object
Interaction Verification 
message Fake 
SUT 
object 
(a.k.a. Behavior Verification)
Interaction Verification 
message Fake 
SUT 
object 
(a.k.a. Behavior Verification) 
Interaction testing is testing how an object sends input to or receives input 
from other objectsโ€”how that object interacts with other objects.
3 kind of verifications
Working with external 
dependencies
What is an external dependency? 
Real 
SUT Object 
Class Collaborator 
? 
request something 
expect a value back 
HOW? 
We shouldnโ€™t care 
External 
dependency 
An external dependency is an object in your system 
that your code under test interacts with, and over 
which you have no control.
External dependencies 
โ€ข Filesystems 
โ€ข Managers 
โ€ข Handlers 
โ€ข Connections 
โ€ข Formatters 
โ€ข Etc 
How are we going to deal with an 
external dependency 
when we need to test our SUT? 
We need to make sure our external 
dependency works as we expect, since 
we are NOT testing it, but our SUT. 
FAKE OBJECTS
Fake Objects 
A fake object is just an object that mimics the behavior of a 
real object in controlled ways. 
! 
We define how we want the fake object to respond to certain 
stimulations, such as method calls. 
Real 
SUT Object 
Class Collaborator 
? 
request something 
expect a value back 
HOW? 
We shouldnโ€™t care 
External 
dependency
Fake Objects 
A fake object is just an object that mimics the behavior of a 
real object in controlled ways. 
! 
We define how we want the fake object to respond to certain 
stimulations, such as method calls. 
request something 
Fake 
Object SUT 
expect a value back 
Class Collaborator 
We tell it what to 
respond 
In such a fast, 
controllable and 
kind of hardcoded 
way.
Fake Objects 
request something 
Fake 
Object SUT 
expect a value back 
Class Collaborator 
We tell it what to 
respondโ€ฆ 
Test 
Class โ€ฆ and then we assert the result. 
(AGAINST WHOMโ€ฆ?)
Fake Objects 
communication 
SUT STUB 
Test 
Class 
assert 
against 
SUT
Fake Objects 
communication 
SUT MOCK 
Test 
Class 
assert 
against 
fake object
Fake Objects 
If we assert againstโ€ฆ 
the system under test the object is just a stub. 
S F 
T 
the fake object the object is a mock. 
S F 
T
Fake Objects 
If we assert againstโ€ฆ 
the system under test the object is just a stub. 
S F 
T 
the fake object the object is a mock. 
S F 
T 
We can have as many stubs as we need 
F 
F 
F
Fake Objects 
If we assert againstโ€ฆ 
the system under test the object is just a stub. 
the fake object the object is a mock. 
S F 
T 
We can have at most one mock per test 
S F 
T 
We can have as many stubs as we need 
F 
F 
F 
F
Fake Objects 
If we assert againstโ€ฆ 
the system under test the object is just a stub. 
the fake object the object is a mock. 
S F 
We can have at most one mock per test 
S F 
T 
We can have as many stubs as we need 
F 
F 
F 
F 
T 
We should never assert against two different things within the same test!
Fake Objects 
However, we could end up having a configuration like 
this oneโ€ฆ 
SUT Fake 
Test 
Fake 
Fake 
Fake
Fake Objects 
However, we could end up having a configuration like 
this oneโ€ฆ 
SUT Mock 
Test 
Stub 
Stub 
Stub 
Where we have multiple stubs, but only one mock.
Fake Objects 
However, we could end up having a configuration like 
this oneโ€ฆ 
SUT Mock 
Test 
Stub 
Stub 
Stub 
Where we have multiple stubs, but only one mock.
Fake Objects 
How do we build fake objects? 
Manually 
By using a framework 
โ€ข OCMock 
โ€ข OCMockito 
โ€ข Etc
How do we make external dependencies interact 
with our SUT? 
Dependency Injection 
Cโ€™monโ€ฆ 
It ainโ€™t gonna hurt! 
THE DEPENDOCTOR
โœค Dependency Injection 
โœค Design for Testing 
Both are important topics on TDD that wonโ€™t be explained in 
this talk because they are too much extensive that theyโ€™d 
require another complete talk to be minimally understood.
Time for iOS tests!
Objective-C Unit Testing Frameworks 
โ– OCUnit! 
โ– XCTest (Xcode 5 onwards)
Objective-C Unit Testing Frameworks 
โ– OCUnit! 
โ– XCTest (Xcode 5 onwards) 
Integrates with OS X Server 
CI using Bots
Objective-C Unit Testing Frameworks 
โ– OCUnit! 
โ– XCTest (Xcode 5 onwards) 
OCHamcrest 
OCMockito 
Integrates with OS X Server 
CI using Bots
UI tests 
@property (strong, nonatomic) 
IBOutlet UIButton *button;
UI tests 
@property (strong, nonatomic) 
IBOutlet UIButton *button; 
- (IBAction)buttonPressed:(id)sender;
UI tests 
@property (strong, nonatomic) 
IBOutlet UIButton *button; 
- (IBAction)buttonPressed:(id)sender; 
Test 1: Is outlet hooked up?
UI tests 
@property (strong, nonatomic) 
IBOutlet UIButton *button; 
- (IBAction)buttonPressed:(id)sender; 
Test 1: Is outlet hooked up? 
Test 2: Is outlet connected to action?
UI tests 
@property (strong, nonatomic) 
IBOutlet UIButton *button; 
- (IBAction)buttonPressed:(id)sender; 
Test 1: Is outlet hooked up? 
Test 2: Is outlet connected to action? 
Test 3: Invoke action directly.
References
Test-Driven iOS 
Development 
by Graham Lee 
Itโ€™s like the holy bibleโ€ฆ 
โ€ฆfor iOS TDD developers 
2012 Objective-C
The Art Of Unit Testing 
with Examples in .NET 
by Roy Osherove 
2009 .NET
xUnit Test Patterns 
Refactoring Test Code 
by Gerard Meszaros 
2007 Java
Working Effectively With 
Legacy Code 
by Michael Feathers 
2005 Java, C++, C
Jon Reidโ€™s blog 
http://qualitycoding.org/ 
2011 - Actuality
couldnโ€™t be missingโ€ฆ 
Wikipedia 
http://en.wikipedia.org/wiki/Test-driven_development 
http://en.wikipedia.org/wiki/Mock_object
Other websites and articles 
http://www.jayway.com/2010/01/15/test-driven-development-in-xcode/ 
! 
http://www.sunetos.com/items/2011/10/24/tdd-ios-part-1/ 
! 
https://www.youtube.com/watch?v=S5MvykD3yiE 
(iOS Unit Testing Like A Boss by Matt Darnal) 
! 
http://www.mockobjects.com/ 
! 
http://osherove.com/blog/2010/1/6/tdd-4-questions-that-will-help-you- 
create-the-simplest-thing.html 
! 
http://martinfowler.com/articles/mocksArentStubs.html
Thatโ€™s all, folks! 
(for nowโ€ฆ)
I hope you start liking 
TDD :) 
Pablo Villar - April 2014 @ InakaLabs

More Related Content

What's hot (20)

PPTX
Test-Driven Development (TDD)
Brian Rasmussen
ย 
PPT
Test Driven Development
Sachithra Gayan
ย 
PDF
A Not-So-Serious Introduction to Test Driven Development (TDD)
CodeOps Technologies LLP
ย 
PPT
Test Driven Development
guestc8093a6
ย 
PDF
Test Driven Development
Mireia Sangalo
ย 
PPTX
TDD - Agile
harinderpisces
ย 
PPTX
Test Driven Development (TDD) Preso 360|Flex 2010
guest5639fa9
ย 
PPT
Scrum and Test-driven development
toteb5
ย 
PPTX
TDD - Test Driven Development
Tung Nguyen Thanh
ย 
PDF
Test Driven Development
Dhaval Dalal
ย 
PPTX
Software Quality via Unit Testing
Shaun Abram
ย 
PPTX
Unit Testing in Action - C#, NUnit, and Moq
XPDays
ย 
PDF
Introduction to TDD (Test Driven development) - Ahmed Shreef
Ahmed Shreef
ย 
PDF
TDD Flow: The Mantra in Action
Dionatan default
ย 
DOCX
Test driven development and unit testing with examples in C++
Hong Le Van
ย 
PDF
Getting started with Test Driven Development
Ferdous Mahmud Shaon
ย 
PDF
Unit testing legacy code
Lars Thorup
ย 
KEY
Unit Testing Your Application
Paladin Web Services
ย 
PPTX
2016 10-04: tdd++: tdd made easier
Christian Hujer
ย 
PPTX
An Introduction to Unit Testing
Joe Tremblay
ย 
Test-Driven Development (TDD)
Brian Rasmussen
ย 
Test Driven Development
Sachithra Gayan
ย 
A Not-So-Serious Introduction to Test Driven Development (TDD)
CodeOps Technologies LLP
ย 
Test Driven Development
guestc8093a6
ย 
Test Driven Development
Mireia Sangalo
ย 
TDD - Agile
harinderpisces
ย 
Test Driven Development (TDD) Preso 360|Flex 2010
guest5639fa9
ย 
Scrum and Test-driven development
toteb5
ย 
TDD - Test Driven Development
Tung Nguyen Thanh
ย 
Test Driven Development
Dhaval Dalal
ย 
Software Quality via Unit Testing
Shaun Abram
ย 
Unit Testing in Action - C#, NUnit, and Moq
XPDays
ย 
Introduction to TDD (Test Driven development) - Ahmed Shreef
Ahmed Shreef
ย 
TDD Flow: The Mantra in Action
Dionatan default
ย 
Test driven development and unit testing with examples in C++
Hong Le Van
ย 
Getting started with Test Driven Development
Ferdous Mahmud Shaon
ย 
Unit testing legacy code
Lars Thorup
ย 
Unit Testing Your Application
Paladin Web Services
ย 
2016 10-04: tdd++: tdd made easier
Christian Hujer
ย 
An Introduction to Unit Testing
Joe Tremblay
ย 

Viewers also liked (20)

PDF
iOS Unit Testing Like a Boss
Salesforce Developers
ย 
PDF
Mocking In Swift
dasdom
ย 
KEY
iOS Unit Testing
sgleadow
ย 
PDF
Automated UI testing for iOS apps using KIF framework and Swift
Jurgis Kirsakmens
ย 
PDF
How To Build iOS Apps Without interface Builder
dasdom
ย 
PDF
Tdd
dasdom
ย 
PDF
iOS UI Testing in Xcode
Jz Chang
ย 
PDF
Writing a REST Interconnection Library in Swift
Pablo Villar
ย 
PPTX
UNIT TESTING PPT
suhasreddy1
ย 
PPT
Unit Testing in iOS
Long Weekend LLC
ย 
PDF
Tech Talk #5 : KIF-iOS Integration Testing Framework - Nguyแป…n Hiแป‡p
Nexus FrontierTech
ย 
PDF
Kotlin, Spek and tests
intive
ย 
PPTX
iOS and Android apps automation
Sridhar Ramakrishnan
ย 
PDF
iOS UIAutomation
Jz Chang
ย 
PDF
Automated Xcode 7 UI Testing
Jouni Miettunen
ย 
PDF
Page Object in XCUITest
Jz Chang
ย 
PPTX
iOS Automation: XCUITest + Gherkin
Kenneth Poon
ย 
PPTX
E4
Alarmclock24
ย 
PDF
Desigualdade social
Beatriz Pfaltzgraff
ย 
PPTX
NYCC 2014 (Bionicle)
rusbionicle
ย 
iOS Unit Testing Like a Boss
Salesforce Developers
ย 
Mocking In Swift
dasdom
ย 
iOS Unit Testing
sgleadow
ย 
Automated UI testing for iOS apps using KIF framework and Swift
Jurgis Kirsakmens
ย 
How To Build iOS Apps Without interface Builder
dasdom
ย 
Tdd
dasdom
ย 
iOS UI Testing in Xcode
Jz Chang
ย 
Writing a REST Interconnection Library in Swift
Pablo Villar
ย 
UNIT TESTING PPT
suhasreddy1
ย 
Unit Testing in iOS
Long Weekend LLC
ย 
Tech Talk #5 : KIF-iOS Integration Testing Framework - Nguyแป…n Hiแป‡p
Nexus FrontierTech
ย 
Kotlin, Spek and tests
intive
ย 
iOS and Android apps automation
Sridhar Ramakrishnan
ย 
iOS UIAutomation
Jz Chang
ย 
Automated Xcode 7 UI Testing
Jouni Miettunen
ย 
Page Object in XCUITest
Jz Chang
ย 
iOS Automation: XCUITest + Gherkin
Kenneth Poon
ย 
Desigualdade social
Beatriz Pfaltzgraff
ย 
NYCC 2014 (Bionicle)
rusbionicle
ย 
Ad

Similar to iOS Test-Driven Development (20)

PPTX
Getting started with Test Driven Development - Ferdous Mahmud Shaon
Cefalo
ย 
PPTX
A Brief Introduction to Test-Driven Development
Shawn Jones
ย 
PPTX
Test-Driven Development In Action
Jon Kruger
ย 
PPTX
Test Driven Development on Android (Kotlin Kenya)
Danny Preussler
ย 
PPTX
Unit testing
PiXeL16
ย 
PDF
Test Drive Development
satya sudheer
ย 
PPTX
Intro to TDD
Jason Nocks
ย 
PDF
Adopting tdd in the workplace
Donny Wals
ย 
PDF
Adopting tdd in the workplace
Donny Wals
ย 
PPTX
{10.0} Test Driven Development.pptx
AmalEldhose2
ย 
PDF
TDD and Simple Design Workshop - Session 1 - March 2019
Paulo Clavijo
ย 
PPTX
Understanding Why Testing is Importaint
Sana Nasar
ย 
PPTX
Tdd
nitinkansal2003
ย 
PPTX
assertYourself - Breaking the Theories and Assumptions of Unit Testing in Flex
michael.labriola
ย 
PDF
Test-Driven Development
Amir Assad
ย 
PPTX
TDD Best Practices
Attila Bertรณk
ย 
PPTX
Test Driven Development
Md. Enamul Haque Chowdhury
ย 
PPTX
An Introduction To Software Development - Test Driven Development, Part 1
Blue Elephant Consulting
ย 
PPT
Agile latvia evening_unit_testing_in_practice
denis Udod
ย 
PDF
Tdd is not about testing
Gianluca Padovani
ย 
Getting started with Test Driven Development - Ferdous Mahmud Shaon
Cefalo
ย 
A Brief Introduction to Test-Driven Development
Shawn Jones
ย 
Test-Driven Development In Action
Jon Kruger
ย 
Test Driven Development on Android (Kotlin Kenya)
Danny Preussler
ย 
Unit testing
PiXeL16
ย 
Test Drive Development
satya sudheer
ย 
Intro to TDD
Jason Nocks
ย 
Adopting tdd in the workplace
Donny Wals
ย 
Adopting tdd in the workplace
Donny Wals
ย 
{10.0} Test Driven Development.pptx
AmalEldhose2
ย 
TDD and Simple Design Workshop - Session 1 - March 2019
Paulo Clavijo
ย 
Understanding Why Testing is Importaint
Sana Nasar
ย 
assertYourself - Breaking the Theories and Assumptions of Unit Testing in Flex
michael.labriola
ย 
Test-Driven Development
Amir Assad
ย 
TDD Best Practices
Attila Bertรณk
ย 
Test Driven Development
Md. Enamul Haque Chowdhury
ย 
An Introduction To Software Development - Test Driven Development, Part 1
Blue Elephant Consulting
ย 
Agile latvia evening_unit_testing_in_practice
denis Udod
ย 
Tdd is not about testing
Gianluca Padovani
ย 
Ad

Recently uploaded (20)

PDF
CodeCleaner: Mitigating Data Contamination for LLM Benchmarking
arabelatso
ย 
PDF
Best Practice for LLM Serving in the Cloud
Alluxio, Inc.
ย 
PPTX
IObit Uninstaller Pro 14.3.1.8 Crack Free Download 2025
sdfger qwerty
ย 
PDF
capitulando la keynote de GrafanaCON 2025 - Madrid
Imma Valls Bernaus
ย 
PDF
Alur Perkembangan Software dan Jaringan Komputer
ssuser754303
ย 
PPTX
Agentforce โ€“ TDX 2025 Hackathon Achievement
GetOnCRM Solutions
ย 
PDF
Which Hiring Management Tools Offer the Best ROI?
HireME
ย 
PDF
Azure AI Foundry: The AI app and agent factory
Maxim Salnikov
ย 
PDF
Why Edge Computing Matters in Mobile Application Tech.pdf
IMG Global Infotech
ย 
PDF
AI Software Development Process, Strategies and Challenges
Net-Craft.com
ย 
DOCX
Zoho Creator Solution for EI by Elsner Technologies.docx
Elsner Technologies Pvt. Ltd.
ย 
PPTX
Iobit Driver Booster Pro 12 Crack Free Download
chaudhryakashoo065
ย 
PDF
Automated Test Case Repair Using Language Models
Lionel Briand
ย 
PDF
Humans vs AI Call Agents - Qcall.ai's Special Report
Udit Goenka
ย 
PDF
Writing Maintainable Playwright Tests with Ease
Shubham Joshi
ย 
DOCX
Best AI-Powered Wearable Tech for Remote Health Monitoring in 2025
SEOLIFT - SEO Company London
ย 
PPTX
Foundations of Marketo Engage - Programs, Campaigns & Beyond - June 2025
BradBedford3
ย 
PDF
The Next-Gen HMIS Software AI, Blockchain & Cloud for Housing.pdf
Prudence B2B
ย 
PDF
The Rise of Sustainable Mobile App Solutions by New York Development Firms
ostechnologies16
ย 
PPTX
CV-Project_2024 version 01222222222.pptx
MohammadSiddiqui70
ย 
CodeCleaner: Mitigating Data Contamination for LLM Benchmarking
arabelatso
ย 
Best Practice for LLM Serving in the Cloud
Alluxio, Inc.
ย 
IObit Uninstaller Pro 14.3.1.8 Crack Free Download 2025
sdfger qwerty
ย 
capitulando la keynote de GrafanaCON 2025 - Madrid
Imma Valls Bernaus
ย 
Alur Perkembangan Software dan Jaringan Komputer
ssuser754303
ย 
Agentforce โ€“ TDX 2025 Hackathon Achievement
GetOnCRM Solutions
ย 
Which Hiring Management Tools Offer the Best ROI?
HireME
ย 
Azure AI Foundry: The AI app and agent factory
Maxim Salnikov
ย 
Why Edge Computing Matters in Mobile Application Tech.pdf
IMG Global Infotech
ย 
AI Software Development Process, Strategies and Challenges
Net-Craft.com
ย 
Zoho Creator Solution for EI by Elsner Technologies.docx
Elsner Technologies Pvt. Ltd.
ย 
Iobit Driver Booster Pro 12 Crack Free Download
chaudhryakashoo065
ย 
Automated Test Case Repair Using Language Models
Lionel Briand
ย 
Humans vs AI Call Agents - Qcall.ai's Special Report
Udit Goenka
ย 
Writing Maintainable Playwright Tests with Ease
Shubham Joshi
ย 
Best AI-Powered Wearable Tech for Remote Health Monitoring in 2025
SEOLIFT - SEO Company London
ย 
Foundations of Marketo Engage - Programs, Campaigns & Beyond - June 2025
BradBedford3
ย 
The Next-Gen HMIS Software AI, Blockchain & Cloud for Housing.pdf
Prudence B2B
ย 
The Rise of Sustainable Mobile App Solutions by New York Development Firms
ostechnologies16
ย 
CV-Project_2024 version 01222222222.pptx
MohammadSiddiqui70
ย 

iOS Test-Driven Development

  • 1. iOS Test-Driven Development Experiencing a new way of working
  • 2. Iโ€™m going to talk aboutโ€ฆ โ€ข Some definitions โ€ข Some concepts โ€ข Unit Tests โ€ข The TDD cycle โ€ข Some principles ! โ€ข TDD: Pros & Cons โ€ข 3 kind of verifications โ€ข External dependencies โ€ข iOS Tests (practice part!) โ€ข References
  • 4. Test-driven development (TDD) is a software development process that relies on the repetition of a very short development cycle. ! - ! Kent Beck, who is credited with having developed or โ€˜rediscovered' the technique, stated in 2003 that TDD encourages simple designs and inspires confidence. โ€“Wikipedia
  • 5. Test-driven development, or TDD for short, is a simple software development practice where unit tests, small focused test cases, drive the development forward. ! - ! Test cases are written before any production code. ! Not all the tests are written up front, itโ€™s rather that a small test is written, then a small piece of production code is written that only allows that test to pass. โ€“jayway.com
  • 6. TDD is an approach to development that uses automated software tests to drive the design and development of an application iteratively. ! - ! It pushes the developer to focus on one thing at a time, and usually one fairly small thing. โ€“Sunetos, Inc.
  • 8. The idea behind test-driven development is that it makes you think about what the code you are writing needs to do while you are designing it.
  • 9. As a side effect, we get some safety against breaking things in the future. OUR work, designing & coding
  • 10. TDD motivates rapid addition of new functionality to your code, because you are free to make any change you want to your code and can rapidly get feedback on whether the change introduced any problemsโ€ฆ
  • 11. TDD motivates rapid addition of new functionality to your code, because you are free to make any change you want to your code and can rapidly get feedback on whether the change introduced any problemsโ€ฆ โ€ฆ or not!
  • 12. A common mistake in any discipline of software engineering is to only ask about code and test the โ€œhappy pathโ€ without specifying or discovering what happens in case of error.
  • 13. A common mistake in any discipline of software engineering is to only ask about code and test the โ€œhappy pathโ€ without specifying or discovering what happens in case of error. That IS NOT GOOD.
  • 14. A common mistake in any discipline of software engineering is to only ask about code and test the โ€œhappy pathโ€ without specifying or discovering what happens in case of error. That IS NOT GOOD. And, guess what? TDD discourages that.
  • 15. What is the cost of fixing a defect vs. preventing it? ! ! ! Learning and implementing TDD at the beginning is hard. ! ! ! However, once you make it over the learning curve of TDD, your time-to-market will be the same, but with many more benefits.
  • 16. TDD changes design from a process of invention to a process of discovery.
  • 17. where the developer thinks hard about what a unit code should do and then implements it TDD changes design from a process of invention to a process of discovery.
  • 18. where the developer thinks hard about what a unit code should do and then implements it TDD changes design from a process of invention to a process of discovery. where the developer adds small increments or functionality and then extracts structure from the working code
  • 19. โ€œPreventing bugs is not a TDD goal. Itโ€™s more like a side effect.โ€
  • 21. What is a Unit Test? A unit test is a piece of code (usually a method) that invokes another piece of code and checks the correctness of some assumptions afterwards. A unit test must test a single unit of code, which is usually a method or function within a class.
  • 22. What is a Unit Test? If assumptions turn out to be wrong, the test has failed. Otherwise, the test has passed. Unit testing will be performed against a System Under Test (SUT). Tests SUT
  • 23. Properties of a Unit Test Any unit test should have the following properties: โ– It should be automated and repeatable. โ– It should be easy to implement. โ– Once itโ€™s written, it should remain for future use. โ– Anyone should be able to run it. โ– It should run quickly.
  • 24. UNIT TESTS INTEGRATION TESTS โ– Verify that a relatively small piece of code is doing what is intended to do ! โ– Narrow in scope ! โ– Easy to write and execute ! โ– Intended for the use of the programmer ! โ– Testers and users downstream should benefit from seeing less bugs ! โ– Done in total isolation โ– Demonstrate that different pieces of the system work together ! โ– Can cover whole applications ! โ– Require more effort to put together ! โ– Intended for the use of non-ยญโ€programmers ! โ– Their output is the integrated system ready for System Testing ! โ– Done altogether (taken from my previous talk about KIF Integration Tests)
  • 25. Unit tests are usually composed by 3 parts: 1. Preparation // given these initial conditionsโ€ฆ // when I execute this codeโ€ฆ // then I expect this to happenโ€ฆ 2. Execution 3. Verification
  • 26. Unit tests are usually composed by 3 parts: // given Counter *counter = [[Counter alloc] init]; [counter setCount:3]; // when [counter increment]; // then XCTAssertTrue(counter.count == 4, @โ€œ3 incremented should give us 4โ€);
  • 31. Write a failing test Write the simplest code to pass the test
  • 32. Write a failing test Write the simplest code to pass the test Refactor! to make code clean
  • 33. Write a failing test Write the simplest code to pass the test Refactor! to make code clean
  • 34. To make sure we are adding Write a failing test something new Write the simplest code to pass the test Refactor! to make code clean Both testing and production code KISS
  • 35. 1.
  • 36. Write a failing test To make sure we are adding something new - (void)testOneIncrementedShouldYieldTwo { // given Counter *counter = [[Counter alloc] init]; [counter setCount:1]; ! // when [counter increment]; ! // then XCTAssertEqual(counter.count, 2, nil); } 1. Tests SUT
  • 37. Write a failing test To make sure we are adding something new @implementation Counter ! - (void)increment { ! } ! @end 1. Tests SUT
  • 38. Write a failing test To make sure we are adding something new @implementation Counter ! - (void)increment { ! } ! @end Leaving this method empty we will make the test fail. 1. Tests SUT
  • 39. Write the simplest code to pass the test KISS - Keep It Simple, Stupid @implementation Counter ! - (void)increment { ! } ! @end 2. Tests SUT
  • 40. Write the simplest code to pass the test KISS - Keep It Simple, Stupid @implementation Counter ! - (void)increment { ! _count = 2; } ! @end 2. Tests SUT
  • 41. Write the simplest code to pass the test KISS - Keep It Simple, Stupid @implementation Counter ! - (void)increment { ! } ! @end With this simple line of code, we are making the test pass. _count = 2; 2. Tests SUT
  • 42. Now, you may complain โ€œThis implementation is really stupidโ€ฆโ€ And, it is!!! โ€ฆIt has to be! ! This is because it satisfies in the cleanest way all the tests we have so far.
  • 43. Now, you may complain โ€œThis implementation is really stupidโ€ฆโ€ And, it is!!! โ€ฆIt has to be! ! This is because it satisfies in the cleanest way all the tests we have so far.
  • 45. Wouldnโ€™t it be better if we made the method contain something like โ€œ_count ++;โ€ instead?! ! So that weโ€™d be making sure itโ€™ll pass some future tests weโ€™d have to implement?
  • 47. Come on, why not??? ! Explain yourself!
  • 48. โ€œWe want to live in the present. Not in the future.โ€ ! Because the present will guide us in ways we might not expect. And that IS GOOD!
  • 49. 4 questions that will help you create the simplest thing that could possibly work
  • 50. 4 questions that will help you create the simplest thing that could possibly work The situation: โ€ข You had a failing test. โ€ข You went to production code and made the test pass in the simplest way you thought possible. โ€ข Butโ€ฆ Was it really the simplest way?
  • 51. create the simplest thing that could possibly work The answer: 4 questions that will help you โ€ข We need to define what we consider being simple. โ€ข For that, weโ€™ll look at the code weโ€™ve written and ask ourselves the followingโ€ฆ
  • 52. 4 questions that will help you create the simplest thing that could possibly work Can I implement the same solution in a way that isโ€ฆ
  • 53. 4 questions that will help you create the simplest thing that could possibly work Can I implement the same solution in a way that isโ€ฆ โ€ข โ€ฆmore hardcodedโ€ฆ
  • 54. 4 questions that will help you create the simplest thing that could possibly work Can I implement the same solution in a way that isโ€ฆ โ€ข โ€ฆmore hardcodedโ€ฆ โ€ข โ€ฆcloser to the beginning of the method I wrote it inโ€ฆ
  • 55. 4 questions that will help you create the simplest thing that could possibly work Can I implement the same solution in a way that isโ€ฆ โ€ข โ€ฆmore hardcodedโ€ฆ โ€ข โ€ฆcloser to the beginning of the method I wrote it inโ€ฆ โ€ข โ€ฆless indented (in as โ€œlessโ€ scopes as possible, like ifโ€™s, loops, try-catch, etc)โ€ฆ
  • 56. 4 questions that will help you create the simplest thing that could possibly work Can I implement the same solution in a way that isโ€ฆ โ€ข โ€ฆmore hardcodedโ€ฆ โ€ข โ€ฆcloser to the beginning of the method I wrote it inโ€ฆ โ€ข โ€ฆless indented (in as โ€œlessโ€ scopes as possible, like ifโ€™s, loops, try-catch, etc)โ€ฆ โ€ข โ€ฆshorter (literally less characters to write) yet still readableโ€ฆ
  • 57. 4 questions that will help you create the simplest thing that could possibly work Can I implement the same solution in a way that isโ€ฆ โ€ข โ€ฆmore hardcodedโ€ฆ โ€ข โ€ฆcloser to the beginning of the method I wrote it inโ€ฆ โ€ข โ€ฆless indented (in as โ€œlessโ€ scopes as possible, like ifโ€™s, loops, try-catch, etc)โ€ฆ โ€ข โ€ฆshorter (literally less characters to write) yet still readableโ€ฆ โ€ฆAND STILL MAKE ALL THE TESTS PASS?
  • 58. 4 questions that will help you create the simplest thing that could possibly work Example @implementation Counter ! - (void)increment { ! } ! @end Tests test 1 incr = 2
  • 59. 4 questions that will help you create the simplest thing that could possibly work Example @implementation Counter ! - (void)increment { ! } ! @end Tests test 1 incr = 2 _count = 2;
  • 60. 4 questions that will help you create the simplest thing that could possibly work Example @implementation Counter ! - (void)increment { ! } ! @end Tests test 1 incr = 2 test 2 incr = 3 _count = 2;
  • 61. test 1 incr = 2 test 2 incr = 3 4 questions that will help you create the simplest thing that could possibly work Example Tests @implementation Counter ! - (void)increment { ! _count ++; } ! @end
  • 62. test 1 incr = 2 test 2 incr = 3 4 questions that will help you create the simplest thing that could possibly work Example Tests _count ++; if (_count == 1) { _count = 2; } else if (_count == 2) { _count = 3; } @implementation Counter ! - (void)increment { ! } ! @end
  • 63. test 1 incr = 2 test 2 incr = 3 4 questions that will help you create the simplest thing that could possibly work Example Tests @implementation Counter ! - (void)increment { ! _count ++; } ! @end if (_count == 1) { _count = 2; } else if (_count == 2) { _count = 3; Simpler than }
  • 64. 3.
  • 65. Refactor! to make code clean Both testing and production code. 3.
  • 66. Refactor! to make code clean Both testing and production code. We have nothing to refactor so far because our code remains cleanโ€ฆ 3.
  • 67. Refactor! to make code clean Both testing and production code. We have nothing to refactor so far because our code remains cleanโ€ฆ So, we can skip this step. 3.
  • 68. Donโ€™t refactor when you have failing tests.
  • 69. Always start refactoring from a clean state.
  • 70. Make sure all your tests keep passing after refactoring.
  • 72. The pillars of good tests Every test you write must be: 1. Trustworthy 2. Maintainable 3. Readable
  • 73. Tips to have trustworthy tests โ˜… Decide when to remove or change tests. โ˜… Avoid test logic. โ˜… Test only one thing. โ˜… Make tests easy to run. โ˜… Assure code coverage.
  • 75. Ya Ainโ€™t Gonna Need It The "YAGNI" principle
  • 76. The "YAGNI" principle If you write tests that describe what's needed of your app code, and you only write code that passes those tests, you will never write any code you donโ€™t need. This encourages production code to be simple, and avoids wasting time writing code that wonโ€™t have any effect later. A test-driven app should have no unused code, and no (or very little) untested code.
  • 77. The "YAGNI" principle Remember: If you find yourself thinking during the refactoring stage that there are some changes you could make to have the code support more conditions, stop. ! Why arenโ€™t those conditions tested for in the test cases? Because those conditions donโ€™t arise in the app. ! So, donโ€™t waste time adding the support, becauseโ€ฆ ! !
  • 78. The "YAGNI" principle Remember: If you find yourself thinking during the refactoring stage that there are some changes you could make to have the code support more conditions, stop. ! Why arenโ€™t those conditions tested for in the test cases? Because those conditions donโ€™t arise in the app. ! So, donโ€™t waste time adding the support, becauseโ€ฆ ! ! YA AINโ€™T GONNA NEED IT
  • 79. The "YAGNI" principle Remember: If you find yourself thinking during the refactoring stage that there are some changes you could make to have the code support more conditions, stop. ! Why arenโ€™t those conditions tested for in the test cases? Because those conditions donโ€™t arise in the app. ! So, donโ€™t waste time adding the support, becauseโ€ฆ ! ! YA AINโ€™T GONNA NEED IT
  • 80. Should we test private methods?
  • 81. Should we test private methodsโ€ฆ? To answer that, let's observe the following fact: ! You have already tested your private methods.
  • 82. By following the TDD's red-green-refactor approach, you designed your objectsโ€™ public APIs to do the work those objects need to do. ! With that work specified by the tests, you are free to organize the internal plumbing of your classes as you see fit.
  • 83. Your private methods have already been tested because all you are doing is refactoring behavior that you already have tests for.
  • 84. You should never end up in a situation where a private method is untested or incompletely tested, because you create them only when you see an opportunity to clean up the implementation of public methods.
  • 85. This ensures that the private methods exist only to support the classesโ€™ public behavior, and that they must be invoked during testing because they are definitely being called from public methods.
  • 86. TDD: Pros & Cons
  • 87. TDD benefits โœ“ A 2005 study found that using TDD meant writing more tests and, in turn, developers who wrote more tests tended to be more productive. โœ“ Programmers using pure TDD reported they only rarely felt the need to invoke a debugger. โœ“ TDD offers more than just simple validation of correctness, but can also drive the design of a program. โœ“ TDD gives the development team, and subsequent users, a greater level of confidence in the code.
  • 88. TDD benefits โœ“ Despite the fact that more code is required with TDD because of the testing code, the total code implementation time could be shorter. โœ“ TDD can lead to more modularized, flexible and extensible code. โœ“ TDD suggests a better modularization, easier reuse and testing of the developed software products. โœ“ TDD raises the overall code quality in the projects, which saves time due to a better maintainability and an easier bug-fixing process.
  • 89. Some TDD goals เน Increase the amount of test code coverage. เน Increase the amount of test code coverage relative to the amount of code churn. เน Reduce the amount of bug reopening. เน Reduce the average bug-fixing time (the time from "bug opened" to "bug closed").
  • 90. TDD shortcomings TDD does not perform sufficient testing in situations where full functional tests are required to determine success or failure due to extensive use of unit tests. If the developer misinterprets the requirements specification for the module being developed, both the tests and the code will be wrong, as giving a false sense of correctness. A high number of passing unit tests may bring a false sense of security, result in in fewer additional software testing activities, such as integration testing and compliance testing.
  • 91. TDD shortcomings Overtesting can consume time both to write the excessive tests, and later, to rewrite the tests when requirements change. Hard learning curve. Very difficult at the beginning due to drastic changes which are involved in the process.
  • 92. Real case of a Pilot Project Team progress and output measured with and without tests Thatโ€™s why itโ€™s important to emphasize that, although unit testing can increase the amount of time it takes to implement a feature, the time balances out over the productโ€™s release cycle because of increased quality and maintainability. source: The Art Of Unit Testing - Roy Osherove
  • 93. Is TDD worth it? (taken from Jon Reidโ€™s blog โ€œQuality Codingโ€)
  • 94. 3 kind of verifications
  • 96. SUT
  • 100. - (id)calculateSomethingFromArg:(id)arg; message return value SUT Return Value Verification
  • 102. - (void)doSomething; SUT initial state
  • 103. - (void)doSomething; SUT initial state message
  • 104. - (void)doSomething; SUT initial state message state accessor result state
  • 105. - (void)doSomething; SUT State Verification initial state message state accessor result state
  • 106. State-based testing (also called state verification) determines whether the exercised method worked correctly by examining the SUT after the method is exercised. - (void)doSomething; SUT State Verification initial state message state accessor result state
  • 110. message Fake SUT object
  • 111. Interaction Verification message Fake SUT object (a.k.a. Behavior Verification)
  • 112. Interaction Verification message Fake SUT object (a.k.a. Behavior Verification) Interaction testing is testing how an object sends input to or receives input from other objectsโ€”how that object interacts with other objects.
  • 113. 3 kind of verifications
  • 114. Working with external dependencies
  • 115. What is an external dependency? Real SUT Object Class Collaborator ? request something expect a value back HOW? We shouldnโ€™t care External dependency An external dependency is an object in your system that your code under test interacts with, and over which you have no control.
  • 116. External dependencies โ€ข Filesystems โ€ข Managers โ€ข Handlers โ€ข Connections โ€ข Formatters โ€ข Etc How are we going to deal with an external dependency when we need to test our SUT? We need to make sure our external dependency works as we expect, since we are NOT testing it, but our SUT. FAKE OBJECTS
  • 117. Fake Objects A fake object is just an object that mimics the behavior of a real object in controlled ways. ! We define how we want the fake object to respond to certain stimulations, such as method calls. Real SUT Object Class Collaborator ? request something expect a value back HOW? We shouldnโ€™t care External dependency
  • 118. Fake Objects A fake object is just an object that mimics the behavior of a real object in controlled ways. ! We define how we want the fake object to respond to certain stimulations, such as method calls. request something Fake Object SUT expect a value back Class Collaborator We tell it what to respond In such a fast, controllable and kind of hardcoded way.
  • 119. Fake Objects request something Fake Object SUT expect a value back Class Collaborator We tell it what to respondโ€ฆ Test Class โ€ฆ and then we assert the result. (AGAINST WHOMโ€ฆ?)
  • 120. Fake Objects communication SUT STUB Test Class assert against SUT
  • 121. Fake Objects communication SUT MOCK Test Class assert against fake object
  • 122. Fake Objects If we assert againstโ€ฆ the system under test the object is just a stub. S F T the fake object the object is a mock. S F T
  • 123. Fake Objects If we assert againstโ€ฆ the system under test the object is just a stub. S F T the fake object the object is a mock. S F T We can have as many stubs as we need F F F
  • 124. Fake Objects If we assert againstโ€ฆ the system under test the object is just a stub. the fake object the object is a mock. S F T We can have at most one mock per test S F T We can have as many stubs as we need F F F F
  • 125. Fake Objects If we assert againstโ€ฆ the system under test the object is just a stub. the fake object the object is a mock. S F We can have at most one mock per test S F T We can have as many stubs as we need F F F F T We should never assert against two different things within the same test!
  • 126. Fake Objects However, we could end up having a configuration like this oneโ€ฆ SUT Fake Test Fake Fake Fake
  • 127. Fake Objects However, we could end up having a configuration like this oneโ€ฆ SUT Mock Test Stub Stub Stub Where we have multiple stubs, but only one mock.
  • 128. Fake Objects However, we could end up having a configuration like this oneโ€ฆ SUT Mock Test Stub Stub Stub Where we have multiple stubs, but only one mock.
  • 129. Fake Objects How do we build fake objects? Manually By using a framework โ€ข OCMock โ€ข OCMockito โ€ข Etc
  • 130. How do we make external dependencies interact with our SUT? Dependency Injection Cโ€™monโ€ฆ It ainโ€™t gonna hurt! THE DEPENDOCTOR
  • 131. โœค Dependency Injection โœค Design for Testing Both are important topics on TDD that wonโ€™t be explained in this talk because they are too much extensive that theyโ€™d require another complete talk to be minimally understood.
  • 132. Time for iOS tests!
  • 133. Objective-C Unit Testing Frameworks โ– OCUnit! โ– XCTest (Xcode 5 onwards)
  • 134. Objective-C Unit Testing Frameworks โ– OCUnit! โ– XCTest (Xcode 5 onwards) Integrates with OS X Server CI using Bots
  • 135. Objective-C Unit Testing Frameworks โ– OCUnit! โ– XCTest (Xcode 5 onwards) OCHamcrest OCMockito Integrates with OS X Server CI using Bots
  • 136. UI tests @property (strong, nonatomic) IBOutlet UIButton *button;
  • 137. UI tests @property (strong, nonatomic) IBOutlet UIButton *button; - (IBAction)buttonPressed:(id)sender;
  • 138. UI tests @property (strong, nonatomic) IBOutlet UIButton *button; - (IBAction)buttonPressed:(id)sender; Test 1: Is outlet hooked up?
  • 139. UI tests @property (strong, nonatomic) IBOutlet UIButton *button; - (IBAction)buttonPressed:(id)sender; Test 1: Is outlet hooked up? Test 2: Is outlet connected to action?
  • 140. UI tests @property (strong, nonatomic) IBOutlet UIButton *button; - (IBAction)buttonPressed:(id)sender; Test 1: Is outlet hooked up? Test 2: Is outlet connected to action? Test 3: Invoke action directly.
  • 142. Test-Driven iOS Development by Graham Lee Itโ€™s like the holy bibleโ€ฆ โ€ฆfor iOS TDD developers 2012 Objective-C
  • 143. The Art Of Unit Testing with Examples in .NET by Roy Osherove 2009 .NET
  • 144. xUnit Test Patterns Refactoring Test Code by Gerard Meszaros 2007 Java
  • 145. Working Effectively With Legacy Code by Michael Feathers 2005 Java, C++, C
  • 146. Jon Reidโ€™s blog http://qualitycoding.org/ 2011 - Actuality
  • 147. couldnโ€™t be missingโ€ฆ Wikipedia http://en.wikipedia.org/wiki/Test-driven_development http://en.wikipedia.org/wiki/Mock_object
  • 148. Other websites and articles http://www.jayway.com/2010/01/15/test-driven-development-in-xcode/ ! http://www.sunetos.com/items/2011/10/24/tdd-ios-part-1/ ! https://www.youtube.com/watch?v=S5MvykD3yiE (iOS Unit Testing Like A Boss by Matt Darnal) ! http://www.mockobjects.com/ ! http://osherove.com/blog/2010/1/6/tdd-4-questions-that-will-help-you- create-the-simplest-thing.html ! http://martinfowler.com/articles/mocksArentStubs.html
  • 149. Thatโ€™s all, folks! (for nowโ€ฆ)
  • 150. I hope you start liking TDD :) Pablo Villar - April 2014 @ InakaLabs