SlideShare a Scribd company logo
1 of 55
Download to read offline
Test Driven Development
Zombie proof your code
Pascal Larocque
●
●
●
●
●
●
●

TrustCharge Team
Behat guy
Testing guy
SOLID guy
Pattern guy
Father of 3
Star Wars Geek

@pascallarocque
Why do we write code?
● Add New Features
● Maintaining old features
What are the challenges?
● I can’t understand what the code does
● Only the original programmer understands what it does… He’s not
with us anymore
● Fixing a Bug creates a NEW BUG
● There is no documentation
● It’s Only working on my local
● We can’t upgrade because all are code is dependent on this
version
● The only way to test is with the real data on production
● I have to test this manually
What are we really saying?

“I’m Scared...When I change something, some
other feature might stop working”
YOU’VE CREATED ZOMBIE CODE
How to survive the Zombie apocalypse
1. Write TESTS!
2. Write TESTABLE CODE
The Power of Testable code
●
●
●
●
●
●
●
●
●
●

Forces Simplification of the code
Simplifies Design
Documentation of the code
Less time debugging
New code doesn’t break old code
Refactoring becomes easier
Interfaces are better designed
Easier to do code reviews
Fearless programming
Faster than writing code without
tests
How to write testable code
PHPUnit
Database Testing
Legacy code testing
PHPspec
Why aren’t we testing?
●
●
●
●
●
●
●
●

Testing main flow is enough
The code is legacy, impossible to test
Don’t have time to test
Fix bug first then write a test
To be on the safe side manual testing is mandatory
No one else is writing tests
Testing and maintaining tests will cost more time
After changing code, I have to change a bunch of test
Sorry to say this but...
WRONG DESIGN
MISSING PROFESSIONALISM

DON’T BLAME TESTING!
LOSING DISCIPLINE

NO-TEST DEVELOPMENT

INEXPERIENCED DEVELOPERS
UNREVIEWED CODE
“THE SECRET TO TESTING IS
WRITING TESTABLE CODE”
HOW TO GET STARTED?
“If the answer is not obvious, or if the tests looks like the
tests would be ugly or had to write, then take that as a
warning signal.
Your design probably needs to modified; change things
around until the code is easy to test, and your design will
end up being better for the effort”
How can you tell if your code isn’t going to
come back from the SVN and BYTE you in the
APP?
Look for the following SYMPTOMS.
SYMPTOMS - CONSTRUCTOR DOES TOO MUCH
●
●
●
●
●
●

Using the NEW keyword in the constructor
STATIC METHOD calls
Anything more than field assignment
Object not fully initialized after constructor
CONTROL FLOW (conditions or loops) in constructor
Constructor build complex Collections instead of using
FACTORY or BUILDER
● There is an INITIALIZE block
● Not asking for Objects, looking for Objects
(DEPENDENCIES in constructor)
SYMPTOMS - Digging into dependencies
● Object passed are used to access other objects
● LAW OF DEMETER VIOLATION: Method call chain
with more than one ->
● SUSPICIOUS NAMES: Manager, Context, Registry,
Container
● Creating mocks that return MOCKS
● Deceitful API (Real Dependencies are unclear)
● Too many “Middle Men” objects
● Needle in a haystack (due to breaking demeter law)
SYMPTOMS - Singletons & Global state (the dark side)
●
●
●
●
●
●
●
●

Using SINGLETONS
Accessing GLOBAL STATE STATICALLY
Using STATIC FIELDS or STATIC METHODS
Using a STATIC INITIALIZATION block
Using a REGISTRIES
NOT (or MIS-) using DEPENDENCY INJECTION
Hard Coded Dependencies
You have to read EVERY LINE OF CODE to
understand the potential side effects
CURE
USE DEPENDENCY INJECTION
Replace your static method
Wrap static method from third party libraries
If you can’t wrap them, write adapters
SYMPTOMS - Object does too much
● When you have to use the word “AND” to describe
what the Object does
● Object is hard to read for team members
● Object have fields that are only used in some methods
● Object has static methods that only operate on
parameters
● MANY collaborators that you need to reach into for
more collaborators
● HIDDEN INTERACTIONS behind public methods
CURE
SOLID
Single Responsibility
Open/Close Principle
Liskov Substitution Principle
Interface Segregation
Dependency Inversion
Tests should...
● Run Without
○ Network connection
○ Production Data
○ External API

