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
66
Unit testing in general
Adam
May 23, 2015
Tweet
Share
More Decks by Adam
See All by Adam
Unit testing with PHPUnit
adammbalogh
1
84
REST: Key Concepts
adammbalogh
2
190
Other Decks in Programming
See All in Programming
AI時代におけるSRE、 あるいはエンジニアの生存戦略
pyama86
4
1.1k
Tauriでネイティブアプリを作りたい
tsucchinoko
0
360
Jakarta EE meets AI
ivargrimstad
0
390
Streams APIとTCPフロー制御 / Web Streams API and TCP flow control
tasshi
2
350
JavaでLチカしたい! / JJUG CCC 2024 Fall LT
nhayato
0
130
Quine, Polyglot, 良いコード
qnighy
4
630
3rd party scriptでもReactを使いたい! Preact + Reactのハイブリッド開発
righttouch
PRO
1
600
NSOutlineView何もわからん:( 前編 / I Don't Understand About NSOutlineView :( Pt. 1
usagimaru
0
310
レガシーシステムにどう立ち向かうか 複雑さと理想と現実/vs-legacy
suzukihoge
14
2.1k
Contemporary Test Cases
maaretp
0
120
ピラミッド、アイスクリームコーン、SMURF: 自動テストの最適バランスを求めて / Pyramid Ice-Cream-Cone and SMURF
twada
PRO
10
1.2k
Boost Performance and Developer Productivity with Jakarta EE 11
ivargrimstad
0
1.7k
Featured
See All Featured
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
44
6.8k
The Illustrated Children's Guide to Kubernetes
chrisshort
48
48k
Speed Design
sergeychernyshev
24
610
GraphQLとの向き合い方2022年版
quramy
43
13k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
280
13k
Intergalactic Javascript Robots from Outer Space
tanoku
269
27k
[RailsConf 2023] Rails as a piece of cake
palkan
51
4.9k
What’s in a name? Adding method to the madness
productmarketing
PRO
22
3.1k
Fontdeck: Realign not Redesign
paulrobertlloyd
82
5.2k
5 minutes of I Can Smell Your CMS
philhawksworth
202
19k
Designing on Purpose - Digital PM Summit 2013
jponch
115
7k
Building a Modern Day E-commerce SEO Strategy
aleyda
38
6.9k
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!