69
Saturday, June 16, 12

Código Fácil De Testear

Embed Size (px)

DESCRIPTION

Charla dada durante la conferencia deSymfony en Castellón, España.http://www.desymfony.com

Citation preview

Page 1: Código Fácil De Testear

Saturday, June 16, 12

Page 2: Código Fácil De Testear

Saturday, June 16, 12

Page 3: Código Fácil De Testear

Acerca de

• Programador en Liip AG | Zürich

• Blog: http://videlalvaro.github.com/

• Twitter: @old_sound

Saturday, June 16, 12

Page 4: Código Fácil De Testear

Hecho en Uruguay

Saturday, June 16, 12

Page 5: Código Fácil De Testear

Acerca de

Co Autor de

ConillMQ en acció

http://bit.ly/rabbitmq

Saturday, June 16, 12

Page 6: Código Fácil De Testear

Acerca de

Co Autor de

RabbitMQ in Action

http://bit.ly/rabbitmq

Saturday, June 16, 12

Page 7: Código Fácil De Testear

No Soy

Saturday, June 16, 12

Page 8: Código Fácil De Testear

No Soy

• Gurú del testeo de aplicaciones

Saturday, June 16, 12

Page 9: Código Fácil De Testear

No Soy

• Guru del testeo de aplicaciones

• Abogado del TDD

Saturday, June 16, 12

Page 10: Código Fácil De Testear

Unit Testing

The goal of unit testing is to isolate each part of the program and show

that the individual parts are correct.

http://en.wikipedia.org/wiki/Unit_testing

Saturday, June 16, 12

Page 11: Código Fácil De Testear

Unit Tests

http://es.wikipedia.org/wiki/Prueba_unitaria

Por definición, sólo prueban las unidades por sí solas. Por lo tanto, no descubrirán

errores de integración, problemas de rendimiento y otros problemas que afectan a

todo el sistema en su conjunto.

Saturday, June 16, 12

Page 12: Código Fácil De Testear

Dogma vs.

Realidad

Saturday, June 16, 12

Page 13: Código Fácil De Testear

Trade Offs

Saturday, June 16, 12

Page 14: Código Fácil De Testear

¿Qué testear?

Saturday, June 16, 12

Page 15: Código Fácil De Testear

¿Cuánto testear?

Saturday, June 16, 12

Page 16: Código Fácil De Testear

http://stackoverflow.com/questions/153234/how-deep-are-your-unit-tests/153565#153565

"I get paid for code that works, not for tests, so my philosophy is to test as little as possible to reach a given

level of confidence" – Kent Beck

Saturday, June 16, 12

Page 17: Código Fácil De Testear

El Secreto del TDD

Saturday, June 16, 12

Page 18: Código Fácil De Testear

El Secreto del TDD

Saturday, June 16, 12

Page 19: Código Fácil De Testear

El Secreto del TDD

Saturday, June 16, 12

Page 20: Código Fácil De Testear

Algunos Libros de Kent Beck

Saturday, June 16, 12

Page 21: Código Fácil De Testear

Para escribir buenos Tests primero hay que

saber programar

Saturday, June 16, 12

Page 22: Código Fácil De Testear

Saturday, June 16, 12

Page 23: Código Fácil De Testear

Los Programadores somos como los

usuarios de los que tanto nos quejamos

Saturday, June 16, 12

Page 24: Código Fácil De Testear

El diseño se madura y macera con el tiempo

Saturday, June 16, 12

Page 25: Código Fácil De Testear

TIPS

Saturday, June 16, 12

Page 26: Código Fácil De Testear

Separar el código puro del impuro o stateful

Saturday, June 16, 12

Page 27: Código Fácil De Testear

Funciones Puras

Saturday, June 16, 12

Page 28: Código Fácil De Testear

Funciones Puras

• Devuelven datos en bases a los parámetros que aceptan

Saturday, June 16, 12

Page 29: Código Fácil De Testear

Funciones Puras

• Devuelven datos en bases a los parámetros que aceptan

• No modifican estado externo

Saturday, June 16, 12

Page 30: Código Fácil De Testear

Funciones Puras

• Devuelven datos en bases a los parámetros que aceptan

• No modifican estado externo

• No producen efectos secundarios

Saturday, June 16, 12

Page 31: Código Fácil De Testear

http://dl.dropbox.com/u/7810909/docs/what-does-fp-mean/what-does-fp-mean/chunk-html/ar01s05.html

