SlideShare ist ein Scribd-Unternehmen logo
1 von 61
Downloaden Sie, um offline zu lesen
Automated tests
Facts and myths
Wojciech Sznapka / @sznapka
Cześć
An introduction
I work in software industry for about 9 years
Care a lot about robust and testable architectures
Loves software craftsmanship, sophisticated architectures, Big Data
and ice hockey
Test Driven
Development
What is this?
Add a test
Write an implementation
Refactor code
Repeat!
What's
important here?
... to have tests
You won’t go to hell if you’ll
write a test after declaring an
interface or prototyping a
class ...
... but you’ll surely end up in
hell, if there won’t be a test
coverage
Myths about
automated tests
Boss doesn't pay
for automated tests
It's a myth!
A 2005 study found that using TDD meant writing
more tests and, in turn, programmers who wrote
more tests tended to be more productive
by American Scientists
But this ain't that hard ...
casRmnovre
ls oaCnetr
{
poetd$ovrin =[
rtce cnesos
10 = '' 90= 'M,50= '' 40= 'D,10= ''
00 > M, 0 > C' 0 > D, 0 > C' 0 > C,
9 = 'C,5 = '' 4 = 'L,1 = ''
0 > X' 0 > L, 0 > X' 0 > X,
9= 'X,5= '' 4= 'V,1= '';
> I'
> V,
> I'
> I]
pbi fnto cnet$nrbc
ulc ucin ovr(iAai)
{
i (i_uei(iAai) {
f !snmrc$nrbc)
trwnwnaiAgmnEcpin' cnetnmrc';
ho e Ivldruetxeto(I ovr ueis)
}
i (iAai < 0 {
f $nrbc = )
rtr ';
eun '
}
ls (aai,$oa)=$hs>ovrinatro(iAai)
it $rbc rmn
ti-cnesoFcoFr$nrbc;
}

rtr $oa .$hs>ovr(iAai -$rbc;
eun rmn
ti-cnet$nrbc
aai)

poetdfnto cnesoFcoFr$nrbc
rtce ucin ovrinatro(iAai)
{
frah(ti-cnesosa $rbc= $oa){
oec $hs>ovrin s aai > rmn
i (aai < $nrbc {
f $rbc = iAai)
rtr [aai,$oa]
eun $rbc rmn;
}
}
}
}
... start from obvious things
casRmnovreTs etnsPPntFaeokTsCs
ls oaCnetret xed HUi_rmwr_etae
{
pbi fnto tsEpy)
ulc ucin etmt(
{
$hs>setqas',(e Rmnovre)>ovr(')
ti-asrEul(' nw oaCnetr-cnet');
}
}
... provide some meat ...
casRmnovreTs etnsPPntFaeokTsCs
ls oaCnetret xed HUi_rmwr_etae
{
/*@aarvdrpoieetaa*
* dtPoie rvdTsDt /
pbi fnto tsCnesos$rbc $oa)
ulc ucin etovrin(aai, rmn
{
$ovre =nwRmnovre(;
cnetr
e oaCnetr)
$hs>setqas$oa,$ovre-cnet$rbc)
ti-asrEul(rmn cnetr>ovr(aai);
}

}

pbi fnto poieetaa)
ulc ucin rvdTsDt(
{
rtr ary
eun ra[
[47 'MCXVI]
39, MMDCI',
[,'',[,'I] [,'I] [,'X] [0 'L]
1 I] 2 I', 6 V', 9 I', 4, X',
[5 'L',[0 'C] [0,'',[0,'D]
4, XV] 9, X', 10 C] 40 C'
]
;
}
... test edge cases
casRmnovreTs etnsPPntFaeokTsCs
ls oaCnetret xed HUi_rmwr_etae
{
/*@xetdxeto naiAgmnEcpin*
* epceEcpin Ivldruetxeto /
pbi fnto tsEpy)
ulc ucin etmt(
{
(e Rmnovre)>ovr(wfIa psighr')
nw oaCnetr-cnet't
m asn ee);
}
}
Help yourself with tests generation
ppntsegn-ts Rmnovre
hui-kle -et oaCnetr
... or using Symfony's
XSolveUnitSkelgenBundle
.apcnoexov:kle:etAm/xmlBnl/evc/
/p/osl slesegnts ceEapeudeSrie.
.apcnoexov:kle:etAm//otolr*
/p/osl slesegnts ce*Cnrle/
We are building
too complicated
system
for an automated tests
TDD forces you to write
cleaner code
It's easier to test smaller units
of code
so you write smaller classes
which are less coupled
and that makes your system more stable
and open for an extension in the future
Your Object Oriented design
becomes S.O.L.I.D. compilant
and this is awesome!
You will of course tackle
costly dependencies
Use PHPUnit's mock
framework or Mockery
Fake it till you make it
casMceTs etnsPPntFaeokTsCs
ls okdet xed HUi_rmwr_etae
{
pbi fnto tsNnxsigauOjcs)
ulc ucin etoEitnVlebet(
{
$ofgrto =okr:mc(ofgrtoVlebet,[
cniuain
Mcey:ok'CniuainauOjc'
'eUl = 'tp/tdsnpap'
gtr' > ht:/d.zak.l,
'eFra'= 'm')
gtomt > xl];
$hs>setqas'm' $ofgrto-gtomt);/ O
ti-asrEul(xl, cniuain>eFra() / K
}
}
Mock things
that can't be tested quickly or nonreproducable
casMceTs etnsPPntFaeokTsCs
ls okdet xed HUi_rmwr_etae
{
pbi fnto tsAial(
ulc ucin etpCls)
{
$uzok okr:mc(uzBosr)
bzMc= Mcey:ok'Bzrwe';
$uzok>hudeev(gt)
bzMc-solRcie'e'
-adeun'rsos>mo>wsmt<mo>/epne';
>nRtr(<epne<odAeoiy/od<rsos>)
/ ti as i ws slto,t wiexlfxue i fl
/ hs lo s ie ouin o rt m itrs n ie
/ $uzok>hudeev(gt)>ne)
/ bzMc-solRcie'e'-oc(
/
/
-adeunfl_e_otns_DR_.'fxue.m')
>nRtr(iegtcnet(_I_
/itrsxl);

}

}

$p =nwpCnue(bzMc)
ai
e Aiosmr$uzok;
$p-asrEul(Aeoiy,$p-gturnMo() / O
ai>setqas'wsmt' ai>eCretod); / K
Expect declared behaviors
casMceTs etnsPPntFaeokTsCs
ls okdet xed HUi_rmwr_etae
{
pbi fnto tsEpcainDcaain(
ulc ucin etxettoselrtos)
{
$uzok okr:mc(uzBosr)
bzMc= Mcey:ok'Bzrwe';
$uzok>hudeev(gt)
bzMc-solRcie'e'
-adeun'rsos>mo>wsmt<mo>/epne';
>nRtr(<epne<odAeoiy/od<rsos>)
$ogrok=okr:mc(oooogr)
lgeMc
Mcey:ok'MnlgLge';
/ w js wn t b sr ta Lge:if wscle ol oc
/ e ut at o e ue ht ogr:no a ald ny ne
$ogrok>hudeev(if'-oc(;
lgeMc-solRcie'no)>ne)

}

}

$p =nwpCnue(bzMc,$ogrok;
ai
e Aiosmr$uzok lgeMc)
$p-asrEul(Aeoiy,$p-gturnMo() / O
ai>setqas'wsmt' ai>eCretod); / K
Be prepared for failures
and check if you prepared for unexpected situations
casMceTs etnsPPntFaeokTsCs
ls okdet xed HUi_rmwr_etae
{
/*@xetdxeto yxetoWapr*
* epceEcpin MEcpinrpe /
pbi fnto tsFieCneto(
ulc ucin etaldoncin)
{
$uzok okr:mc(uzBosr)
bzMc= Mcey:ok'Bzrwe';
$uzok>hudeev(gt)
bzMc-solRcie'e'
-adho(uzEcpinCinEcpin)
>nTrw'Bzxetoletxeto';
$ogrok=okr:mc(oooogr)
lgeMc
Mcey:ok'MnlgLge';
$ogrok>hudeev(if'-nvr)
lgeMc-solRcie'no)>ee(;
$ogrok>hudeev(er)>ne)
lgeMc-solRcie'r'-oc(;

}

}

(e pCnue(bzMc,$ogrok)>eCretod)
nw Aiosmr$uzok lgeMc)-gturnMo(;
We providing
API for external
consumers
it can't be tested...
Use Symfony's WebTestCase
to test your API
I call it integration tests
Call your API and check if it
returns prepared data
casEpniueotolretetnsWbetae
ls xedtrCnrleTs xed eTsCs
{
ueIoaeTssri;/ i rst ts evrnet
s sltdetTat / t ees et niomn
pbi fnto tsGtitnsn)
ulc ucin eteLsIJo(
{
$let=sai:cetCin(;
cin
ttc:raelet)
$let>eus(GT,'epniue.sn)
cin-rqet'E' /xedtrsjo';
$sn=jo_eoe$let>eRsos(-gtotn()
jo
sndcd(cin-gtepne)>eCnet);

}

}

$hs>setre$let>eRsos(-iScesu()
ti-asrTu(cin-gtepne)>sucsfl);
$hs>seton(0 $sn;
ti-asrCut8, jo)
$hs>setraeTaOEul
ti-asrGetrhnrqa(
nwaeie$sn7]>rae_t,
e DtTm(jo[9-cetda)
nwaeie$sn0-cetda);
e DtTm(jo[]>rae_t)
Use fixtures and reset
environment
IsolatedTestsTrait should do the trick
Steps required to effectively
run in isolation
1.
2.
3.
4.
5.
6.
7.

configure PDO SQLite in file
create database
drop schema
load fixtres
copy database as a backup
copy database from backup for every test
delete database backup after test suite
Requirements
changes
frequently
we can't keep up with unit
tests
Use behavioral
approach
Behat in PHP
Describe features as scenarios
It will be readable for: business, developers and machines
Perfectly fits into Agile
process
Simple example
Faue lo frajb
etr: ok o
o
I odrt fn co jb
n re o id ol o
A a aprn porme
s n siig rgamr
Ine t b al t ls jbofr
ed o e be o it o fes
Seai:ls ofr frP vrin
cnro it fes o L eso
GvnIa o ""
ie
m n /
Te Isol se"oązd tau
hn
hud e Dłc o em"
Adcik"oązd tau
n lc Dłc o em"
Te Isol b o "krea
hn
hud e n /air"
AdIsol se"H Sno Dvlpr(lwc)
n
hud e PP eir eeoe Giie"
Seai:n ofr frE st
cnro o fes o N ie
GvnIa o "e"
ie
m n /n
Te Isol ntse"oązd tau
hn
hud o e Dłc o em"
AdIsol ntsea "ji-s eeet
n
hud o e n #onu" lmn
Our tests are
slow!
This could be true ...
Always use in-memory sqlite
database
Or create clean sqlite database and copy
it for every test
Group your tests
Use PHPUnit's @group or Behat's @tag
Create "smoke tests" groups
Those should be fast test, which ensures your system is most likely
stable
Slower tests should run during
night
Facts
It gives you confidence
about changes and your code
Team is able to rapidly
experiment with code
TDD enforces better
Object Oriented design
Smaller units of code and lower coupling always leads to better
understanding of the codebase by future devs
End user experiences better
quality
lower 500 error ratio in the production
Happier users
↓
more $$$ in future
Conclusion
Setting up an working
environment for automated
tests
is timely costly at the begining, but it pays off in the future
System without any kind of
automated tests
has big potential to be a big ball of mud
Good coverage can be easily
achieved with
mix of unit, functional and behavioral tests
You need to build and
cultivate
TDD culture in your surrounding
Thank you so much for
attending!
Feedback is much appreciated
wojciech@sznapka.pl
Twitter: @sznapka
GitHub: @wowo
Join XSolve team :-)

Weitere ähnliche Inhalte

Was ist angesagt?

The State of PHPUnit
The State of PHPUnitThe State of PHPUnit
The State of PHPUnit
Edorian
 
บทที่6 update&delete
บทที่6 update&deleteบทที่6 update&delete
บทที่6 update&delete
Palm Unnop
 

Was ist angesagt? (20)

[Webinar] An Hour with the CTO: All About APIs and the Nuxeo Platform
[Webinar] An Hour with the CTO: All About APIs and the Nuxeo Platform[Webinar] An Hour with the CTO: All About APIs and the Nuxeo Platform
[Webinar] An Hour with the CTO: All About APIs and the Nuxeo Platform
 
Windows shutdown virus source code c++
Windows shutdown virus source code c++Windows shutdown virus source code c++
Windows shutdown virus source code c++
 
The State of PHPUnit
The State of PHPUnitThe State of PHPUnit
The State of PHPUnit
 
Introduction to JavaFX 2
Introduction to JavaFX 2Introduction to JavaFX 2
Introduction to JavaFX 2
 
Send mail-oracle11g-det
Send mail-oracle11g-detSend mail-oracle11g-det
Send mail-oracle11g-det
 
The State of PHPUnit
The State of PHPUnitThe State of PHPUnit
The State of PHPUnit
 
Varnish, ESI & Cookies @Service-Public
Varnish, ESI & Cookies @Service-PublicVarnish, ESI & Cookies @Service-Public
Varnish, ESI & Cookies @Service-Public
 
Scaling antispam solutions with Puppet
Scaling antispam solutions with PuppetScaling antispam solutions with Puppet
Scaling antispam solutions with Puppet
 
learn you some erlang - chap11 to chap12
learn you some erlang - chap11 to chap12learn you some erlang - chap11 to chap12
learn you some erlang - chap11 to chap12
 
Algoritma 5 november wiwik p.l
Algoritma 5 november wiwik p.lAlgoritma 5 november wiwik p.l
Algoritma 5 november wiwik p.l
 
Continuous testing In PHP
Continuous testing In PHPContinuous testing In PHP
Continuous testing In PHP
 
Getting started with TDD - Confoo 2014
Getting started with TDD - Confoo 2014Getting started with TDD - Confoo 2014
Getting started with TDD - Confoo 2014
 
install PHP7 on CentOS7 by Ansible
install PHP7 on CentOS7 by Ansibleinstall PHP7 on CentOS7 by Ansible
install PHP7 on CentOS7 by Ansible
 
Spock framework
Spock frameworkSpock framework
Spock framework
 
Feb14 successful development
Feb14 successful developmentFeb14 successful development
Feb14 successful development
 
Особенности фоновой работы iOS-приложения на примере синхронизации Яндекс.Ди...
 Особенности фоновой работы iOS-приложения на примере синхронизации Яндекс.Ди... Особенности фоновой работы iOS-приложения на примере синхронизации Яндекс.Ди...
Особенности фоновой работы iOS-приложения на примере синхронизации Яндекс.Ди...
 
บทที่6 update&delete
บทที่6 update&deleteบทที่6 update&delete
บทที่6 update&delete
 
Microprocessors and interfacing d.a.godse a.p.godse
Microprocessors and interfacing   d.a.godse a.p.godseMicroprocessors and interfacing   d.a.godse a.p.godse
Microprocessors and interfacing d.a.godse a.p.godse
 
Tests unitaires mock_kesako_20130516
Tests unitaires mock_kesako_20130516Tests unitaires mock_kesako_20130516
Tests unitaires mock_kesako_20130516
 
Changelog
ChangelogChangelog
Changelog
 

Andere mochten auch

Mongo db – document oriented database
Mongo db – document oriented databaseMongo db – document oriented database
Mongo db – document oriented database
Wojciech Sznapka
 

Andere mochten auch (9)

Łebski Development czyli kiedy i dlaczego tworzyć oprogramowanie pod klucz i ...
Łebski Development czyli kiedy i dlaczego tworzyć oprogramowanie pod klucz i ...Łebski Development czyli kiedy i dlaczego tworzyć oprogramowanie pod klucz i ...
Łebski Development czyli kiedy i dlaczego tworzyć oprogramowanie pod klucz i ...
 
Smart development environments
Smart development environmentsSmart development environments
Smart development environments
 
MongoDB - baza danych zorientowana dokumentowo. Czy ruch NoSQL ma sens?
MongoDB - baza danych zorientowana dokumentowo. Czy ruch NoSQL ma sens?MongoDB - baza danych zorientowana dokumentowo. Czy ruch NoSQL ma sens?
MongoDB - baza danych zorientowana dokumentowo. Czy ruch NoSQL ma sens?
 
Symfony2 showcase
Symfony2 showcaseSymfony2 showcase
Symfony2 showcase
 
Symfony2 w chmurze
Symfony2 w chmurzeSymfony2 w chmurze
Symfony2 w chmurze
 
Mongo db – document oriented database
Mongo db – document oriented databaseMongo db – document oriented database
Mongo db – document oriented database
 
MUSIC and ARTS Gr. 10 Learner's Module Quarter 1 to 4 complete
MUSIC and ARTS Gr. 10 Learner's Module   Quarter 1 to 4 completeMUSIC and ARTS Gr. 10 Learner's Module   Quarter 1 to 4 complete
MUSIC and ARTS Gr. 10 Learner's Module Quarter 1 to 4 complete
 
The Outcome Economy
The Outcome EconomyThe Outcome Economy
The Outcome Economy
 
The Six Highest Performing B2B Blog Post Formats
The Six Highest Performing B2B Blog Post FormatsThe Six Highest Performing B2B Blog Post Formats
The Six Highest Performing B2B Blog Post Formats
 

Ähnlich wie Automated tests - facts and myths

Refactoring to symfony components
Refactoring to symfony componentsRefactoring to symfony components
Refactoring to symfony components
Michael Peacock
 
Introduction to AngularJS
Introduction to AngularJSIntroduction to AngularJS
Introduction to AngularJS
Pat Cito
 
Nginx Scripting - Extending Nginx Functionalities with Lua
Nginx Scripting - Extending Nginx Functionalities with LuaNginx Scripting - Extending Nginx Functionalities with Lua
Nginx Scripting - Extending Nginx Functionalities with Lua
Tony Fabeen
 
Devinsampa nginx-scripting
Devinsampa nginx-scriptingDevinsampa nginx-scripting
Devinsampa nginx-scripting
Tony Fabeen
 

Ähnlich wie Automated tests - facts and myths (20)

PhpUnit Best Practices
PhpUnit Best PracticesPhpUnit Best Practices
PhpUnit Best Practices
 
Refactoring to symfony components
Refactoring to symfony componentsRefactoring to symfony components
Refactoring to symfony components
 
Building modern web apps with html5, javascript, and java
Building modern web apps with html5, javascript, and javaBuilding modern web apps with html5, javascript, and java
Building modern web apps with html5, javascript, and java
 
Advanced QUnit - Front-End JavaScript Unit Testing
Advanced QUnit - Front-End JavaScript Unit TestingAdvanced QUnit - Front-End JavaScript Unit Testing
Advanced QUnit - Front-End JavaScript Unit Testing
 
Learn Frontend Testing
Learn Frontend TestingLearn Frontend Testing
Learn Frontend Testing
 
Javascript: The Important Bits
Javascript: The Important BitsJavascript: The Important Bits
Javascript: The Important Bits
 
One page app with AngularJS
One page app with AngularJSOne page app with AngularJS
One page app with AngularJS
 
An Introduction to CSS Preprocessors
An Introduction to CSS PreprocessorsAn Introduction to CSS Preprocessors
An Introduction to CSS Preprocessors
 
A Backbone.js Tutorial for the Impatient - Part 1
A Backbone.js Tutorial for the Impatient - Part 1A Backbone.js Tutorial for the Impatient - Part 1
A Backbone.js Tutorial for the Impatient - Part 1
 
Introduction to AngularJS
Introduction to AngularJSIntroduction to AngularJS
Introduction to AngularJS
 
Nginx Scripting - Extending Nginx Functionalities with Lua
Nginx Scripting - Extending Nginx Functionalities with LuaNginx Scripting - Extending Nginx Functionalities with Lua
Nginx Scripting - Extending Nginx Functionalities with Lua
 
Devinsampa nginx-scripting
Devinsampa nginx-scriptingDevinsampa nginx-scripting
Devinsampa nginx-scripting
 
Testing Code and Assuring Quality
Testing Code and Assuring QualityTesting Code and Assuring Quality
Testing Code and Assuring Quality
 
Testing TYPO3 Applications
Testing TYPO3 ApplicationsTesting TYPO3 Applications
Testing TYPO3 Applications
 
Angular.js + Rails at WeWork or: The Accidental Feature
Angular.js + Rails at WeWork or: The Accidental FeatureAngular.js + Rails at WeWork or: The Accidental Feature
Angular.js + Rails at WeWork or: The Accidental Feature
 
Build a custom (micro)framework with ZF2 Components (as building blocks)
Build a custom (micro)framework with ZF2 Components (as building blocks)Build a custom (micro)framework with ZF2 Components (as building blocks)
Build a custom (micro)framework with ZF2 Components (as building blocks)
 
Hubot: a look inside our robot friend
Hubot: a look inside our robot friendHubot: a look inside our robot friend
Hubot: a look inside our robot friend
 
Geb for browser automation
Geb for browser automationGeb for browser automation
Geb for browser automation
 
2013-02-21 - .NET UG Rhein-Neckar: JavaScript Best Practices
2013-02-21 - .NET UG Rhein-Neckar: JavaScript Best Practices2013-02-21 - .NET UG Rhein-Neckar: JavaScript Best Practices
2013-02-21 - .NET UG Rhein-Neckar: JavaScript Best Practices
 
NDC 2011 - The FLUID Principles
NDC 2011 - The FLUID PrinciplesNDC 2011 - The FLUID Principles
NDC 2011 - The FLUID Principles
 

Kürzlich hochgeladen

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 
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
Safe Software
 

Kürzlich hochgeladen (20)

MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 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
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
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
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
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
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
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...
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
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
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
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
 

Automated tests - facts and myths

  • 1. Automated tests Facts and myths Wojciech Sznapka / @sznapka
  • 3. An introduction I work in software industry for about 9 years Care a lot about robust and testable architectures Loves software craftsmanship, sophisticated architectures, Big Data and ice hockey
  • 10. ... to have tests
  • 11. You won’t go to hell if you’ll write a test after declaring an interface or prototyping a class ...
  • 12. ... but you’ll surely end up in hell, if there won’t be a test coverage
  • 14. Boss doesn't pay for automated tests
  • 15. It's a myth! A 2005 study found that using TDD meant writing more tests and, in turn, programmers who wrote more tests tended to be more productive by American Scientists
  • 16. But this ain't that hard ... casRmnovre ls oaCnetr { poetd$ovrin =[ rtce cnesos 10 = '' 90= 'M,50= '' 40= 'D,10= '' 00 > M, 0 > C' 0 > D, 0 > C' 0 > C, 9 = 'C,5 = '' 4 = 'L,1 = '' 0 > X' 0 > L, 0 > X' 0 > X, 9= 'X,5= '' 4= 'V,1= ''; > I' > V, > I' > I] pbi fnto cnet$nrbc ulc ucin ovr(iAai) { i (i_uei(iAai) { f !snmrc$nrbc) trwnwnaiAgmnEcpin' cnetnmrc'; ho e Ivldruetxeto(I ovr ueis) } i (iAai < 0 { f $nrbc = ) rtr '; eun ' } ls (aai,$oa)=$hs>ovrinatro(iAai) it $rbc rmn ti-cnesoFcoFr$nrbc; } rtr $oa .$hs>ovr(iAai -$rbc; eun rmn ti-cnet$nrbc aai) poetdfnto cnesoFcoFr$nrbc rtce ucin ovrinatro(iAai) { frah(ti-cnesosa $rbc= $oa){ oec $hs>ovrin s aai > rmn i (aai < $nrbc { f $rbc = iAai) rtr [aai,$oa] eun $rbc rmn; } } } }
  • 17. ... start from obvious things casRmnovreTs etnsPPntFaeokTsCs ls oaCnetret xed HUi_rmwr_etae { pbi fnto tsEpy) ulc ucin etmt( { $hs>setqas',(e Rmnovre)>ovr(') ti-asrEul(' nw oaCnetr-cnet'); } }
  • 18. ... provide some meat ... casRmnovreTs etnsPPntFaeokTsCs ls oaCnetret xed HUi_rmwr_etae { /*@aarvdrpoieetaa* * dtPoie rvdTsDt / pbi fnto tsCnesos$rbc $oa) ulc ucin etovrin(aai, rmn { $ovre =nwRmnovre(; cnetr e oaCnetr) $hs>setqas$oa,$ovre-cnet$rbc) ti-asrEul(rmn cnetr>ovr(aai); } } pbi fnto poieetaa) ulc ucin rvdTsDt( { rtr ary eun ra[ [47 'MCXVI] 39, MMDCI', [,'',[,'I] [,'I] [,'X] [0 'L] 1 I] 2 I', 6 V', 9 I', 4, X', [5 'L',[0 'C] [0,'',[0,'D] 4, XV] 9, X', 10 C] 40 C' ] ; }
  • 19. ... test edge cases casRmnovreTs etnsPPntFaeokTsCs ls oaCnetret xed HUi_rmwr_etae { /*@xetdxeto naiAgmnEcpin* * epceEcpin Ivldruetxeto / pbi fnto tsEpy) ulc ucin etmt( { (e Rmnovre)>ovr(wfIa psighr') nw oaCnetr-cnet't m asn ee); } }
  • 20. Help yourself with tests generation ppntsegn-ts Rmnovre hui-kle -et oaCnetr
  • 21. ... or using Symfony's XSolveUnitSkelgenBundle .apcnoexov:kle:etAm/xmlBnl/evc/ /p/osl slesegnts ceEapeudeSrie. .apcnoexov:kle:etAm//otolr* /p/osl slesegnts ce*Cnrle/
  • 22. We are building too complicated system for an automated tests
  • 23. TDD forces you to write cleaner code
  • 24. It's easier to test smaller units of code
  • 25. so you write smaller classes which are less coupled and that makes your system more stable and open for an extension in the future
  • 26. Your Object Oriented design becomes S.O.L.I.D. compilant and this is awesome!
  • 27. You will of course tackle costly dependencies
  • 29. Fake it till you make it casMceTs etnsPPntFaeokTsCs ls okdet xed HUi_rmwr_etae { pbi fnto tsNnxsigauOjcs) ulc ucin etoEitnVlebet( { $ofgrto =okr:mc(ofgrtoVlebet,[ cniuain Mcey:ok'CniuainauOjc' 'eUl = 'tp/tdsnpap' gtr' > ht:/d.zak.l, 'eFra'= 'm') gtomt > xl]; $hs>setqas'm' $ofgrto-gtomt);/ O ti-asrEul(xl, cniuain>eFra() / K } }
  • 30. Mock things that can't be tested quickly or nonreproducable casMceTs etnsPPntFaeokTsCs ls okdet xed HUi_rmwr_etae { pbi fnto tsAial( ulc ucin etpCls) { $uzok okr:mc(uzBosr) bzMc= Mcey:ok'Bzrwe'; $uzok>hudeev(gt) bzMc-solRcie'e' -adeun'rsos>mo>wsmt<mo>/epne'; >nRtr(<epne<odAeoiy/od<rsos>) / ti as i ws slto,t wiexlfxue i fl / hs lo s ie ouin o rt m itrs n ie / $uzok>hudeev(gt)>ne) / bzMc-solRcie'e'-oc( / / -adeunfl_e_otns_DR_.'fxue.m') >nRtr(iegtcnet(_I_ /itrsxl); } } $p =nwpCnue(bzMc) ai e Aiosmr$uzok; $p-asrEul(Aeoiy,$p-gturnMo() / O ai>setqas'wsmt' ai>eCretod); / K
  • 31. Expect declared behaviors casMceTs etnsPPntFaeokTsCs ls okdet xed HUi_rmwr_etae { pbi fnto tsEpcainDcaain( ulc ucin etxettoselrtos) { $uzok okr:mc(uzBosr) bzMc= Mcey:ok'Bzrwe'; $uzok>hudeev(gt) bzMc-solRcie'e' -adeun'rsos>mo>wsmt<mo>/epne'; >nRtr(<epne<odAeoiy/od<rsos>) $ogrok=okr:mc(oooogr) lgeMc Mcey:ok'MnlgLge'; / w js wn t b sr ta Lge:if wscle ol oc / e ut at o e ue ht ogr:no a ald ny ne $ogrok>hudeev(if'-oc(; lgeMc-solRcie'no)>ne) } } $p =nwpCnue(bzMc,$ogrok; ai e Aiosmr$uzok lgeMc) $p-asrEul(Aeoiy,$p-gturnMo() / O ai>setqas'wsmt' ai>eCretod); / K
  • 32. Be prepared for failures and check if you prepared for unexpected situations casMceTs etnsPPntFaeokTsCs ls okdet xed HUi_rmwr_etae { /*@xetdxeto yxetoWapr* * epceEcpin MEcpinrpe / pbi fnto tsFieCneto( ulc ucin etaldoncin) { $uzok okr:mc(uzBosr) bzMc= Mcey:ok'Bzrwe'; $uzok>hudeev(gt) bzMc-solRcie'e' -adho(uzEcpinCinEcpin) >nTrw'Bzxetoletxeto'; $ogrok=okr:mc(oooogr) lgeMc Mcey:ok'MnlgLge'; $ogrok>hudeev(if'-nvr) lgeMc-solRcie'no)>ee(; $ogrok>hudeev(er)>ne) lgeMc-solRcie'r'-oc(; } } (e pCnue(bzMc,$ogrok)>eCretod) nw Aiosmr$uzok lgeMc)-gturnMo(;
  • 33. We providing API for external consumers it can't be tested...
  • 34. Use Symfony's WebTestCase to test your API I call it integration tests
  • 35. Call your API and check if it returns prepared data casEpniueotolretetnsWbetae ls xedtrCnrleTs xed eTsCs { ueIoaeTssri;/ i rst ts evrnet s sltdetTat / t ees et niomn pbi fnto tsGtitnsn) ulc ucin eteLsIJo( { $let=sai:cetCin(; cin ttc:raelet) $let>eus(GT,'epniue.sn) cin-rqet'E' /xedtrsjo'; $sn=jo_eoe$let>eRsos(-gtotn() jo sndcd(cin-gtepne)>eCnet); } } $hs>setre$let>eRsos(-iScesu() ti-asrTu(cin-gtepne)>sucsfl); $hs>seton(0 $sn; ti-asrCut8, jo) $hs>setraeTaOEul ti-asrGetrhnrqa( nwaeie$sn7]>rae_t, e DtTm(jo[9-cetda) nwaeie$sn0-cetda); e DtTm(jo[]>rae_t)
  • 36. Use fixtures and reset environment IsolatedTestsTrait should do the trick
  • 37. Steps required to effectively run in isolation 1. 2. 3. 4. 5. 6. 7. configure PDO SQLite in file create database drop schema load fixtres copy database as a backup copy database from backup for every test delete database backup after test suite
  • 40. Describe features as scenarios It will be readable for: business, developers and machines
  • 41. Perfectly fits into Agile process
  • 42. Simple example Faue lo frajb etr: ok o o I odrt fn co jb n re o id ol o A a aprn porme s n siig rgamr Ine t b al t ls jbofr ed o e be o it o fes Seai:ls ofr frP vrin cnro it fes o L eso GvnIa o "" ie m n / Te Isol se"oązd tau hn hud e Dłc o em" Adcik"oązd tau n lc Dłc o em" Te Isol b o "krea hn hud e n /air" AdIsol se"H Sno Dvlpr(lwc) n hud e PP eir eeoe Giie" Seai:n ofr frE st cnro o fes o N ie GvnIa o "e" ie m n /n Te Isol ntse"oązd tau hn hud o e Dłc o em" AdIsol ntsea "ji-s eeet n hud o e n #onu" lmn
  • 44. This could be true ...
  • 45. Always use in-memory sqlite database Or create clean sqlite database and copy it for every test
  • 46. Group your tests Use PHPUnit's @group or Behat's @tag
  • 47. Create "smoke tests" groups Those should be fast test, which ensures your system is most likely stable
  • 48. Slower tests should run during night
  • 49. Facts
  • 50. It gives you confidence about changes and your code
  • 51. Team is able to rapidly experiment with code
  • 52. TDD enforces better Object Oriented design Smaller units of code and lower coupling always leads to better understanding of the codebase by future devs
  • 53. End user experiences better quality lower 500 error ratio in the production
  • 56. Setting up an working environment for automated tests is timely costly at the begining, but it pays off in the future
  • 57. System without any kind of automated tests has big potential to be a big ball of mud
  • 58. Good coverage can be easily achieved with mix of unit, functional and behavioral tests
  • 59. You need to build and cultivate TDD culture in your surrounding
  • 60. Thank you so much for attending! Feedback is much appreciated wojciech@sznapka.pl Twitter: @sznapka GitHub: @wowo