o cpan tem as ferramentas que você precisa para fazer tdd em perl, o coding dojo tem o resto
DESCRIPTION
Palestra realizada no YAPC::Brasil 2011, em 05/11 no Largo do Machado / Rio de Janeiro.TRANSCRIPT
![Page 1: O CPAN tem as ferramentas que você precisa para fazer TDD em Perl, o Coding Dojo tem o resto](https://reader033.vdocuments.pub/reader033/viewer/2022051209/54966644b47959514d8b4fc6/html5/thumbnails/1.jpg)
Rodolfo Carvalho - @201YAPC::Brasil 2011
![Page 2: O CPAN tem as ferramentas que você precisa para fazer TDD em Perl, o Coding Dojo tem o resto](https://reader033.vdocuments.pub/reader033/viewer/2022051209/54966644b47959514d8b4fc6/html5/thumbnails/2.jpg)
Rodolfo Carvalho
● Eng. Computação UFRJ
● PythOnRio
● Coding Dojo Rio
● Ex-Globo.com, ex-Intelie
● Entusiasta de Racket
● Monge Perl?
Quem sabe em breve :P
![Page 3: O CPAN tem as ferramentas que você precisa para fazer TDD em Perl, o Coding Dojo tem o resto](https://reader033.vdocuments.pub/reader033/viewer/2022051209/54966644b47959514d8b4fc6/html5/thumbnails/3.jpg)
metacpan.org
CPAN: Comprehensive Perl Archive Network
![Page 4: O CPAN tem as ferramentas que você precisa para fazer TDD em Perl, o Coding Dojo tem o resto](https://reader033.vdocuments.pub/reader033/viewer/2022051209/54966644b47959514d8b4fc6/html5/thumbnails/4.jpg)
Desenvolvimento "tradicional"
Design
Code
Test
![Page 5: O CPAN tem as ferramentas que você precisa para fazer TDD em Perl, o Coding Dojo tem o resto](https://reader033.vdocuments.pub/reader033/viewer/2022051209/54966644b47959514d8b4fc6/html5/thumbnails/5.jpg)
TDD: Test-Driven Development
Design
Code
Test
Test
Refactor
![Page 6: O CPAN tem as ferramentas que você precisa para fazer TDD em Perl, o Coding Dojo tem o resto](https://reader033.vdocuments.pub/reader033/viewer/2022051209/54966644b47959514d8b4fc6/html5/thumbnails/6.jpg)
![Page 7: O CPAN tem as ferramentas que você precisa para fazer TDD em Perl, o Coding Dojo tem o resto](https://reader033.vdocuments.pub/reader033/viewer/2022051209/54966644b47959514d8b4fc6/html5/thumbnails/7.jpg)
No longo prazo...
...
tempo
x
![Page 8: O CPAN tem as ferramentas que você precisa para fazer TDD em Perl, o Coding Dojo tem o resto](https://reader033.vdocuments.pub/reader033/viewer/2022051209/54966644b47959514d8b4fc6/html5/thumbnails/8.jpg)
● CPAN contém diversas opções
● TAP: "Test Anything Protocol"
Test::SimpleTest::More + Test::Exception Test::Differences Test::Deep Test::Warn = Test::Most
Em Perl
+ prove
![Page 9: O CPAN tem as ferramentas que você precisa para fazer TDD em Perl, o Coding Dojo tem o resto](https://reader033.vdocuments.pub/reader033/viewer/2022051209/54966644b47959514d8b4fc6/html5/thumbnails/9.jpg)
Exemplo
Problema: calcular o n-ésimo número da sequência de Fibonacci. Design: escrever uma função que recebe um argumento numérico (n) e retorna o número da sequência correspondente.
![Page 10: O CPAN tem as ferramentas que você precisa para fazer TDD em Perl, o Coding Dojo tem o resto](https://reader033.vdocuments.pub/reader033/viewer/2022051209/54966644b47959514d8b4fc6/html5/thumbnails/10.jpg)
Exemplo: Fibonacci
Teste:use Test::More; is( fibonacci(0), 0 ); done_testing();
Resultado:$ prove fibonacci.pl fibonacci.pl .. Undefined subroutine &main::fibonacci called at fibonacci.pl line 4.fibonacci.pl .. Dubious, test returned 9 (wstat 2304, 0x900)
![Page 11: O CPAN tem as ferramentas que você precisa para fazer TDD em Perl, o Coding Dojo tem o resto](https://reader033.vdocuments.pub/reader033/viewer/2022051209/54966644b47959514d8b4fc6/html5/thumbnails/11.jpg)
Exemplo: Fibonacci
Implementação:sub fibonacci {
return 0;}
Resultado:$ prove fibonacci.pl fibonacci.pl .. ok All tests successful.
![Page 12: O CPAN tem as ferramentas que você precisa para fazer TDD em Perl, o Coding Dojo tem o resto](https://reader033.vdocuments.pub/reader033/viewer/2022051209/54966644b47959514d8b4fc6/html5/thumbnails/12.jpg)
Exemplo: Fibonacci
...
![Page 13: O CPAN tem as ferramentas que você precisa para fazer TDD em Perl, o Coding Dojo tem o resto](https://reader033.vdocuments.pub/reader033/viewer/2022051209/54966644b47959514d8b4fc6/html5/thumbnails/13.jpg)
Exemplo: Fibonacci
Teste:is( fibonacci(0), 0 );is( fibonacci(1), 1 );is( fibonacci(2), 1 );
Implementação:sub fibonacci { my $n = shift; return 0 if $n eq 0; return 1 if $n eq 1; return 1 if $n eq 2;}
![Page 14: O CPAN tem as ferramentas que você precisa para fazer TDD em Perl, o Coding Dojo tem o resto](https://reader033.vdocuments.pub/reader033/viewer/2022051209/54966644b47959514d8b4fc6/html5/thumbnails/14.jpg)
Exemplo: Fibonacci
Teste:is( fibonacci(0), 0 );is( fibonacci(1), 1 );is( fibonacci(2), 1 );is( fibonacci(3), 2 );
Resultado:$ prove fibonacci.pl fibonacci.pl .. 1/? # Failed test at fibonacci.pl line 15.# got: ''# expected: '2'# Looks like you failed 1 test of 4.
![Page 15: O CPAN tem as ferramentas que você precisa para fazer TDD em Perl, o Coding Dojo tem o resto](https://reader033.vdocuments.pub/reader033/viewer/2022051209/54966644b47959514d8b4fc6/html5/thumbnails/15.jpg)
Exemplo: Fibonacci
Implementação:sub fibonacci {
my $n = shift;return 0 if $n eq 0;return 1 if $n eq 1;return fibonacci($n-2) +
fibonacci($n-1);}
Resultado:$ prove fibonacci.pl fibonacci.pl .. ok All tests successful.
![Page 16: O CPAN tem as ferramentas que você precisa para fazer TDD em Perl, o Coding Dojo tem o resto](https://reader033.vdocuments.pub/reader033/viewer/2022051209/54966644b47959514d8b4fc6/html5/thumbnails/16.jpg)
Exemplo: Fibonacci
Teste:my $phi = (1+sqrt 5)/2;my $psi = -1/$phi;foreach ( 0..20 ) { is( fibonacci($_), ($phi**$_ - $psi**$_)/ sqrt 5 );}
Resultado:$ prove fibonacci.pl fibonacci.pl .. ok All tests successful.
![Page 17: O CPAN tem as ferramentas que você precisa para fazer TDD em Perl, o Coding Dojo tem o resto](https://reader033.vdocuments.pub/reader033/viewer/2022051209/54966644b47959514d8b4fc6/html5/thumbnails/17.jpg)
oferece muito mais
Perl
![Page 18: O CPAN tem as ferramentas que você precisa para fazer TDD em Perl, o Coding Dojo tem o resto](https://reader033.vdocuments.pub/reader033/viewer/2022051209/54966644b47959514d8b4fc6/html5/thumbnails/18.jpg)
OOP's "xUnit"
● Test::Class
● Test::MockObject
● DBD::Mock
● Test::TCP
package Example::Test;use base qw(Test::Class);use Test::More; # setup methods are run before every test method.sub make_fixture : Test(setup) { my $array = [1, 2]; shift->{test_array} = $array;}; # a test method that runs 1 testsub test_push : Test { my $array = shift->{test_array}; push @$array, 3; is_deeply($array, [1, 2, 3], 'push worked');}; # a test method that runs 4 testssub test_pop : Test(4) { my $array = shift->{test_array}; is(pop @$array, 2, 'pop = 2'); is(pop @$array, 1, 'pop = 1'); is_deeply($array, [], 'array empty'); is(pop @$array, undef, 'pop = undef');}; # teardown methods are run after every test method.sub teardown : Test(teardown) { my $array = shift->{test_array}; diag("array = (@$array) after test(s)");};
![Page 19: O CPAN tem as ferramentas que você precisa para fazer TDD em Perl, o Coding Dojo tem o resto](https://reader033.vdocuments.pub/reader033/viewer/2022051209/54966644b47959514d8b4fc6/html5/thumbnails/19.jpg)
TDD + Web
● Test::WWW::Mechanize
use Test::More tests => 5;use Test::WWW::Mechanize; my $mech = Test::WWW::Mechanize->new;$mech->get_ok( $page );$mech->base_is( 'http://petdance.com/', 'Proper <BASE HREF>' );$mech->title_is( 'Invoice Status', "Make sure we're on the invoice page" );$mech->text_contains( 'Andy Lester', 'My name somewhere' );$mech->content_like( qr/(cpan|perl)\.org/, 'Link to perl.org or CPAN' );
![Page 20: O CPAN tem as ferramentas que você precisa para fazer TDD em Perl, o Coding Dojo tem o resto](https://reader033.vdocuments.pub/reader033/viewer/2022051209/54966644b47959514d8b4fc6/html5/thumbnails/20.jpg)
TDD + Web
● Test::Mojo HTML5 Real-time webapps
use Test::More tests => 10;use Test::Mojo; my $t = Test::Mojo->new('MyApp'); $t->get_ok('/welcome') ->status_is(200) ->content_like(qr/Hello!/, 'welcome message'); $t->post_form_ok('/search', {title => 'Perl', author => 'taro'}) ->status_is(200) ->content_like(qr/Perl.+taro/); $t->delete_ok('/something') ->status_is(200) ->header_is('X-Powered-By' => 'Mojolicious (Perl)') ->header_isnt('X-Bender' => 'Bite my shiny metal ass!'); ->content_is('Hello world!'); $t->websocket_ok('/echo') ->send_message_ok('hello') ->message_is('echo: hello') ->finish_ok;
![Page 21: O CPAN tem as ferramentas que você precisa para fazer TDD em Perl, o Coding Dojo tem o resto](https://reader033.vdocuments.pub/reader033/viewer/2022051209/54966644b47959514d8b4fc6/html5/thumbnails/21.jpg)
Test anything...
● Test::Pod::Coverage
● Test::UseAllModules
● Test::NoWarnings
● Test::Perl::Critic
● Test::AskAnExpert
● Test::Inline
● Test::UniqueTestNames
![Page 22: O CPAN tem as ferramentas que você precisa para fazer TDD em Perl, o Coding Dojo tem o resto](https://reader033.vdocuments.pub/reader033/viewer/2022051209/54966644b47959514d8b4fc6/html5/thumbnails/22.jpg)
Não basta escrever testes
Fácil:● Dada uma entrada, comparar saída e saída esperada
Difícil:● O que testar
● Como testar
● Erros não previstos
![Page 23: O CPAN tem as ferramentas que você precisa para fazer TDD em Perl, o Coding Dojo tem o resto](https://reader033.vdocuments.pub/reader033/viewer/2022051209/54966644b47959514d8b4fc6/html5/thumbnails/23.jpg)
Não basta escrever testesShort list of ways tests could fail without any code change:
● "Temporary" test files and fixtures might be dirty.
● "Temporary" databases and tables might be dirty.
● It is sensitive to time or date.
● It uses network resources and they changed.
● The compiler was changed.
● The installed libraries used were changed.
● The libraries the libraries use were changed.
● The kernel was changed.
● Any servers used (databases, web servers, etc...) were changed.
● It uses parallel processing and a subtle bug only occurs sometimes.
● The disk (or the filesystem where temp files go) is full.
● The disk (or the filesystem where temp files go) is broken.
● Your memory/disk/process/filehandle quotas were reduced.
● The machine has run out of memory.
● The machine has run out of filehandles.
● It uses fixtures with randomly generated data and generated some that tickled a bug.
Michael Schwern
![Page 24: O CPAN tem as ferramentas que você precisa para fazer TDD em Perl, o Coding Dojo tem o resto](https://reader033.vdocuments.pub/reader033/viewer/2022051209/54966644b47959514d8b4fc6/html5/thumbnails/24.jpg)
metacpan.org
![Page 25: O CPAN tem as ferramentas que você precisa para fazer TDD em Perl, o Coding Dojo tem o resto](https://reader033.vdocuments.pub/reader033/viewer/2022051209/54966644b47959514d8b4fc6/html5/thumbnails/25.jpg)
![Page 26: O CPAN tem as ferramentas que você precisa para fazer TDD em Perl, o Coding Dojo tem o resto](https://reader033.vdocuments.pub/reader033/viewer/2022051209/54966644b47959514d8b4fc6/html5/thumbnails/26.jpg)
Quero fazer TDD, e agora?
![Page 27: O CPAN tem as ferramentas que você precisa para fazer TDD em Perl, o Coding Dojo tem o resto](https://reader033.vdocuments.pub/reader033/viewer/2022051209/54966644b47959514d8b4fc6/html5/thumbnails/27.jpg)
Coding Dojo
![Page 28: O CPAN tem as ferramentas que você precisa para fazer TDD em Perl, o Coding Dojo tem o resto](https://reader033.vdocuments.pub/reader033/viewer/2022051209/54966644b47959514d8b4fc6/html5/thumbnails/28.jpg)
![Page 29: O CPAN tem as ferramentas que você precisa para fazer TDD em Perl, o Coding Dojo tem o resto](https://reader033.vdocuments.pub/reader033/viewer/2022051209/54966644b47959514d8b4fc6/html5/thumbnails/29.jpg)
Coding Dojo
● Prática deliberada de TDD
● Programação em par
● Aprendizado coletivo
+ é divertido!
![Page 30: O CPAN tem as ferramentas que você precisa para fazer TDD em Perl, o Coding Dojo tem o resto](https://reader033.vdocuments.pub/reader033/viewer/2022051209/54966644b47959514d8b4fc6/html5/thumbnails/30.jpg)
Como funciona?
Precisamos de:● Piloto e co-piloto
● Problema
● Computador
● Projetor
● Cronômetro
● Comida!
![Page 31: O CPAN tem as ferramentas que você precisa para fazer TDD em Perl, o Coding Dojo tem o resto](https://reader033.vdocuments.pub/reader033/viewer/2022051209/54966644b47959514d8b4fc6/html5/thumbnails/31.jpg)
5 ~ 7 min
![Page 32: O CPAN tem as ferramentas que você precisa para fazer TDD em Perl, o Coding Dojo tem o resto](https://reader033.vdocuments.pub/reader033/viewer/2022051209/54966644b47959514d8b4fc6/html5/thumbnails/32.jpg)
Retrospectiva
![Page 33: O CPAN tem as ferramentas que você precisa para fazer TDD em Perl, o Coding Dojo tem o resto](https://reader033.vdocuments.pub/reader033/viewer/2022051209/54966644b47959514d8b4fc6/html5/thumbnails/33.jpg)
Onde
Centro, toda quarta-feira às 18:30Av Treze de Maio, 13 – sala 616
Edifício Municipal
UFF - Niteróiquintas 19:00
TDD+
Perl
![Page 34: O CPAN tem as ferramentas que você precisa para fazer TDD em Perl, o Coding Dojo tem o resto](https://reader033.vdocuments.pub/reader033/viewer/2022051209/54966644b47959514d8b4fc6/html5/thumbnails/34.jpg)
![Page 35: O CPAN tem as ferramentas que você precisa para fazer TDD em Perl, o Coding Dojo tem o resto](https://reader033.vdocuments.pub/reader033/viewer/2022051209/54966644b47959514d8b4fc6/html5/thumbnails/35.jpg)
Agradecimentos
Breno (Garu) Dojo Rio
Contato about.me/rhcarvalho
dojorio.org