SlideShare a Scribd company logo
Test first!
Test first!
Test first!
Test	Driven
Development
What	is	TDD
• It's	a	development	technique	based	on	a	short	development
process
• It's	a	way	to	deeply	analyze	the	expected	behavior	of	a
software	
• It's	a	mean	to	improve	the	quality	of	the	source	code
• It's	a	practice	to	enhance	the	documentation	of	a	software
• It's	a	way	to	ensure	a	safer	way	to	modify	the	software
behavior
TDD	and	Unit	Tests
Unit	tests	can	be	written	before	writing	the	code,	after	the	code	has
been	written	or	during	the	development
• Unit	Testing	refers	to	what	you	are	testing,	TDD	to	when	you	are
testing
• Unit	Testing	means,	well,	testing	individual	units	of	behavior
The	D	in	the	Middle
• You	let	the	tests	to	drive	you
• The	tests	tell	you	what	to	do,	what	to	do	next,	what's	done
• The	tests	are	the	skeleton	of	the	API	design
TDD	Workflow
TDD	is	writing	a	test	which	will	fail,	then	writing	the	minimum
amount	of	code	required	to	make	it	run,	then	refactoring	the	code	to
make	it	clean
The	D	at	the	End
• It's	supposed	to	mean	development	but	originally	was	supposed
to	mean	design	
• Test	Driven	Design	is	the	discipline	of	using	Unit	Testing		(or
other	tests)	to	design	the	software
• Working	on	a	better	design	it's	possible	to	improve	the	quality	of
the	software
Small	and	Testable
• Methods	and	functions	should	be	simple	in	order	to	be	testable
• Generally	speaking,	the	code	needed	to	satisfy	a	test	is	usually
a	very	small	chunk	of	code
• As	a	result	the	public	API	are	simple	and	easy	to	maintain
Test	Double
The	general	name	for	objects	used	to	replace	real	component	servers
for	testing	purposes
• Dummy,	an	object	without	any	specific	implementation
• Stub,	an	object	with	interface	or	minimal	implementation
• Spy,	a	step	up	from	stub	because	it	records	which	object	used
itself
• Fake,	an	object	with	a	more	complex	implementation
• Mock,	an	object	dynamically	created	by	a	library	(e.g.
http://sinonjs.org)
Common	Sentences
• “It	costs	too	much	time,	we’d	better	fix	these	bugs	first”
• “Our	system	is	too	big	for	unit	tests”
• “We	just	ought	to	be	more	careful	not	to	introduce	bugs,	that
will	help	us	a	lot”
• “Our	system	is	not	suited	for	unit	tests”
• “We	have	a	QA-team	that	will	detect	errors”
TDD	and	QA
• Test	driven	development	is	not	a	quality	assurance	method
• Most	people	associate	"testing"	with	"quality	assurance"
• With	TDD	you	write	code	using	tests	as	a	tool,	the	QA	team
searches	bugs	using	the	same	tests	as	a	tool
Best	Practices
Assertions
• Assertions	are	statements	that	perform	an	actual	check	on	the
software’s	output
• A	single	function	called	assert	is	enough	to	express	any	check
• TDD	libraries	have	many	assert	functions	for	specific	needs
(assertFalse,	assertEqual,s,	etc.)
• A	single	assert	should	be	used	in	each	test
• An	assert	failure	should	add	a	clear	message	to	the	test	logs
Integration	Testing
• A	mid-level	testing	activity	that	verifies	a	certain	set	of	modules
work	correctly	together	
• Integration	tests	are	like	unit	tests	without	using	test	doubles
for	some	dependencies
Source	&	Tests
Separation
Pick	the	First	Test
• Carefully	read	the	requirements	and	eventually	split	them	if
needed	
• Choose	a	requirement	and	write	a	list	of	tests	for	this
requirement
• Select	a	test	that	is	atomic	and	well	isolated		
• Try	to	prioritize	them	(e.g.	test	first	user	creation	than	login!)
Write	the	Assertion
First
Determine	how	the	specification	(i.e.	the	test)	is	going	to	be	validated
First	Time	Failure
• How	to	test	something	that	doesn't	exist?
• By	adding	methods	or	functions	to	test		developers	start	to
design	first	the	code
Unit	Testing
(Unit	Testing	refers	to	what	you	are	testing,	TDD	to	when	you	are
testing)
Glossary
• Unit,	the	smallest	piece	of	code	software	that	can	be	tested	in
isolation
• Assertion,	it's	a	predicate	that	states	the	programmer’s
intended	state	of	a	system
• Integration	test,	a	test	that	go	outside	the	current	process
interacting	with	something	else
• Interaction	test,	a	test	on	the	way	that	objects	work	together
• Fake,	any	stand-in	object	that's	used	instead	of	the	real	thing
• Stub,	a	stand-in	object	that	provides	a	dependency	required	by
the	code	under	test
• Mock,	a	simulated	object	that	mimics	a	specific	behavior
Individual	Units	
• Source	code	is	tested	to	understand	if	it	can	be	used	by	other
part	of	the	software
• Test	cases	are	separated	and	independent	by	each	other
• Method	stubs,	mock	objects	and	fakes	data	can	be	used	to	run
the	tests	in	isolation
Benefits	
• Helps	finding	problems	early
• Facilitates	changes	(i.e.	regressions	testing)
• Simplifies	integration
• Improves	the	documentation
Limitations
• Unit	testing	will	not	catch	every	error	in	the	program
• Integration	errors	will	be	not	catch	because	the	test	itself	by
definition	test	only	the	unit
• Code	coverage	doesn't	prove	the	software	is	working
• It's	time	consuming
Common
Challenges
• Setting	up	realistic	and	useful	tests
• Testing	asynchronous	code
• Testing	private	methods
• Writing	flexible	tests		
• Effective	names
Usefulness	
• Define	relevant	initial	conditions	
• Tests	have	to	exercising	the	code	in	a	realistic	context
• Avoid	unnecessary	preconditions	
• Don’t	unit-test	configuration	settings
Flexibility
• Tests	should	never	over	specify	the	behavior	of	the	target	code
• Tests	should	never	test	more	than	once	the	same	code	(i.e.
don't	repeat	yourself!)
• Always	use	only	data	relevant	to	a	particular	test	in	the	test	
• Consider	always	how	to	pass	input	data	(e.g.	Builder	pattern)
Async	Code
• The	main	issue	is	to	keep	the	test	blocked	until	the	async	code
has	been	executed
• You	can	repeatedly	polling	the	target	system	for	a	state	change
(i.e.	Sampling)
• You	can	use	an	event-based	assertion	that	waits	for	an	event	by
blocking	on	a	monitor	until	it	gets	notified	or	times	out	(i.e.
Listening)
Private	Methods
Events	handler,	also	if	private,	are	your	public	API
• Put	the	tests	in	the	class	you	want	to	test
• Put	the	tests	in	another	class/source	file	&	expose	the	private
methods	you	want	to	test	as	public	methods
Naming
• The	name	should	clearly	indicate	the	tested	feature
• The	test	name	should	indicates	what	the	unit	does	not	what	the
unit	is
• The	test	name	should	say	something	about	the	motivation	for
the	scenario
• Use	the	TestDox	convention	where	each	test	name	reads	like	a
sentence
Acceptance	Test
Driven
Development
Key	Features
• ATDD	is	a	practice	in	which	the	whole	team	collaboratively
discusses	requirements	acceptability	
• ATDD,	like	TDD,	involves	creating	tests	before	code
• The	acceptance	tests	then	become	like	executable	requirements
Expected	Benefits
• ATDD	results	in	applications	designed	to	be	easier	to	test
• ATDD	favors	the	creation	of	interfaces	specific	to	functional
testing
• ATDD	clarify	to	all	the	team	members	which	are	the	customer
expectations
ATDD	and	Unit
Testing
• ATDD	doesn't	imply	to	remove	unit	testing	from	the
development	cycle
• ATDD	implies	the	usage	of	tools	to	automate	the	functional
testing	execution	(e.g.	FitNess,		Cucumber,	Rspec)
Testing	Pyramid
Software
Requirements
As	a	<role>	I	want	to	<goal>	so	that	<motivation>
• User	stories	are	the	central	axis	around	which	a	software	project
rotates
• User	stories	express	requirements	in	terms	of	The	Role,	The
Goal,	and	The	Motivation
• User	stories	aren’t	exhaustive	requirements	specifications
Acceptance	Criteria
• A	set	of	conditions	the	requirement	must	fit	in	to	be	considered
done
• It's	the	result	of	a	conversation	with	the	customer	(or	product
owner)
• It's	a	way	to	describe	the	tests	a	requirement	should	pass
Writing	Acceptance
Criteria
• An	acceptance	criteria	should	contain	an	Actor,	a	Verb	and	an
Observable	Result
• An	acceptance	criteria	describe	a	pre-condtion	when	an	actor
should	perform	an	action	and	obtain	the	expected	result	(e.g.
mandatory	fields)
• An	acceptance	criteria	should	be	also	identify	performances
expectations
Acceptance	Tests
Concrete	examples,	concrete	behavior,	no	ambiguity
• A	formal	description	of	the	behavior	of	a	software	product
expressed	as	an	example	
• Are	the	combination	of	acceptance	criteria	and	real	examples
(i.e.	data	and	scenarios)
ATDD	Cycle
Behavior	Driven
Development
Key	Features	
• Help	to	write	tests	that	reflect	the	behavior	desired	by	the
stakeholders
• Uses	a	Ubiquitous	Language	that	can	be	understood	by	both	the
developer	and	the	customer
Test	Different
• Unit	testing	force	developers	to	think	in	terms	of	tests	and
assertions	while	behavior	driven	development	bring	them	to
consider	tests	as	part	of	specification
• When	you	realize	that	it's	all	about	specifying	behavior	and	not
writing	tests,	your	point	of	view	shifts
It’s	Still	TDD?
Nothing	in	Behavior-Driven	Development	changes	Test	Driven
Development’s	mechanics	(i.e.	write	the	test	->	pass	it	->	refactor)
BDD	and	Code
Design
• Unit	testing	is	often	specifically	about	the	design	of	code	units
and	modules	such	as	classes
• BDD	is	also	concerned	with	unit	design,	but	addresses	a	broader
range	of	design	concerns
BDD	and
Acceptance	Criteria
• Acceptance	criteria	are	transformed	into	lower-level
specifications	written	in	code,	and	executed	using	a	testing	or
specification	framework
Sign	of	Use
• Significant	portion	of	"functional	documentation"	is	in	the	form
of	User	Stories	augmented	with	executable	scenarios	or	examples
• Instead	of	referring	to	"tests",	team	members	will	prefer	the
terms	"scenario"	and	"specification"
• Tests	are	a	mean	to	define	the	specifications	of	the	behavior	of
a	Module
Common	Issues
• BDD	requires	familiarity	with	a	greater	range	of	concepts	than
Unit	Testing	does
• BDD	don't	requires	at	the	beginning	particular	tools	or
programming	languages,	and	is	primarily	a	conceptual	approach
• BDD	requires	a	different	development	cycle
BDD	Cycle
Context	and
Specifications
• Sets	of	related	observations	(tests)	will	be	gathered	into
contexts
• Contexts	aren’t	simply	arbitrary	groupings,	they	represent
cohesive	and	consistent	circumstances	that	software	modules	can
be	found	in
• Specifications	are	the	tests	written	using	a	BDD	testing
framework
Effective	Scenarios
• Capture	a	summary	scenario
• Identify	actors	and	their	goals
• Sentences	should	be	in	sequential	order
• Each	sentence	should	have	an	actor	performing	an	action
Unit	Testing,	ATDD
and	BDD
BDD	is	an	evolution	of	test-driven	development	that	enhance	and
enforce	the	practices	deriving	from	unit	testing	and	acceptance
testing
TDD	===	BDD
Tests	Anatomy
Unit	Tests
test("prettydate	basics",	function()	{
					var	now	=	"2008/01/28	22:25:00";
		equal(prettyDate(now,	"2008/01/28	22:24:30"),	"just	now");
		equal(prettyDate(now,	"2008/01/28	22:23:30"),	"1	minute	ago");
		equal(prettyDate(now,	"2008/01/28	21:23:30"),	"1	hour	ago");
		equal(prettyDate(now,	"2008/01/27	22:23:30"),	"Yesterday");
		equal(prettyDate(now,	"2008/01/26	22:23:30"),	"2	days	ago");
		equal(prettyDate(now,	"2007/01/26	22:23:30"),	undefined);
	});
ATDD	Tests
BDD	Tests
function	addValues(	a,	b	)	{	
				return	a	+	b;	
}
describe("addValues(a,	b)	function",	function()	{	
				it("should	equal	3",	function(){	
								expect(	addValues(1,	2)	).toBe(	3	);	
				});
				it("should	equal	3.75",	function(){	
								expect(	addValues(1.75,	2)	).toBe(	3.75	);	
					});	
				it("should	NOT	equal	'3'	as	a	String",	function(){
Jasmine
(JavaScript	testing	suite)
Jasmine	Provides
• A	natural	BDD	syntax	for	organizing	the	test	logic	
• Asynchronous	testing	support
• Mocks	object
• Spies
• Custom	matchers
Sample	Test
describe("Hello	world",	function()	{
				it("says	hello",	function(){	
								expect(helloWorld()).toEqual("Hello	world!");			
								});	
								
});
Suite
• describe	("Hello	world"...	)	is	what	is	called	a	suite
• The	name	of	the	suite	(“Hello	world”	in	this	case)	defines	a
component	of	your	application
Spec
• Inside	of	that	suite	(technically,	inside	of	an	anonymous
function),	is	the	it()	block	
• This	is	called	a	specification,	or	a	spec	for	short
Matcher
• In	this	case,	you’re	testing	if	helloWorld()	does	indeed	return
"Hello	world!"	
• This	check	is	called	a	matcher...	Jasmine	supports	several
matchers!
Specs.html
It	contains	four	script	tags:
• 	The	first	section	includes	the	Jasmine	test	runner	files
• In	the	next	two	sections,	you	reference	your	JavaScript	code	to
be	tested	as	well	as	the	tests
• The	fourth	runs	the	tests
Async	Calls
it("	does	an	asynchronous	call",	function()	{	
				exampleAsyncCall(	function(	response)	{	
								expect(	response).toContain("	something	expected");	
								done();	
				});	
});
Running	Tests	with
Grunt
• A	JavaScript	task	runner
• A	project	scaffolding	tool
• Use	it	to	automate	common	task	execution	execution
• Configurable	plugins	such	as	require.js,	handlebar,	jasmine,	etc.
Using	Grunt
• First	of	all	install	grunt	command	line	tool	npm	install	-g	grunt-
cli
• Most	grunt-init	templates	will	automatically	create	a
package.json	file			
• Install	grunt	an	the	plugins	npm	install	grunt	--save-dev
• Install	the	jasmine	plugin	npm	install	grunt-contrib-jasmine	--
save-dev
The	Grunt.js	File
module.exports	=	function(grunt)	{
		'use	strict';
		grunt.initConfig({
				jasmine	:	{
						src	:	'src/**/*.js',
						options	:	{
								specs	:	'spec/**/*.js',
								template	:	require('grunt-template-jasmine-istanbul'),
								templateOptions:	{
										coverage:	'reports/coverage.json',
										report:	'reports/coverage'
								}
Questions	&
Answers
Links
• http://msdn.microsoft.com/en-us/magazine/cc163358.aspx
• http://guide.agilealliance.org/guide/atdd.html
• http://guide.agilealliance.org/guide/acceptance.html
• http://www.codemag.com/article/0805061
• http://www.slideshare.net/nashjain/acceptance-test-driven-
development-350264
• http://testobsessed.com/wp-
content/uploads/2011/04/atddexample.pdf
• http://programmers.stackexchange.com/questions/135218/what-
is-the-difference-between-bdd-and-tdd
• http://www.slideshare.net/emwendelin/test-your-javascript
My	Latest	Book
What's	Next
Test first!

More Related Content

PDF
TDD and PhoneGap
PPT
Test Driven Development
PPTX
Test Driven Development - a Practitioner’s Perspective
PDF
Agile engineering practices – a short overview
PDF
Introduction to TDD
PPTX
Test Driven Development
PPTX
optimizing code in compilers using parallel genetic algorithm
PPTX
(Agile) engineering best practices - What every project manager should know
TDD and PhoneGap
Test Driven Development
Test Driven Development - a Practitioner’s Perspective
Agile engineering practices – a short overview
Introduction to TDD
Test Driven Development
optimizing code in compilers using parallel genetic algorithm
(Agile) engineering best practices - What every project manager should know

What's hot (16)

PPT
Software Engineering Fundamentals Svetlin Nakov
PPTX
DDT Testing Library for Android
PPTX
Introduction to test for non testers
PPTX
Acceptance Test Driven Development
PDF
Audrys Kažukauskas - Introduction into Extreme Programming
PPTX
An Evaluation of Pair Programming Practice
PPTX
Capability Building for Cyber Defense: Software Walk through and Screening
PPTX
Testing strategy for agile projects updated
PPT
Agile Software Development with XP
PPTX
Agile Practices - eXtreme Programming
PPTX
Agile software development and extreme Programming
PPTX
Xp exterme-programming-model
PPTX
Test driven development
ODP
Extreme Programming
PPTX
Career Paths for Software Professionals
Software Engineering Fundamentals Svetlin Nakov
DDT Testing Library for Android
Introduction to test for non testers
Acceptance Test Driven Development
Audrys Kažukauskas - Introduction into Extreme Programming
An Evaluation of Pair Programming Practice
Capability Building for Cyber Defense: Software Walk through and Screening
Testing strategy for agile projects updated
Agile Software Development with XP
Agile Practices - eXtreme Programming
Agile software development and extreme Programming
Xp exterme-programming-model
Test driven development
Extreme Programming
Career Paths for Software Professionals
Ad

Viewers also liked (6)

PPTX
Don’t leave home without IT
PPT
Broadcasting Technologies Branch Overview Nov 2007
PDF
The Little Shop of TDD Horrors
PDF
Harmonik
PPT
Broadcasting Technologies Overview April 2008
PDF
Ecma6 in 30 minutes
Don’t leave home without IT
Broadcasting Technologies Branch Overview Nov 2007
The Little Shop of TDD Horrors
Harmonik
Broadcasting Technologies Overview April 2008
Ecma6 in 30 minutes
Ad

Similar to Test first! (20)

PPTX
Test Driven Development
PPTX
Test-Driven-Development.pptx
PPT
Test Driven Development - Overview and Adoption
PPT
Presentation_TDD
PPTX
Test driven development v1.0
PDF
Test driven development : software process
PPTX
Test-Driven Development.pptx
PPTX
Test-Driven Development In Action
PPTX
Understanding TDD - theory, practice, techniques and tips.
PPTX
TDD - Agile
PPTX
Tdd 1-introduction
PPT
Tech talks #1- Unit testing and TDD
PPTX
Test driven development(tdd)
PDF
PPTX
TDD - Seriously, try it! - Opensouthcode
PPTX
TDD - Seriously, try it! (updated '22)
PPTX
TDD - Seriously, try it! - Bucarest Tech Week
PPTX
TDD - Seriously, try it - Codemotion (May '24)
PPTX
TDD- Test Driven Development
PPT
Test_Driven_Development_v5.ppt
Test Driven Development
Test-Driven-Development.pptx
Test Driven Development - Overview and Adoption
Presentation_TDD
Test driven development v1.0
Test driven development : software process
Test-Driven Development.pptx
Test-Driven Development In Action
Understanding TDD - theory, practice, techniques and tips.
TDD - Agile
Tdd 1-introduction
Tech talks #1- Unit testing and TDD
Test driven development(tdd)
TDD - Seriously, try it! - Opensouthcode
TDD - Seriously, try it! (updated '22)
TDD - Seriously, try it! - Bucarest Tech Week
TDD - Seriously, try it - Codemotion (May '24)
TDD- Test Driven Development
Test_Driven_Development_v5.ppt

More from Giorgio Natili (17)

PDF
Driving Assistant Solutions with Android
PDF
Isomorphic Reactive Programming
PDF
Service worker API
PDF
I beacon mobile_tea
PDF
Android, getting started
PDF
Clear the UIViewController Mess
PDF
Big data and mobile
PDF
The short path to ecma 6
PDF
Jasmine 2.0
PDF
Mobile raspberry pi
PDF
WebRTC communication and wearable devices
PDF
Multithreading development with workers
PDF
Undoable architectures
PDF
WebRTC and Mobile Integration
PDF
Develop, test and debug cross platforms apps with PhoneGap
PDF
Test first
PDF
Mobile benchmarking-and-profiling
Driving Assistant Solutions with Android
Isomorphic Reactive Programming
Service worker API
I beacon mobile_tea
Android, getting started
Clear the UIViewController Mess
Big data and mobile
The short path to ecma 6
Jasmine 2.0
Mobile raspberry pi
WebRTC communication and wearable devices
Multithreading development with workers
Undoable architectures
WebRTC and Mobile Integration
Develop, test and debug cross platforms apps with PhoneGap
Test first
Mobile benchmarking-and-profiling

Recently uploaded (20)

PPTX
CHAPTER 2 - PM Management and IT Context
PPTX
Introduction to Artificial Intelligence
PDF
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
PDF
Nekopoi APK 2025 free lastest update
PDF
PTS Company Brochure 2025 (1).pdf.......
PPTX
history of c programming in notes for students .pptx
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PPTX
Transform Your Business with a Software ERP System
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PDF
AI in Product Development-omnex systems
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PDF
How Creative Agencies Leverage Project Management Software.pdf
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PPTX
Reimagine Home Health with the Power of Agentic AI​
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
CHAPTER 2 - PM Management and IT Context
Introduction to Artificial Intelligence
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
Nekopoi APK 2025 free lastest update
PTS Company Brochure 2025 (1).pdf.......
history of c programming in notes for students .pptx
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
Transform Your Business with a Software ERP System
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
VVF-Customer-Presentation2025-Ver1.9.pptx
AI in Product Development-omnex systems
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
How Creative Agencies Leverage Project Management Software.pdf
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
2025 Textile ERP Trends: SAP, Odoo & Oracle
Reimagine Home Health with the Power of Agentic AI​
Navsoft: AI-Powered Business Solutions & Custom Software Development
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises

Test first!