Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Unit testing in general
Search
Adam
May 23, 2015
Programming
74
2
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Unit testing in general
Adam
May 23, 2015
More Decks by Adam
See All by Adam
Unit testing with PHPUnit
adammbalogh
1
89
REST: Key Concepts
adammbalogh
2
200
Other Decks in Programming
See All in Programming
新卒PdEのリアル
ryu1013
1
490
初心者DevRelとして参加者だった私が、DevRel Talks!#2に登壇するまでにしてきたこと
sokohirai
0
370
GraphRAGのKnowledge Graphを 直接!見る/View-GraphRAG's-KnowledgeGraph-directly!
tyumugi1113
1
290
モジュールの視点からSwiftを読み解く #iosdc
s_shimotori
0
120
PHPプロジェクトの結合バランスを可視化する #php_night
kajitack
0
240
Laravelのアプリケーションをどこにデプロイするか #ツナギメオフライン.9
akase244
0
130
【DroidKaigi 2026】「アクセシビリティを利用するとき、 アクセシビリティもまたこちらを利用している」 〜マルウェアによる攻撃と防衛について〜
halunoyo
0
450
【高い買い物LT会】初任給で話題の国産フィジカルAIを買った話
akagami
PRO
0
150
Omarchy Tokyo やると聞いて UMPC 買ってセットアップしてきた
mtsmfm
0
140
DroidKaigi 2026 「個人開発という実験場: Android エンジニアが手にする4つの自由」
slashnephy
0
230
Foundry Localでエージェント開発
seosoft
0
180
テストを司るデーモンに会いに行く 〜隔離した仮想マシンでテストを通すまで〜
h1d3mun3
1
210
Featured
See All Featured
DevOps and Value Stream Thinking: Enabling flow, efficiency and business value
helenjbeal
1
380
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
49
3.6k
Highjacked: Video Game Concept Design
rkendrick25
PRO
1
450
Making Projects Easy
brettharned
120
6.7k
Claude Code どこまでも/ Claude Code Everywhere
nwiizo
67
58k
How to Grow Your eCommerce with AI & Automation
katarinadahlin
PRO
1
270
Measuring & Analyzing Core Web Vitals
bluesmoon
9
990
Amusing Abliteration
ianozsvald
1
290
The Curse of the Amulet
leimatthew05
2
14k
The SEO identity crisis: Don't let AI make you average
varn
0
560
B2B Lead Gen: Tactics, Traps & Triumph
marketingsoph
0
240
HDC tutorial
michielstock
2
860
Transcript
Unit testing in general
“Is a method of assessing the functionality of a software
program.” Software testing
None
Test Case Input Test Case Output Black box testing App
Test Case Input Test Case Output White box testing
Cost Maintaining Testing Pyramid http://martinfowler.com/bliki/TestPyramid.html # of tests
Unit test Units are tested to determine if they are
fit for use.
In a nutshell public function testArrayPush() { $stack = array();
array_push($stack, 'foo'); $this->assertEquals('foo', $stack[0]); } Precondition Execution Assertion
a single check that always evaluates to true or false
Assertion
Unit? Unit is a class. (in OOP)
Single class Related classes Related methods Unit?
Unit? “really it's a situational thing - the team decides
what makes sense to be a unit” - Martin Fowler
Unit tests Why should I care?
Fundamental to regression testing Required for refactoring Can be a
good documentation Finds problems early Unit tests / Pros
Gives confidence Reduces the cost of change Improves design Unit
tests / Pros
We have to write the tests Maintaining Unit tests /
Cons
Hard-coded dependencies Static method calls Methods with too many responsibilities
Tight coupling Complex methods Unreadable code “Untestable” code
Hard-coded dependencies public function doSomething() { $bar = new Bar;
$bar->doSomethingElse(); }
Static method calls class Foo { public static function doSomething()
{ return static::helper(); } public static function helper() { return 'foo'; } }
None
Hard-coded dependencies public function doSomething() { $bar = new Bar;
$bar->doSomethingElse(); }
Hard-coded dependencies protected function newCallback($className) { switch ($className) { case
'Bar': return 'BarMock'; default: return $className; } } set_new_overload(array($this, 'newCallback')); http://sebastian-bergmann.de/archives/885-Stubbing-Hard-Coded-Dependencies.html
Static method calls class Foo { public static function doSomething()
{ return static::helper(); } public static function helper() { return 'foo'; } }
Static method calls $class = $this->getMockClass( 'Foo', # class to
mock array('helper') # methods to mock ); $class::staticExpects($this->any()) ->method('helper') ->will($this->returnValue('bar')); https://sebastian-bergmann.de/archives/883-Stubbing-and-Mocking-Static-Methods.html
!
“Just Because You Can, Does Not Mean You Should”
None
Use dependency injection Try to avoid static method calls SRP
Readable KISS, DRY, SOLID Testable code
Hard-coded dependencies public function doSomething() { $bar = new Bar;
$bar->doSomethingElse(); }
Dependency Injection public function doSomething(Bar $bar) { $bar->doSomethingElse(); }
Unit tests are first class citizens
Fast Isolated Repeatable Self-verifying Timely Unit tests should be FIRST
File system Database API call from each other Isolated?
Mock your dependencies “…mock objects are simulated objects that mimic
the behavior of real objects in controlled ways”
None
None
What code should be tested? We should test the public
API (of a unit)
Protected/Private methods? Getters and Setters? Trivial code? Exceptions? What code
should be tested?
Selective unit testing http://blog.stevensanderson.com/2009/11/04/selective-unit-testing-costs-and-benefits/
Code coverage
Code coverage http://martinfowler.com/bliki/TestCoverage.html
Code coverage CRAP Change Risk Analysis and Predictions
Code coverage 100% 80% 50% Low Medium High
Automation CI Watchman IDE
Thank you!