● Run Fast
○ ~milliseconds

● Must
○ be Easy to maintain
○ run before every
commit
Rules for TDD
1. Devise possible tests
2. Implement 1 FAILING
test
3. Implement just enough
code to PASS the test
4. Refactor code and clean
up design
5. Repeat until ALL tests
are passed

THINK DIFFERENTLY
TEST

DESIGN

IMPLEMENT

TEST
How to write testable code
PHPUnit
Database Testing
Legacy code testing
PHPspec
PHPUnit - Installation
●
●
●
●

Pear
Phar
Ubuntu package
Composer

Optional Packages
● Selenium
● DbUnit
● SkeletonGenerator
PHPUnit - Bootstrap
● Common Initialization of testing Environment
$baseDir = realpath(__DIR__.'/..');

require_once $baseDir. '/tc_platform/Include/minimalBootstrap.php';
require_once $baseDir . '/TrustChargeLibrairies/vendor/autoload.php';
require_once $baseDir . '/PHPLibs/vendor/autoload.php';
PHPUnit - Writing Test
● Use the skeleton generator (should be in Eclipse)
phpunit-skelgen --bootstrap bootstrap.php --test -- Class_Something Lib/Class/Something.php Class_SomethingTest
Tests/Lib/Class/SomethingTest.php

● Write it yourself
class Class_SomethingTest extends PHPUnit_Framework_TestCase
PHPUnit - Setup
●

●

●

setUpBeforeClass() - Executed before class is instantiated
○ Setup DB
○ Setup Files
setUp() - Executed before every test
○ Reset DB
○ Load Extra fixtures
tearDown() - Executed after Test
○ Clean up DB
○ Remove Files
Depends
@depends - For tests that
depend on output from other
tests
DataProvider
@dataProvider
- To test the
same function
with multiple
values
Stubs and Mocks
Stubs - Test double that
allows you to control the flow
of data
Mock - Test double that
allows you to control the flow
of data and that assert that
the object was called in a
specific way
Test Doubles
Doubles - When you
cannot replace a
static dependency
you can preload a test
doubles instead
Test Doubles
Doubles - When you
cannot replace a
static dependency
you can preload a test
doubles instead

Run in own process

Preload Test Doubles
How to write testable code
PHPUnit
Database Testing
Legacy code testing
PHPspec
Database testing
●
●
●
●

Must be fast
Need to control the data in the database
Must be ran in TESTING environment
Clean up after yourself
1. Setup - DB
Connection
Make sure you’re using
TEST DB

Use MYISAM to bypass
foreign key issues

Create the tables
2. Setup - Fixtures
Load Data

Export Data
3. Tests
Hard coded dependencies

Hard coded DB name
will cause you to
truncate production
DB instead of TEST
DB
CURE
Create new test
double interface
How to write testable code
PHPUnit
Database Testing
Legacy code testing
PHPspec
"Behavior is the most important thing about
software. It is what users depends on. Users
like it when we add behavior, but if we change
or remove behavior they depend on, they stop
trusting us."
Legacy Code Change Algorithm
1.
2.
3.
4.
5.

Identify the change points
Find test points
Break Dependencies
Write Tests
Makes changes and refactor
Identify test point
Find the code you
need to change

Find test points
Bridge Class
● Class that bridges the gap between legacy code and
new architecture
● Allows you to test in isolation
● Helps refactor out dependencies
Extract Method

Extract 535 Lines
of code
Bridge
How to write testable code
PHPUnit
Database Testing
Legacy code testing
PHPspec
PHPSpec
●