if($player->getScore() > 0) { $player->setSwizzle(7);} else { $player->setSwizzle( $player->getSwizzle() + 1 );}

¿Qué tiene de malo este código?

Saturday, June 16, 12

Page 32: Código Fácil De Testear

http://dl.dropbox.com/u/7810909/docs/what-does-fp-mean/what-does-fp-mean/chunk-html/ar01s05.html

$newScore = $player->getScore() > 0 ? 7 : $player->getSwizzle() + 1;

$player->setSwizzle($newScore);

Version que no mezcla modificaciones de estado

Saturday, June 16, 12

Page 33: Código Fácil De Testear

El cálculo del Score se puede extraer a una

función

Saturday, June 16, 12

Page 34: Código Fácil De Testear

El cálculo del Score se puede testear

Saturday, June 16, 12

Page 35: Código Fácil De Testear

Primero Escribir Código Puro

Saturday, June 16, 12

Page 36: Código Fácil De Testear

Agregar paso a paso el codigo

impuro necesario

Saturday, June 16, 12

Page 37: Código Fácil De Testear

Código Fácil de Componer

Saturday, June 16, 12

Page 38: Código Fácil De Testear

http://en.wikipedia.org/wiki/Function_(mathematics)

Function Composition

Saturday, June 16, 12

Page 39: Código Fácil De Testear

http://en.wikipedia.org/wiki/Function_(mathematics)

Function Composition

Saturday, June 16, 12

Page 40: Código Fácil De Testear

Esto suena familiar

Saturday, June 16, 12

Page 41: Código Fácil De Testear

“Many UNIX programs do quite trivial tasks in

isolation, but, combined with other programs,

become general and useful tools.”

http://math.albany.edu/math/pers/hammond/unixphil.html

Saturday, June 16, 12

Page 42: Código Fácil De Testear

http://www.commandlinefu.com/commands/view/1767/number-of-open-connections-per-ip.

netstat -ntu | awk '{print $5}' | \cut -d: -f1 | sort | uniq -c | sort -n

Number of open connections per IP

Saturday, June 16, 12

Page 43: Código Fácil De Testear

¿Por qué no programar código de esa forma?

Saturday, June 16, 12

Page 44: Código Fácil De Testear

Esto suena familiar

Saturday, June 16, 12

Page 45: Código Fácil De Testear

¡Bienvenido a la Programación

Funcional!

Saturday, June 16, 12

Page 46: Código Fácil De Testear

“Writing unit tests is reinventing functional

programming in non-functional

languages”http://noss.github.com/2009/02/25/writing-unit-tests-is-reinventing-functional-programming-in-non-functional-languages.html

Saturday, June 16, 12

Page 47: Código Fácil De Testear

¿Qué nos enseña la programación

funcional?

Saturday, June 16, 12

Page 48: Código Fácil De Testear

Uso Correcto de Types

Saturday, June 16, 12

Page 49: Código Fácil De Testear

¿Qué significa ‘null’?

Saturday, June 16, 12

Page 50: Código Fácil De Testear

¿Qué significa ‘false|true’?

Saturday, June 16, 12

Page 51: Código Fácil De Testear

Código semánticamente

correcto

Saturday, June 16, 12

Page 52: Código Fácil De Testear

Vamos a hacer la comida

Saturday, June 16, 12

Page 53: Código Fácil De Testear

Vamos a cocinar la comida

Saturday, June 16, 12

Page 54: Código Fácil De Testear

Funciones que cumplen sólo una función

Saturday, June 16, 12

Page 55: Código Fácil De Testear

Separación radical de código

puro del impuro

Saturday, June 16, 12

Page 56: Código Fácil De Testear

Veamos un Ejemplo

http://pastie.org/4097327

Saturday, June 16, 12

Page 57: Código Fácil De Testear

Testeando AMQP

https://github.com/videlalvaro/php-amqplib/

Saturday, June 16, 12

Page 58: Código Fácil De Testear

public  function  queue_declare($queue="",  $passive=false,  $durable=false,        $exclusive=false,  $auto_delete=true,  $nowait=false,    $arguments=NULL,  $ticket=NULL)

