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
71
Unit testing in general
Adam
May 23, 2015
Tweet
Share
More Decks by Adam
See All by Adam
Unit testing with PHPUnit
adammbalogh
1
86
REST: Key Concepts
adammbalogh
2
190
Other Decks in Programming
See All in Programming
AI時代のUIはどこへ行く?
yusukebe
18
9.1k
AI Agents: How Do They Work and How to Build Them @ Shift 2025
slobodan
0
100
請來的 AI Agent 同事們在寫程式時,怎麼用 pytest 去除各種幻想與盲點
keitheis
0
130
アルテニア コンサル/ITエンジニア向け 採用ピッチ資料
altenir
0
110
Putting The Genie in the Bottle - A Crash Course on running LLMs on Android
iurysza
0
140
Kiroで始めるAI-DLC
kaonash
2
630
🔨 小さなビルドシステムを作る
momeemt
4
690
もうちょっといいRubyプロファイラを作りたい (2025)
osyoyu
1
460
CJK and Unicode From a PHP Committer
youkidearitai
PRO
0
110
RDoc meets YARD
okuramasafumi
4
170
2025 年のコーディングエージェントの現在地とエンジニアの仕事の変化について
azukiazusa1
24
12k
Navigating Dependency Injection with Metro
zacsweers
3
3.5k
Featured
See All Featured
Designing for humans not robots
tammielis
253
25k
Side Projects
sachag
455
43k
Facilitating Awesome Meetings
lara
55
6.5k
What’s in a name? Adding method to the madness
productmarketing
PRO
23
3.7k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
26
3k
VelocityConf: Rendering Performance Case Studies
addyosmani
332
24k
Large-scale JavaScript Application Architecture
addyosmani
513
110k
Git: the NoSQL Database
bkeepers
PRO
431
66k
Rails Girls Zürich Keynote
gr2m
95
14k
Typedesign – Prime Four
hannesfritz
42
2.8k
RailsConf 2023
tenderlove
30
1.2k
Measuring & Analyzing Core Web Vitals
bluesmoon
9
580
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!