SlideShare una empresa de Scribd logo
DeSymfonyDay 2014 - To mock or not to mock - Spanish
DeSymfonyDay 2014 - To mock or not to mock - Spanish
¿Quiénes somos?
Eloi Poch Jordi Llonch
Agenda
• ¿Qué hacemos en Akamon?
• To test or not to test?
• ¿Cómo hacemos test?
• Librerías de mock.
• Conclusiones y consejos.
• Osiris:
• Nueva plataforma de servicios de Akamon.
• Principios:
• Simplicidad sobre facilidad.
¿Qué hacemos?
Calidad
Funcionalidad Velocidad
Domain
Contract
Infrastructure
Domain
(Business Logic)
Contract
Infrastructure
Domain
(Business Logic)
Applications
Background
Website
APIsAPIsAPIs
Arquitectura
by Akamon
APIs
DBs
Contract
External Services
Infrastructure
Domain
(Business Logic)
Calidad
• Pull Request
• Pair Programming
• Test
by Akamon
To test or not to test?
Todo
el m
undo
hace
tests
To test or not to test
Los buenos test ahorran DINERO
en cualquier aplicación
To test or not to test
Testear es controvertido
• David Heinemeier (creador de Ruby on Rails y
Fundador y CTO de Basecamp)
• TDD is dead. Long live testing & RailsConf 2014
Keynote - Writing Software by DHH
• Hangouts entre Kent Beck, DHH y M. Fowler
El caso DHH
¿Cómo hacemos test?
Akamon
Way
Un feedback rápido…
…para el código que estamos
escribiendo…
…de una funcionalidad.
¿Qué queremos de los
tests?
Testean de manera rápida
una funcionalidad.
¿Qué hacen nuestros tests?
Infrastructure
External Services
Test Double
Test Double
APIs
DBs
Contract
Domain
(Business Logic)
Test doubles
• Implementación alternativa de un colaborador
(clase o servicio)
• Objetivo:
• Hacer nuestros tests rápidos.
• Aislar sistemas externos.
• Testear casos extremos.
Test doubles
• Tipos:
• Dummy: Implementación no funcional.
• Fake: Implementación completamente funcional pero no
usable en un entorno real.
• Stub: Implementación parcialmente funcional preparada
únicamente para poder ser usada en el test.
• Spy: Stub con memoria.
• Mock: Spy con expectaciones sobre las llamadas a
recibir que las auto-comprueba él mismo.
Tests Unitarios
Pros Contras
Rápidos
No pueden probar la ausencia
de errores
Permiten testear todos los
casos
Los doubles no son confiables
Ayudan a encontrar problemas
pronto
Los doubles están acoplados a
sus implementaciones
Facilitan el cambio
Buena documentación
Escuelas de Tests Unitarios
Chicago / Classical London / Mockist
Énfasis Algoritmo Envío de mensajes
Enfoque
Resultado, estado y
efectos colaterales
Roles,
responsabilidades e
interacciones
Libro
Test-Driven
Development by
Example
Growing Object
Oriented Software
Guided by Tests
Unit Testing :: Chicago School
• Pros:
• Menor riesgo de expectaciones incorrectas.
• Tests estables.
• Menos mantenimiento.
• Contras:
• Ciclo largo de Red-Green-Refactor.
• Explosión combinatoria de los casos a testear.
• Un simple bug rompe muchos tests.
• Más debugging para encontrar la raíz del error.
Unit Testing :: London School
• Pros:
• Facilita el enfoque.
• Baby steps.
• Contras:
• Alto riesgo de expectaciones incorrectas (el refactoring no
detecta cambios en los colaboradores)
• Acople del test a la implementación.
• Test frágiles.
Glosario
• Test Unitarios:!
• Test del código y la lógica de negocio de una
funcionalidad (operaciones con el contrato del
módulo).
• Test double: La infraestructura y los servicios externos.
• Riesgo:
• Test demasiado profundo que no permita testear de
manera simple todas las casuísticas
by Akamon
…pero los test unitarios
no son suficientes…
Queremos saber si las
integraciones con los sistemas
externos funcionan correctamente.
Infrastructure
External Services
APIs
DBs
Contract
Domain
(Business Logic)
Tests de Integración
Pros Contras
Confiables Lentos
Encontrar problemas/cambios
con servicios de terceros
Imposible de testear algunos
casos extremos
Buena documentación
• Test de integración:!
• Test de las implementaciones de los contratos/
interfaces de la infraestructura y los servicios
externos.
• Test double: Nada.
by Akamon
Glosario
…pero con test unitario e
integración todavía no es
suficiente…
Applications
APIs
Background
Website
APIsAPIs
?
Domain
Module
User
Module
Analytics
Module
Marketing
Module
Device
Module
Economy
Module
Game
Module
Game
Event
Tests de Aceptación
Pros Contras
Amigables y entendibles para
usuarios no técnicos
Lentos
Gran confianza
Complicado de testear algunos
casos
Buena documentación
Imposible testear algunos casos
extremos
Complicado localizar errores
Complicados de escribir
(estado inicial)
Glosario
• Test de aceptación:!
• Test de una funcionalidad (end-to-end black-box
mode).
• Test double: Nada.
by Akamon
Nuestra pirámide de tests
Acceptance
Integration
Unit
Librerías mock
https://github.com/Akamon/to-mock-or-not-to-mock
PHPUnit_MockObject
/** @test */!
public function it_should_publish_a_message()!
{!
$body = 'my message';!
$message = new Message($body);!
$serializedMessage = ['body' => $body];!
!
$serializer = $this->getMock(SerializerInterface::class, ['serialize']);!
$serializer!
->expects($this->once())!
->method('serialize')!
->with($this->identicalTo($message))!
->will($this->returnValue($serializedMessage));!
!
$sender = $this->getMock(SenderInterface::class, ['send']);!
$sender!
->expects($this->once())!
->method('send')!
->with($this->equalTo($serializedMessage))!
->will($this->returnValue(true));!
!
$publisher = new Publisher($serializer, $sender);!
$this->assertTrue($publisher->send($message));!
}
Mockery
/** @test */!
public function it_should_publish_a_message()!
{!
$body = 'my message';!
$message = new Message($body);!
$serializedMessage = ['body' => $body];!
!
$serializer = Mockery::mock(SerializerInterface::class);!
$serializer!
->shouldReceive('serialize')!
->with(Mockery::mustBe($message))!
->once()!
->andReturn($serializedMessage);!
!
$sender = Mockery::mock(SenderInterface::class);!
$sender!
->shouldReceive('send')!
->with($serializedMessage)!
->once()!
->andReturn(true);!
!
$publisher = new Publisher($serializer, $sender);!
$this->assertTrue($publisher->send($message));!
}
Phake
/** @test */!
public function it_should_publish_a_message()!
{!
$body = 'my message';!
$message = new Message($body);!
$serializedMessage = ['body' => $body];!
!
$serializer = Phake::mock(SerializerInterface::class);!
Phake::when($serializer)!
->serialize($message)!
->thenReturn($serializedMessage);!
!
$sender = Phake::mock(SenderInterface::class);!
Phake::when($sender)!
->send($serializedMessage)!
->thenReturn(true);!
!
$publisher = new Publisher($serializer, $sender);!
$this->assertTrue($publisher->send($message));!
!
Phake::verify($serializer, Phake::times(1))->serialize($message);!
Phake::verify($sender, Phake::times(1))->send($serializedMessage);!
}
Prophecy/** @var Prophet */!
private $prophet;!
!
protected function setup()!
{!
$this->prophet = new Prophet();!
}!
!
protected function tearDown()!
{!
$this->prophet->checkPredictions();!
}!
!
/** @test */!
public function it_should_publish_a_message()!
{!
$body = 'my message';!
$message = new Message($body);!
$serializedMessage = ['body' => $body];!
!
$serializer = $this->prophet->prophesize(SerializerInterface::class);!
$serializer!
->serialize($message)!
->shouldBeCalled()!
->willReturn($serializedMessage);!
!
$sender = $this->prophet->prophesize(SenderInterface::class);!
$sender!
->send($serializedMessage)!
->shouldBeCalled()!
->willReturn(true);!
!
$publisher = new Publisher($serializer->reveal(), $sender->reveal());!
$this->assertTrue($publisher->send($message));!
}
Comparativa
PHPUnit 4.1 Mockery 0.9 Phake 1.1 Prophecy 1.0 Notas
Invocation Count
Constraint
Ok Muy bueno Muy bueno Ok
Mockery/Phake son mejores.
Métodos atMost() y atLeast()
disponibles.
Ordered Expectations Ok Bueno Ok No
Mockery es mejor. Simplemente
usando ordered([group]).
Argument Matchers Bueno Ok Ok Ok
PHPUnit es mejor. Sobre todo con la
funcionalidad delta
Partial Mocking Ok Muy bueno Ok No
La construcción es mucho más
simple con Mockery.
Mocking Demeter
Chains And Fluent
Interfaces
Ok Muy bueno Ok Ok
Muy sencillo con Mockery y un poco
rebuscado con los otros.
Test Doubles Ok Bueno Ok Bueno
Prophecy es el único con spies y
Mockery el único que gestiona
static, final & private
Otro punto de vista
Property-Based Testing
• Aproximación totalmente diferente.
• No se escriben los tests, se generan.
• QuickCheck: Canonical framework escrito en Haskell.
• ¿Cómo funciona?:
• Describe las entradas y salidas permitidas y las transiciones de estado.
• Genera aleatóriamente un gran número de casos de test y busca fallos.
• Devuelve el mínimo ejemplo de fallo.
• Casos de uso: Comportamientos no determinísticos y sistemas
concurrentes.
Conclusiones
En el mundo del testing lo más importante es
determinar la unidad o sistema a testear.
Los tipos de tests, los doubles e incluso los
frameworks dependen de la unidad o sistema a
testear.
Consejos
• Evita Minimiza los dobles:
• Están acoplados a la implementación ➙ Limitan la
refactorización pura.
• No son confiables ➙ Pueden tener un
comportamiento diferente al real.
by Akamon
Consejos
• Los test unitarios testean código no funcionalidad:
• TDD por si solo no es suficiente.
• BDD por si solo puede ser suficiente (si no te
importa la lentitud).
• TDD + BDD = WIN
by Akamon
Consejos
• Separa la lógica del test de los datos del test:
• Usa el @dataProvider de PHPUnit (no para los
doubles).
• Usa el Scenario Outline de Behat.
by Akamon
• La unidad a testear:
• Mockists are dead. Long live classicists.
• Unit Tests: Isolation
• Mock aren’t stubs: Dummy, Fake, Stub and Mock
• TTDD (Tautological TDD): An anti-pattern
• The depth of tests
• Avoid Testing Implementation Details, Test Behaviours
• The Failures of "Intro to TDD"
• The London School of Test Driven Development
Referencias
• Test doubles:
• Tests doubles
• Mocks, fakes, stubs and dummies
• The little mocker
Referencias
• Contract tests (a.k.a integration tests):
• Integrated tests are scam (Collaboration &
Contract tests)
• Integration contract tests
Referencias
• La pirámide de los tests:
• Test Pyramid
• Testing Pyramid: A case study
• Inverting the testing pyramid
• Testing ice-crean corn anti-pattern
Referencias
Referencias
• Property-Based testing:
• Better than unit tests
• Powerful testing with test.check
• Testing the Hard Stuff and Staying Sane
• Entrevistas a veteranos del TDD:
• Ron Jeffries (One of the founders of Extreme
Programming & Agile Manifesto)
• Steve Freeman (Co-author of Growing Object-
Oriented Software Guided by Tests)
• James Shore (Author of The Art of Agile Development)
• J.B. Rainsberger (Author of JUnit Recipes : Practical
Methods for Programmer Testing
Referencias
¿Preguntas?
¡Gracias!
Imágenes
http://transmedialshakespeare.files.wordpress.com/2011/01/3459513455_d1288a14b9_o.jpeg
http://info.fonality.com/Portals/65551/images/Presentation.jpg
http://www.renders-graphiques.fr/image/upload/normal/organisateur_bloc_notes_agenda-.png
http://www.luxfacta.com/wp-content/uploads/2014/01/programar.jpg
http://www.kinderlandshop.es/WebRoot/StoreES2/Shops/eb0950/5020/E112/E380/66B2/C9CA/AC10/1414/0340/C05_1.jpg
http://www.allpurposeabrasives.com.au/uploads/images/Quality%20Assurrance.jpg
http://news.liv.ac.uk/wp-content/uploads/2013/03/knowledgeHOMEb.jpg
http://www.worldnpa.org/site/wp-content/uploads/2013/01/hand-writing.jpg
http://ievasapple.com/photo/images/my%20point%20of%20view.JPG
http://www.snegmortgageteam.ca/wp-content/uploads/2013/12/mortgage-glossary.jpg
http://birddogrealestate.net/wp-content/uploads/2012/09/Home-Toolbox.jpg
http://www.balticblues-events.com/sites/default/files/images/ideas/photos/32-Live-Cooking1.jpg
http://www.youwall.com/papel/on_my_way_wallpaper_fa2bb.jpg
http://inheritancethefilm.com/wp-content/uploads/2013/06/thankyou.jpg
http://www.ngn.com.au/wp-content/uploads/2013/07/Websites-on-white.png
http://hn-marketing.co.uk/wp-content/uploads/2012/09/shutterstock_12553684.jpg
http://picturesofmoney.org/wp-content/uploads/2013/04/Animated-American-Money-Falling-Into-a-Pile.jpg
http://st.gdefon.com/wallpapers_original/wallpapers/18716_palma_more_plyazh_pesok_oblaka_tropiki_2560x1600_(www.GdeFon.ru).jpg
http://i.imgur.com/DbTGPys.gif
http://media.tumblr.com/e96b538708be71104f21689d3a820b9c/tumblr_inline_n54iazp56x1raprkq.gif
http://i.imgur.com/8Lpsys5.gif

Más contenido relacionado

PDF
Symfony2 Introducción
PDF
DevOps & Infraestructura como código: Promesas Rotas
PPTX
SecDevOps - La seguridad en el desarrollo
PDF
Escalabilidad y alto rendimiento con Symfony2
PDF
Dev ops infraestructura agil con open source
ODP
Ansible para Gestión de la configuración y Automatización
PDF
Infraestructura como código
PDF
Ponele el TURBO al Dev Team de tu Startup
Symfony2 Introducción
DevOps & Infraestructura como código: Promesas Rotas
SecDevOps - La seguridad en el desarrollo
Escalabilidad y alto rendimiento con Symfony2
Dev ops infraestructura agil con open source
Ansible para Gestión de la configuración y Automatización
Infraestructura como código
Ponele el TURBO al Dev Team de tu Startup

La actualidad más candente (20)

PPTX
Introduccion a Ansible
PDF
Gradle vs Maven
PDF
De desarrollo a producción usando docker
PDF
Introducción a NodeJS
PDF
Curso node.js
PDF
Casper JS - Asegurando la calidad en front-end Drupal
KEY
Introducción a NodeJS
PDF
Introducción a Node.js
PPTX
ASP.NET 5 en Linux y Mac OS X: herramientas e integración
PDF
Jenkins, no me rompas los builds!
ODP
Gestionando tu infraestructura con Ansible
PDF
Capistrano drupalcamp-jerez-2015
PDF
Rendimiento extremo en php
PDF
Introducción a Node.js
PPTX
Automatizacion de proyectos con gradle
PDF
El uso correcto de MySQLi
PDF
Tests de integración automatizados con Docker y Bamboo
PPTX
Introducción Nodejs
PPTX
Desarrollo modermo de aplicaciones web
PDF
An evening with... Liquidbase
Introduccion a Ansible
Gradle vs Maven
De desarrollo a producción usando docker
Introducción a NodeJS
Curso node.js
Casper JS - Asegurando la calidad en front-end Drupal
Introducción a NodeJS
Introducción a Node.js
ASP.NET 5 en Linux y Mac OS X: herramientas e integración
Jenkins, no me rompas los builds!
Gestionando tu infraestructura con Ansible
Capistrano drupalcamp-jerez-2015
Rendimiento extremo en php
Introducción a Node.js
Automatizacion de proyectos con gradle
El uso correcto de MySQLi
Tests de integración automatizados con Docker y Bamboo
Introducción Nodejs
Desarrollo modermo de aplicaciones web
An evening with... Liquidbase
Publicidad

Similar a DeSymfonyDay 2014 - To mock or not to mock - Spanish (20)

PDF
To mock or not to mock
PPTX
Unit Testing with Mock Objects
PDF
Introducción a testing en php
PPTX
Pruebas Automatizadas
PPTX
20180313 Keep Calm And Test Your Code RiojaDotNet
PPT
Test unitarios
PDF
Test Driven Development
PPTX
Testing & Pizza by Lito & nitsnets
PDF
Pruebas unitarias
PPTX
Pruebas automaticas
ODP
Testing Ruby on Rails (spanish)
PDF
presentacion de programacion Software testing.pdf
PPTX
Codemotion 2015 - Unit Testing
PPTX
Test Automation .NET
PDF
"Demystifying development techniques" por @eturino
PDF
Desarrollo con Java y metodologías agiles
PPTX
pruebasunitarias-110921232512-phpapp02.pptx
PDF
Testing, tipos y otros flamewars
PDF
Más allá del testing
To mock or not to mock
Unit Testing with Mock Objects
Introducción a testing en php
Pruebas Automatizadas
20180313 Keep Calm And Test Your Code RiojaDotNet
Test unitarios
Test Driven Development
Testing & Pizza by Lito & nitsnets
Pruebas unitarias
Pruebas automaticas
Testing Ruby on Rails (spanish)
presentacion de programacion Software testing.pdf
Codemotion 2015 - Unit Testing
Test Automation .NET
"Demystifying development techniques" por @eturino
Desarrollo con Java y metodologías agiles
pruebasunitarias-110921232512-phpapp02.pptx
Testing, tipos y otros flamewars
Más allá del testing
Publicidad

Último (20)

PPTX
El uso de las TIC en la vida cotidiana..
DOCX
Zarate Quispe Alex aldayir aplicaciones de internet .docx
PDF
Tips de Seguridad para evitar clonar sus claves del portal bancario.pdf
PDF
Influencia-del-uso-de-redes-sociales.pdf
PDF
Ronmy José Cañas Zambrano - Potenciando la tecnología en Venezuela.pdf
PDF
ADMINISTRACIÓN DE ARCHIVOS - TICS (SENA).pdf
PPTX
COMO AYUDAN LAS TIC EN LA EDUCACION SUPERIOR.pptx
PDF
MANUAL de recursos humanos para ODOO.pdf
PDF
MANUAL TECNOLOGÍA SER MINISTERIO EDUCACIÓN
DOCX
Contenido Fundamentos de comunicaciones Fibra Optica (1).docx
PDF
Maste clas de estructura metálica y arquitectura
PPTX
sa-cs-82-powerpoint-hardware-y-software_ver_4.pptx
PPTX
Acronis Cyber Protect Cloud para Ciber Proteccion y Ciber Seguridad LATAM - A...
PPTX
Presentación de Redes de Datos modelo osi
PDF
PRESENTACIÓN GENERAL MIPIG - MODELO INTEGRADO DE PLANEACIÓN
PPT
introduccion a las_web en el 2025_mejoras.ppt
PPTX
Sesion 1 de microsoft power point - Clase 1
PDF
programa-de-estudios-2011-guc3ada-para-el-maestro-secundarias-tecnicas-tecnol...
PDF
clase auditoria informatica 2025.........
PDF
Diapositiva proyecto de vida, materia catedra
El uso de las TIC en la vida cotidiana..
Zarate Quispe Alex aldayir aplicaciones de internet .docx
Tips de Seguridad para evitar clonar sus claves del portal bancario.pdf
Influencia-del-uso-de-redes-sociales.pdf
Ronmy José Cañas Zambrano - Potenciando la tecnología en Venezuela.pdf
ADMINISTRACIÓN DE ARCHIVOS - TICS (SENA).pdf
COMO AYUDAN LAS TIC EN LA EDUCACION SUPERIOR.pptx
MANUAL de recursos humanos para ODOO.pdf
MANUAL TECNOLOGÍA SER MINISTERIO EDUCACIÓN
Contenido Fundamentos de comunicaciones Fibra Optica (1).docx
Maste clas de estructura metálica y arquitectura
sa-cs-82-powerpoint-hardware-y-software_ver_4.pptx
Acronis Cyber Protect Cloud para Ciber Proteccion y Ciber Seguridad LATAM - A...
Presentación de Redes de Datos modelo osi
PRESENTACIÓN GENERAL MIPIG - MODELO INTEGRADO DE PLANEACIÓN
introduccion a las_web en el 2025_mejoras.ppt
Sesion 1 de microsoft power point - Clase 1
programa-de-estudios-2011-guc3ada-para-el-maestro-secundarias-tecnicas-tecnol...
clase auditoria informatica 2025.........
Diapositiva proyecto de vida, materia catedra

DeSymfonyDay 2014 - To mock or not to mock - Spanish