{    if($arguments  ==  NULL)        $arguments  =  array();

     $args  =  new  AMQPWriter();      if($ticket  !=  NULL)          $args-­‐>write_short($ticket);      else          $args-­‐>write_short($this-­‐>default_ticket);      $args-­‐>write_shortstr($queue);      $args-­‐>write_bit($passive);      $args-­‐>write_bit($durable);      $args-­‐>write_bit($exclusive);      $args-­‐>write_bit($auto_delete);      $args-­‐>write_bit($nowait);      $args-­‐>write_table($arguments);      $this-­‐>send_method_frame(array(50,  10),  $args);

     if(!$nowait)          return  $this-­‐>wait(array("50,11"));        //  Channel.queue_declare_ok}

Testeando queue_declare

Saturday, June 16, 12

Page 59: Código Fácil De Testear

Testeando queue_declareclass  AMQPChannel  extends  AbstractChannel{    public  function  __construct($connection,  $channel_id=null,  $auto_decode=true)    {        $this-­‐>frameBuilder  =  new  FrameBuilder();

       if  ($channel_id  ==  null)  {            $channel_id  =  $connection-­‐>get_free_channel_id();        }

       parent::__construct($connection,  $channel_id);

       if  ($this-­‐>debug)  {            MiscHelper::debug_msg("using  channel_id:  "  .  $channel_id);        }

       $this-­‐>default_ticket  =  0;        $this-­‐>is_open  =  false;        $this-­‐>active  =  true;  //  Flow  control        $this-­‐>alerts  =  array();        $this-­‐>callbacks  =  array();        $this-­‐>auto_decode  =  $auto_decode;

       $this-­‐>x_open();    }    /*  ...  más  métodos  y  propiedades  omitidos  */}

Saturday, June 16, 12

Page 60: Código Fácil De Testear

Testeando queue_declare

public  function  queueDeclare($queue,  $passive,  $durable,  $exclusive,  $auto_delete,  $nowait,  $arguments,  $ticket)

{    $args  =  new  AMQPWriter();    $args-­‐>write_short($ticket)              -­‐>write_shortstr($queue)              -­‐>write_bit($passive)              -­‐>write_bit($durable)              -­‐>write_bit($exclusive)              -­‐>write_bit($auto_delete)              -­‐>write_bit($nowait)              -­‐>write_table($arguments);    return  $args;}

Saturday, June 16, 12

Page 61: Código Fácil De Testear

Testeando queue_declare

public  function  queue_declare($queue="",  $passive=false,  $durable=false,                                                                  $exclusive=false,  $auto_delete=true,  $nowait=false,                                                              $arguments=NULL,  $ticket=NULL){    $arguments  =  $this-­‐>getArguments($arguments);    $ticket  =  $this-­‐>getTicket($ticket);

   $args  =  $this-­‐>frameBuilder-­‐>queueDeclare(                                              $queue,  $passive,  $durable,  $exclusive,

 $auto_delete,  $nowait,  $arguments,  $ticket);        $this-­‐>send_method_frame(array(50,  10),  $args);

   if  (!$nowait)  {        return  $this-­‐>wait(array("50,11"));        //  Channel.queue_declare_ok                                                              }}

Saturday, June 16, 12

Page 62: Código Fácil De Testear

Testeando queue_declare

public  function  testQueueDeclare(){    $expected  =  "\x00\x00\x03foo\x00\x00\x00\x00\x00";    $args  =  $this-­‐>frameBuilder                              -­‐>queueDeclare(

     'foo',  false,  false,  false,  false,  false,  array(),  0);

   $this-­‐>assertEquals($expected,  $args-­‐>getvalue());}

Saturday, June 16, 12

Page 63: Código Fácil De Testear

¿Cómo aplico esto al mundo real?

Saturday, June 16, 12

Page 64: Código Fácil De Testear

¿Cómo aplico esto al mundo real?

• Ver la charla de Ricard “Servicios en Symfony2”

Saturday, June 16, 12

Page 65: Código Fácil De Testear

¿Cómo aplico esto al mundo real?

• Ver la charla de Ricard “Servicios en Symfony2”

• Muchos servicios especializados y desacoplados

Saturday, June 16, 12

Page 66: Código Fácil De Testear

¿Cómo aplico esto al mundo real?

• Ver la charla de Ricard “Servicios en Symfony2”

• Muchos servicios especializados y desacoplados

• Clases que cumplen sólo una función

Saturday, June 16, 12

Page 67: Código Fácil De Testear

http://www.linfo.org/q_programming.html

“Inside every well-written large

program is a well-written small program”

Saturday, June 16, 12

Page 68: Código Fácil De Testear

¿Preguntas?

Saturday, June 16, 12

Page 69: Código Fácil De Testear

¡Gracias!Álvaro Videla

http://twitter.com/old_sound

http://github.com/videlalvaro

http://www.slideshare.net/old_sound

Saturday, June 16, 12