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
2
73
Unit testing in general
Adam
May 23, 2015
Tweet
Share
More Decks by Adam
See All by Adam
Unit testing with PHPUnit
adammbalogh
1
88
REST: Key Concepts
adammbalogh
2
200
Other Decks in Programming
See All in Programming
PHP でエミュレータを自作して Ubuntu を動かそう
m3m0r7
PRO
2
150
条件判定に名前、つけてますか? #phperkaigi #c
77web
2
840
S3ストレージクラスの「見える」「ある」「使える」は全部違う ─ 体験から見た、仕様の深淵を覗く
ya_ma23
0
1.2k
今からFlash開発できるわけないじゃん、ムリムリ! (※ムリじゃなかった!?)
arkw
0
160
エンジニアの「手元の自動化」を加速するn8n 2026.02.27
symy2co
0
180
守る「だけ」の優しいEMを抜けて、 事業とチームを両方見る視点を身につけた話
maroon8021
3
1.5k
メッセージングを利用して時間的結合を分離しよう #phperkaigi
kajitack
3
440
Goの型安全性で実現する複数プロダクトの権限管理
ishikawa_pro
2
1.4k
Xdebug と IDE による デバッグ実行の仕組みを見る / Exploring-How-Debugging-Works-with-Xdebug-and-an-IDE
shin1x1
0
210
Understanding Apache Lucene - More than just full-text search
spinscale
0
140
Windows on Ryzen and I
seosoft
0
410
ロボットのための工場に灯りは要らない
watany
12
3.2k
Featured
See All Featured
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
4.2k
How to Think Like a Performance Engineer
csswizardry
28
2.5k
Producing Creativity
orderedlist
PRO
348
40k
Being A Developer After 40
akosma
91
590k
Testing 201, or: Great Expectations
jmmastey
46
8.1k
Designing for humans not robots
tammielis
254
26k
Visualization
eitanlees
150
17k
Self-Hosted WebAssembly Runtime for Runtime-Neutral Checkpoint/Restore in Edge–Cloud Continuum
chikuwait
0
420
The B2B funnel & how to create a winning content strategy
katarinadahlin
PRO
1
310
Become a Pro
speakerdeck
PRO
31
5.9k
Mozcon NYC 2025: Stop Losing SEO Traffic
samtorres
0
190
The #1 spot is gone: here's how to win anyway
tamaranovitovic
2
1k
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!