http://www.phpspec.net

While PHPUnit focuses on testing the functions and methods of the classes
used to develop features, specBDD focuses on testing the behaviors of these
classes.
“ describing the code before you actually write it is a fear management
technique. You don’t have to write all the code, just the spec of the next thing
you want to work on. ” -- Kent Beck
Demo
It’s not worth writing test unless you have
CONTINUOUS INTEGRATION

I

ZOMBIES
The broken window Theory
Don't leave "broken windows" (bad designs, wrong decisions, or poor code)
unrepaired. Fix each one as soon as it is discovered. Take some action to
prevent further damage and to show that you're on top of the situation.
We've seen clean, functional systems deteriorate pretty quickly once builds
start breaking. There are other factors that can contribute to software rot, but
neglect accelerates the rot faster than any other factor.
Test driven development - Zombie proof your code

More Related Content

What's hot

TDD - Test Driven Development
TDD - Test Driven DevelopmentTDD - Test Driven Development
TDD - Test Driven DevelopmentLim Chanmann
 
Test driven development vs Behavior driven development
Test driven development vs Behavior driven developmentTest driven development vs Behavior driven development
Test driven development vs Behavior driven developmentGallop Solutions
 
TDD That Was Easy!
TDD   That Was Easy!TDD   That Was Easy!
TDD That Was Easy!Kaizenko
 
A Not-So-Serious Introduction to Test Driven Development (TDD)
A Not-So-Serious Introduction to Test Driven Development (TDD) A Not-So-Serious Introduction to Test Driven Development (TDD)
A Not-So-Serious Introduction to Test Driven Development (TDD) CodeOps Technologies LLP
 
TDD - Test Driven Development
TDD - Test Driven DevelopmentTDD - Test Driven Development
TDD - Test Driven DevelopmentTung Nguyen Thanh
 
Test driven development
Test driven developmentTest driven development
Test driven developmentNascenia IT
 
Test Driven Development Powered by LEGO
Test Driven Development Powered by LEGOTest Driven Development Powered by LEGO
Test Driven Development Powered by LEGOAgile Montréal
 
Agile Test Driven Development
Agile Test Driven DevelopmentAgile Test Driven Development
Agile Test Driven DevelopmentViraf Karai
 
Tdd in php a brief example
Tdd in php   a brief exampleTdd in php   a brief example
Tdd in php a brief exampleJeremy Kendall
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Developmentguestc8093a6
 
Test Driven Development (TDD) Preso 360|Flex 2010
Test Driven Development (TDD) Preso 360|Flex 2010Test Driven Development (TDD) Preso 360|Flex 2010
Test Driven Development (TDD) Preso 360|Flex 2010guest5639fa9
 
Introduction to TDD (Test Driven development) - Ahmed Shreef
Introduction to TDD (Test Driven development) - Ahmed ShreefIntroduction to TDD (Test Driven development) - Ahmed Shreef
Introduction to TDD (Test Driven development) - Ahmed ShreefAhmed Shreef
 
