Test DoublesRubén Bernárdez www.rubenbp.com   @rubenbpv    #aos2011
¿Por qué usamos dobles?El colaborador:class Communicator():    def send_email(self, address, total_savings):        supone...
Pruébalo cobarde! … pecador!...class Communicator():    def send_email(self, address, total_savings):        suponemos_que...
¿Mock o Spy?class LoFlipoEnEl_AOS(unittest.TestCase):    def test_al_menos_es_enviado_a_la_central(self):        communica...
¿Qué test se romperá?class Communicator():    def send_email(self, address, total_savings):        suponemos_que_esto_envi...
Un diseño distinto...                 ¿Ahora qué hacemos?class Communicator():    def send_email(self, address, total_savi...
Mejor Stub que Spy esta vez! fistro!class SavingsService():    def __init__(self, communicator, repository):        self.c...
Tests frágiles VS legibilidad    Classic VS Mockist
Test doubles frameworks   ¿Qué más comportamientos se pueden    programar en los dobles de test? Con qué    frameworks?  ...
pyDoubles.org        Made with TDD and open source!   when(obj.method).with_args(1).then_return(1000)   when(obj.method)...
iExpertos.com   Formación para equipos de desarrollo:       Iniciación a TDD y TDD Avanzado       eXtreme Programming a...
Upcoming SlideShare
Loading in …5
×

Test Doubles - stubs, spies & mocks

1,398 views

Published on

Slides de la sesión sobre stub, spies & mocks que tuvo lugar en el AOS 2011

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,398
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
3
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Test Doubles - stubs, spies & mocks

  1. 1. Test DoublesRubén Bernárdez www.rubenbp.com @rubenbpv #aos2011
  2. 2. ¿Por qué usamos dobles?El colaborador:class Communicator(): def send_email(self, address, total_savings): suponemos_que_esto_envia_un_email = TrueEl SUT (System Under Test):class SavingsService(): def __init__(self, communicator): self.communicator = communicator def analyze_month(self, month_savings): calculation = month_savings[0] + month_savings[1] self.communicator.send_email(savings@iexpertos.com, calculation)
  3. 3. Pruébalo cobarde! … pecador!...class Communicator(): def send_email(self, address, total_savings): suponemos_que_esto_envia_un_email = Trueclass SavingsService(): def __init__(self, communicator): self.communicator = communicator def analyze_month(self, month_savings): calculation = month_savings[0] + month_savings[1] self.communicator.send_email(savings@iexpertos.com, calculation)class LoFlipoEnEl_AOS(unittest.TestCase): def test_report_correcto_es_enviado_a_la_central(self): communicator = spy(Communicator()) month_savings = [1234,1276435] service = SavingsService(communicator) service.analyze_month(month_savings) assert_that(communicator.send_email).was_called()
  4. 4. ¿Mock o Spy?class LoFlipoEnEl_AOS(unittest.TestCase): def test_al_menos_es_enviado_a_la_central(self): communicator = spy(Communicator()) month_savings = [1234,1276435] service = SavingsService(communicator) service.analyze_month(month_savings) assert_that(communicator.send_email).was_called() def test_se_envia_a_la_central_y_nada_mais(self): communicator = mock(Communicator()) expect_call(communicator.send_email) month_savings = [1234,1276435] service = SavingsService(communicator) service.analyze_month(month_savings) communicator.assert_that_is_satisfied()
  5. 5. ¿Qué test se romperá?class Communicator(): def send_email(self, address, total_savings): suponemos_que_esto_envia_un_email = True def post_savings(self, total_savings): suponemos_que_esto_llama_a_un_webservice = Trueclass SavingsService(): def __init__(self, communicator): self.communicator = communicator def analyze_month(self, month_savings): calculation = month_savings[0] + month_savings[1] self.communicator.send_email(savings@iexpertos.com, calculation) self.communicator.post_savings(calculation)
  6. 6. Un diseño distinto... ¿Ahora qué hacemos?class Communicator(): def send_email(self, address, total_savings): suponemos_que_esto_envia_un_email = Trueclass AccountRepository(): def month_savings(self): suponemos_que_esto_devuelve_de_bd_los_ahorros = Trueclass SavingsService(): def __init__(self, communicator, repository): self.communicator = communicator self.repository = repository def analyze_month(self): month_savings = self.repository.month_savings() calculation = month_savings[0] + month_savings[1] self.communicator.send_email(savings@iexpertos.com, calculation)
  7. 7. Mejor Stub que Spy esta vez! fistro!class SavingsService(): def __init__(self, communicator, repository): self.communicator = communicator self.repository = repository def analyze_month(self): month_savings = self.repository.month_savings() calculation = month_savings[0] + month_savings[1] self.communicator.send_email(savings@iexpertos.com, calculation)class LoFlipoEnEl_AOS(unittest.TestCase): def test_si_hay_report_envialo_a_la_central(self): communicator = spy(Communicator()) repository = stub(AccountRepository()) when(repository.month_savings).then_return([1234,1276435]) service = SavingsService(communicator, repository) service.analyze_month(month_savings) assert_that(communicator.send_email).was_called()
  8. 8. Tests frágiles VS legibilidad Classic VS Mockist
  9. 9. Test doubles frameworks ¿Qué más comportamientos se pueden programar en los dobles de test? Con qué frameworks?  pyDoubles  Mockito  Jmock  Moq  Rhino.Mocks  PHPUnit  GoogleMock
  10. 10. pyDoubles.org Made with TDD and open source! when(obj.method).with_args(1).then_return(1000) when(obj.method).then_raise(ApplicationException()) when(obj.method).then_return(1000) expect_call(obj.method).with_args(1) expect_call(obj.method).with_args(1, ANY_ARG) assert_that(obj.method).was_called().with_args(1) when(obj.method).with_args( str_containing(”abc”)).then_return(1) Powered by...
  11. 11. iExpertos.com Formación para equipos de desarrollo:  Iniciación a TDD y TDD Avanzado  eXtreme Programming a fondo (XP)  Coaching Desarrollamos contigo:  Dos iExpertos en tu empresa desde el arranque del proyecto.  Formación y desarrollo, todo en uno.

×