Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
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
in-process GraphQL のすすめ #ginzajs
izumin5210
4
1.4k
関東Kaggler会_NVIDIA_Nemotron_コンペ_振り返り
rick_ds
0
710
ソフトウェアラスタライザ
fadis
1
500
異なる設計思想のフレームワークを経験して得た学び
amekuhideki
1
180
My Marp Sample
sinoue0108
0
110
S3 を使うアプリケーションをローカル完結で動かすことに全力を注いでみた / Running S3 Apps Offline
contour_gara
0
580
進化を続けるGo toolsの現在地 / The Current State of Ever-Evolving Go Tools
hond0413
0
250
変わらないものが、変わるものを決める — 意図駆動開発 × イベントソーシング × イミュータブル | What Doesn't Change Decides What Can — IDD × Event Sourcing × Immutability
tomohisa
0
1.8k
VibeCodingからAgenticWorkflowへ
starfish719
0
780
KotlinConf Extended South Korea 2026 Keynote
l2hyunwoo
0
120
引き算の組織 ― アウトカムとAIに全振りするために辞めたこと ― / Organization by Subtraction
hirokiyamamoto14
PRO
0
280
go-spidermonkeyでAIエージェントのCode Modeを実装する
syumai
1
650
Featured
See All Featured
How Software Deployment tools have changed in the past 20 years
geshan
1
34k
The Straight Up "How To Draw Better" Workshop
denniskardys
239
140k
A Guide to Academic Writing Using Generative AI - A Workshop
ks91
PRO
1
380
jQuery: Nuts, Bolts and Bling
dougneiner
66
8.5k
Amusing Abliteration
ianozsvald
1
260
How To Speak Unicorn (iThemes Webinar)
marktimemedia
1
550
Winning Ecommerce Organic Search in an AI Era - #searchnstuff2025
aleyda
1
2.1k
Stop Working from a Prison Cell
hatefulcrawdad
274
21k
Public Speaking Without Barfing On Your Shoes - THAT 2023
reverentgeek
1
540
Ruling the World: When Life Gets Gamed
codingconduct
0
310
How People are Using Generative and Agentic AI to Supercharge Their Products, Projects, Services and Value Streams Today
helenjbeal
1
280
How to optimise 3,500 product descriptions for ecommerce in one day using ChatGPT
katarinadahlin
PRO
2
3.8k
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!