Overview on TDD (Test Driven Development) & ATDD (Acceptance Test Driven Deve...
Overview on TDD (Test Driven Development) & ATDD (Acceptance Test Driven Deve...Overview on TDD (Test Driven Development) & ATDD (Acceptance Test Driven Deve...
Overview on TDD (Test Driven Development) & ATDD (Acceptance Test Driven Deve...Zohirul Alam Tiemoon
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven DevelopmentMireia Sangalo
 
TDD and Simple Design Workshop - Session 1 - March 2019
TDD and Simple Design Workshop - Session 1 - March 2019TDD and Simple Design Workshop - Session 1 - March 2019
TDD and Simple Design Workshop - Session 1 - March 2019Paulo Clavijo
 

What's hot (20)

TDD - Test Driven Development
TDD - Test Driven DevelopmentTDD - Test Driven Development
TDD - Test Driven Development
 
Tdd com Java
Tdd com JavaTdd com Java
Tdd com Java
 
Test drive on driven development process
Test drive on driven development processTest drive on driven development process
Test drive on driven development process
 
TDD = bra design?
TDD = bra design?TDD = bra design?
TDD = bra design?
 
Test driven development vs Behavior driven development
Test driven development vs Behavior driven developmentTest driven development vs Behavior driven development
Test driven development vs Behavior driven development
 
TDD That Was Easy!
TDD   That Was Easy!TDD   That Was Easy!
TDD That Was Easy!
 
A Not-So-Serious Introduction to Test Driven Development (TDD)
A Not-So-Serious Introduction to Test Driven Development (TDD) A Not-So-Serious Introduction to Test Driven Development (TDD)
A Not-So-Serious Introduction to Test Driven Development (TDD)
 
TDD - Test Driven Development
TDD - Test Driven DevelopmentTDD - Test Driven Development
TDD - Test Driven Development
 
Test driven development
Test driven developmentTest driven development
Test driven development
 
Test Driven Development Powered by LEGO
Test Driven Development Powered by LEGOTest Driven Development Powered by LEGO
Test Driven Development Powered by LEGO
 
Agile Test Driven Development
Agile Test Driven DevelopmentAgile Test Driven Development
Agile Test Driven Development
 
Tdd in php a brief example
Tdd in php   a brief exampleTdd in php   a brief example
Tdd in php a brief example
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
Test Driven Development (TDD) Preso 360|Flex 2010
Test Driven Development (TDD) Preso 360|Flex 2010Test Driven Development (TDD) Preso 360|Flex 2010
Test Driven Development (TDD) Preso 360|Flex 2010
 
PHPUnit - Unit testing
PHPUnit - Unit testingPHPUnit - Unit testing
PHPUnit - Unit testing
 
Introduction to TDD (Test Driven development) - Ahmed Shreef
Introduction to TDD (Test Driven development) - Ahmed ShreefIntroduction to TDD (Test Driven development) - Ahmed Shreef
Introduction to TDD (Test Driven development) - Ahmed Shreef
 
Overview on TDD (Test Driven Development) & ATDD (Acceptance Test Driven Deve...
Overview on TDD (Test Driven Development) & ATDD (Acceptance Test Driven Deve...Overview on TDD (Test Driven Development) & ATDD (Acceptance Test Driven Deve...
Overview on TDD (Test Driven Development) & ATDD (Acceptance Test Driven Deve...
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
TDD and Simple Design Workshop - Session 1 - March 2019
TDD and Simple Design Workshop - Session 1 - March 2019TDD and Simple Design Workshop - Session 1 - March 2019
TDD and Simple Design Workshop - Session 1 - March 2019
 
TDD and Getting Paid
TDD and Getting PaidTDD and Getting Paid
TDD and Getting Paid
 

Viewers also liked

PHPSpec & Behat: Two Testing Tools That Write Code For You (#phptek edition)
PHPSpec & Behat: Two Testing Tools That Write Code For You (#phptek edition)PHPSpec & Behat: Two Testing Tools That Write Code For You (#phptek edition)
PHPSpec & Behat: Two Testing Tools That Write Code For You (#phptek edition)Joshua Warren
 
Emergent design with phpspec
Emergent design with phpspecEmergent design with phpspec
Emergent design with phpspecMarcello Duarte
 
PhpSpec 2.0 ilustrated by examples
PhpSpec 2.0 ilustrated by examplesPhpSpec 2.0 ilustrated by examples
PhpSpec 2.0 ilustrated by examplesMarcello Duarte
 
Scaling Agile at Spotify (representation)
Scaling Agile at Spotify (representation)Scaling Agile at Spotify (representation)
Scaling Agile at Spotify (representation)Vlad Mysla
 

Viewers also liked (6)

PHPSpec & Behat: Two Testing Tools That Write Code For You (#phptek edition)
PHPSpec & Behat: Two Testing Tools That Write Code For You (#phptek edition)PHPSpec & Behat: Two Testing Tools That Write Code For You (#phptek edition)
PHPSpec & Behat: Two Testing Tools That Write Code For You (#phptek edition)
 
Hexagonal symfony
Hexagonal symfonyHexagonal symfony
Hexagonal symfony
 
PHPSpec BDD for PHP
PHPSpec BDD for PHPPHPSpec BDD for PHP
PHPSpec BDD for PHP
 
Emergent design with phpspec
Emergent design with phpspecEmergent design with phpspec
Emergent design with phpspec
 
PhpSpec 2.0 ilustrated by examples
PhpSpec 2.0 ilustrated by examplesPhpSpec 2.0 ilustrated by examples
PhpSpec 2.0 ilustrated by examples
 
Scaling Agile at Spotify (representation)
Scaling Agile at Spotify (representation)Scaling Agile at Spotify (representation)
Scaling Agile at Spotify (representation)
 

Similar to Test driven development - Zombie proof your code

Working With Legacy Code
Working With Legacy CodeWorking With Legacy Code
Working With Legacy CodeAndrea Polci
 
Test driven development_continuous_integration
Test driven development_continuous_integrationTest driven development_continuous_integration
Test driven development_continuous_integrationhaochenglee
 
Testing Rest with Spring by Kostiantyn Baranov (Senior Software Engineer, Gl...
Testing Rest with Spring  by Kostiantyn Baranov (Senior Software Engineer, Gl...Testing Rest with Spring  by Kostiantyn Baranov (Senior Software Engineer, Gl...
Testing Rest with Spring by Kostiantyn Baranov (Senior Software Engineer, Gl...GlobalLogic Ukraine
 
Keeping code clean
Keeping code cleanKeeping code clean
Keeping code cleanBrett Child
 
Unit testing legacy code
Unit testing legacy codeUnit testing legacy code
Unit testing legacy codeLars Thorup
 
Binary Studio Academy: .NET Code Testing
Binary Studio Academy: .NET Code TestingBinary Studio Academy: .NET Code Testing
Binary Studio Academy: .NET Code TestingBinary Studio
 
How do you tame a big ball of mud? One test at a time.
How do you tame a big ball of mud? One test at a time.How do you tame a big ball of mud? One test at a time.
How do you tame a big ball of mud? One test at a time.Matt Eland
 
Play with Testing on Android - Gilang Ramadhan (Academy Content Writer at Dic...
Play with Testing on Android - Gilang Ramadhan (Academy Content Writer at Dic...Play with Testing on Android - Gilang Ramadhan (Academy Content Writer at Dic...
Play with Testing on Android - Gilang Ramadhan (Academy Content Writer at Dic...DicodingEvent
 
Test Driven Development with PHP
Test Driven Development with PHPTest Driven Development with PHP
Test Driven Development with PHPRogério Vicente
 
Test-Driven Development.pptx
Test-Driven Development.pptxTest-Driven Development.pptx
Test-Driven Development.pptxTomas561914
 
A la découverte des google/test (aka gtest)
A la découverte des google/test (aka gtest)A la découverte des google/test (aka gtest)
A la découverte des google/test (aka gtest)Thierry Gayet
 
Indy meetup#7 effective unit-testing-mule
Indy meetup#7 effective unit-testing-muleIndy meetup#7 effective unit-testing-mule
Indy meetup#7 effective unit-testing-muleikram_ahamed
 
Test all the things! Automated testing with Drupal 8
Test all the things! Automated testing with Drupal 8Test all the things! Automated testing with Drupal 8
Test all the things! Automated testing with Drupal 8Sam Becker
 
Software Testing Basic Concepts
Software Testing Basic ConceptsSoftware Testing Basic Concepts
Software Testing Basic Conceptswesovi
 
Effective TDD - Less is more
Effective TDD - Less is moreEffective TDD - Less is more
Effective TDD - Less is moreBen Lau
 

Similar to Test driven development - Zombie proof your code (20)

Introduction to Unit Tests and TDD
Introduction to Unit Tests and TDDIntroduction to Unit Tests and TDD
Introduction to Unit Tests and TDD
 
Working With Legacy Code
Working With Legacy CodeWorking With Legacy Code
Working With Legacy Code
 
Test driven development
Test driven developmentTest driven development
Test driven development
 
Test driven development_continuous_integration
Test driven development_continuous_integrationTest driven development_continuous_integration
Test driven development_continuous_integration
 
Testing Rest with Spring by Kostiantyn Baranov (Senior Software Engineer, Gl...
Testing Rest with Spring  by Kostiantyn Baranov (Senior Software Engineer, Gl...Testing Rest with Spring  by Kostiantyn Baranov (Senior Software Engineer, Gl...
Testing Rest with Spring by Kostiantyn Baranov (Senior Software Engineer, Gl...
 
Keeping code clean
Keeping code cleanKeeping code clean
Keeping code clean
 
Unit testing legacy code
Unit testing legacy codeUnit testing legacy code
Unit testing legacy code
 
Binary Studio Academy: .NET Code Testing
Binary Studio Academy: .NET Code TestingBinary Studio Academy: .NET Code Testing
Binary Studio Academy: .NET Code Testing
 
How do you tame a big ball of mud? One test at a time.
How do you tame a big ball of mud? One test at a time.How do you tame a big ball of mud? One test at a time.
How do you tame a big ball of mud? One test at a time.
 
NET Code Testing
NET Code TestingNET Code Testing
NET Code Testing
 
Play with Testing on Android - Gilang Ramadhan (Academy Content Writer at Dic...
Play with Testing on Android - Gilang Ramadhan (Academy Content Writer at Dic...Play with Testing on Android - Gilang Ramadhan (Academy Content Writer at Dic...
Play with Testing on Android - Gilang Ramadhan (Academy Content Writer at Dic...
 
Spring Test Framework
Spring Test FrameworkSpring Test Framework
Spring Test Framework
 
Usable Software Design
Usable Software DesignUsable Software Design
Usable Software Design
 
Test Driven Development with PHP
Test Driven Development with PHPTest Driven Development with PHP
Test Driven Development with PHP
 
Test-Driven Development.pptx
Test-Driven Development.pptxTest-Driven Development.pptx
Test-Driven Development.pptx
 
A la découverte des google/test (aka gtest)
A la découverte des google/test (aka gtest)A la découverte des google/test (aka gtest)
A la découverte des google/test (aka gtest)
 
Indy meetup#7 effective unit-testing-mule
Indy meetup#7 effective unit-testing-muleIndy meetup#7 effective unit-testing-mule
Indy meetup#7 effective unit-testing-mule
 
Test all the things! Automated testing with Drupal 8
Test all the things! Automated testing with Drupal 8Test all the things! Automated testing with Drupal 8
Test all the things! Automated testing with Drupal 8
 
Software Testing Basic Concepts
Software Testing Basic ConceptsSoftware Testing Basic Concepts
Software Testing Basic Concepts
 
Effective TDD - Less is more
Effective TDD - Less is moreEffective TDD - Less is more
Effective TDD - Less is more
 

Recently uploaded

Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...apidays
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024The Digital Insurer
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 

Recently uploaded (20)

Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 

Test driven development - Zombie proof your code

  • 2. Pascal Larocque ● ● ● ● ● ● ● TrustCharge Team Behat guy Testing guy SOLID guy Pattern guy Father of 3 Star Wars Geek @pascallarocque
  • 3. Why do we write code? ● Add New Features ● Maintaining old features
  • 4. What are the challenges? ● I can’t understand what the code does ● Only the original programmer understands what it does… He’s not with us anymore ● Fixing a Bug creates a NEW BUG ● There is no documentation ● It’s Only working on my local ● We can’t upgrade because all are code is dependent on this version ● The only way to test is with the real data on production ● I have to test this manually
  • 5. What are we really saying? “I’m Scared...When I change something, some other feature might stop working”
  • 7. How to survive the Zombie apocalypse 1. Write TESTS! 2. Write TESTABLE CODE
  • 8. The Power of Testable code ● ● ● ● ● ● ● ● ● ● Forces Simplification of the code Simplifies Design Documentation of the code Less time debugging New code doesn’t break old code Refactoring becomes easier Interfaces are better designed Easier to do code reviews Fearless programming Faster than writing code without tests
  • 9. How to write testable code PHPUnit Database Testing Legacy code testing PHPspec
  • 10. Why aren’t we testing? ● ● ● ● ● ● ● ● Testing main flow is enough The code is legacy, impossible to test Don’t have time to test Fix bug first then write a test To be on the safe side manual testing is mandatory No one else is writing tests Testing and maintaining tests will cost more time After changing code, I have to change a bunch of test
  • 11. Sorry to say this but...
  • 12. WRONG DESIGN MISSING PROFESSIONALISM DON’T BLAME TESTING! LOSING DISCIPLINE NO-TEST DEVELOPMENT INEXPERIENCED DEVELOPERS UNREVIEWED CODE
  • 13. “THE SECRET TO TESTING IS WRITING TESTABLE CODE”
  • 14. HOW TO GET STARTED? “If the answer is not obvious, or if the tests looks like the tests would be ugly or had to write, then take that as a warning signal. Your design probably needs to modified; change things around until the code is easy to test, and your design will end up being better for the effort”
  • 15. How can you tell if your code isn’t going to come back from the SVN and BYTE you in the APP? Look for the following SYMPTOMS.
  • 16. SYMPTOMS - CONSTRUCTOR DOES TOO MUCH ● ● ● ● ● ● Using the NEW keyword in the constructor STATIC METHOD calls Anything more than field assignment Object not fully initialized after constructor CONTROL FLOW (conditions or loops) in constructor Constructor build complex Collections instead of using FACTORY or BUILDER ● There is an INITIALIZE block ● Not asking for Objects, looking for Objects (DEPENDENCIES in constructor)
  • 17. SYMPTOMS - Digging into dependencies ● Object passed are used to access other objects ● LAW OF DEMETER VIOLATION: Method call chain with more than one -> ● SUSPICIOUS NAMES: Manager, Context, Registry, Container ● Creating mocks that return MOCKS ● Deceitful API (Real Dependencies are unclear) ● Too many “Middle Men” objects ● Needle in a haystack (due to breaking demeter law)
  • 18. SYMPTOMS - Singletons & Global state (the dark side) ● ● ● ● ● ● ● ● Using SINGLETONS Accessing GLOBAL STATE STATICALLY Using STATIC FIELDS or STATIC METHODS Using a STATIC INITIALIZATION block Using a REGISTRIES NOT (or MIS-) using DEPENDENCY INJECTION Hard Coded Dependencies You have to read EVERY LINE OF CODE to understand the potential side effects
  • 19. CURE USE DEPENDENCY INJECTION Replace your static method Wrap static method from third party libraries If you can’t wrap them, write adapters
  • 20. SYMPTOMS - Object does too much ● When you have to use the word “AND” to describe what the Object does ● Object is hard to read for team members ● Object have fields that are only used in some methods ● Object has static methods that only operate on parameters ● MANY collaborators that you need to reach into for more collaborators ● HIDDEN INTERACTIONS behind public methods
  • 21. CURE SOLID Single Responsibility Open/Close Principle Liskov Substitution Principle Interface Segregation Dependency Inversion
  • 22. Tests should... ● Run Without ○ Network connection ○ Production Data ○ External API ● Run Fast ○ ~milliseconds ● Must ○ be Easy to maintain ○ run before every commit
  • 23. Rules for TDD 1. Devise possible tests 2. Implement 1 FAILING test 3. Implement just enough code to PASS the test 4. Refactor code and clean up design 5. Repeat until ALL tests are passed THINK DIFFERENTLY TEST DESIGN IMPLEMENT TEST
  • 24. How to write testable code PHPUnit Database Testing Legacy code testing PHPspec
  • 25. PHPUnit - Installation ● ● ● ● Pear Phar Ubuntu package Composer Optional Packages ● Selenium ● DbUnit ● SkeletonGenerator
  • 26. PHPUnit - Bootstrap ● Common Initialization of testing Environment $baseDir = realpath(__DIR__.'/..'); require_once $baseDir. '/tc_platform/Include/minimalBootstrap.php'; require_once $baseDir . '/TrustChargeLibrairies/vendor/autoload.php'; require_once $baseDir . '/PHPLibs/vendor/autoload.php';
  • 27. PHPUnit - Writing Test ● Use the skeleton generator (should be in Eclipse) phpunit-skelgen --bootstrap bootstrap.php --test -- Class_Something Lib/Class/Something.php Class_SomethingTest Tests/Lib/Class/SomethingTest.php ● Write it yourself class Class_SomethingTest extends PHPUnit_Framework_TestCase
  • 28. PHPUnit - Setup ● ● ● setUpBeforeClass() - Executed before class is instantiated ○ Setup DB ○ Setup Files setUp() - Executed before every test ○ Reset DB ○ Load Extra fixtures tearDown() - Executed after Test ○ Clean up DB ○ Remove Files
  • 29. Depends @depends - For tests that depend on output from other tests
  • 30. DataProvider @dataProvider - To test the same function with multiple values
  • 31. Stubs and Mocks Stubs - Test double that allows you to control the flow of data Mock - Test double that allows you to control the flow of data and that assert that the object was called in a specific way
  • 32. Test Doubles Doubles - When you cannot replace a static dependency you can preload a test doubles instead
  • 33. Test Doubles Doubles - When you cannot replace a static dependency you can preload a test doubles instead Run in own process Preload Test Doubles
  • 34. How to write testable code PHPUnit Database Testing Legacy code testing PHPspec
  • 35. Database testing ● ● ● ● Must be fast Need to control the data in the database Must be ran in TESTING environment Clean up after yourself
  • 36. 1. Setup - DB Connection Make sure you’re using TEST DB Use MYISAM to bypass foreign key issues Create the tables
  • 37. 2. Setup - Fixtures Load Data Export Data
  • 39.
  • 40. Hard coded dependencies Hard coded DB name will cause you to truncate production DB instead of TEST DB
  • 42. How to write testable code PHPUnit Database Testing Legacy code testing PHPspec
  • 43.
  • 44. "Behavior is the most important thing about software. It is what users depends on. Users like it when we add behavior, but if we change or remove behavior they depend on, they stop trusting us."
  • 45. Legacy Code Change Algorithm 1. 2. 3. 4. 5. Identify the change points Find test points Break Dependencies Write Tests Makes changes and refactor
  • 46. Identify test point Find the code you need to change Find test points
  • 47. Bridge Class ● Class that bridges the gap between legacy code and new architecture ● Allows you to test in isolation ● Helps refactor out dependencies
  • 48. Extract Method Extract 535 Lines of code
  • 50. How to write testable code PHPUnit Database Testing Legacy code testing PHPspec
  • 51. PHPSpec ● http://www.phpspec.net While PHPUnit focuses on testing the functions and methods of the classes used to develop features, specBDD focuses on testing the behaviors of these classes. “ describing the code before you actually write it is a fear management technique. You don’t have to write all the code, just the spec of the next thing you want to work on. ” -- Kent Beck
  • 52. Demo
  • 53. It’s not worth writing test unless you have CONTINUOUS INTEGRATION I ZOMBIES
  • 54. The broken window Theory Don't leave "broken windows" (bad designs, wrong decisions, or poor code) unrepaired. Fix each one as soon as it is discovered. Take some action to prevent further damage and to show that you're on top of the situation. We've seen clean, functional systems deteriorate pretty quickly once builds start breaking. There are other factors that can contribute to software rot, but neglect accelerates the rot faster than any other factor.