View
411
Download
0
Category
Preview:
DESCRIPTION
Palestra realizada no YAPC::Brasil 2011, em 05/11 no Largo do Machado / Rio de Janeiro.
Citation preview
Rodolfo Carvalho - @201YAPC::Brasil 2011
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
metacpan.org
CPAN: Comprehensive Perl Archive Network
Desenvolvimento "tradicional"
Design
Code
Test
TDD: Test-Driven Development
Design
Code
Test
Test
Refactor
No longo prazo...
...
tempo
x
● 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
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.
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)
Exemplo: Fibonacci
Implementação:sub fibonacci {
return 0;}
Resultado:$ prove fibonacci.pl fibonacci.pl .. ok All tests successful.
Exemplo: Fibonacci
...
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;}
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.
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.
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.
oferece muito mais
Perl
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)");};
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' );
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;
Test anything...
● Test::Pod::Coverage
● Test::UseAllModules
● Test::NoWarnings
● Test::Perl::Critic
● Test::AskAnExpert
● Test::Inline
● Test::UniqueTestNames
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
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
metacpan.org
Quero fazer TDD, e agora?
Coding Dojo
Coding Dojo
● Prática deliberada de TDD
● Programação em par
● Aprendizado coletivo
+ é divertido!
Como funciona?
Precisamos de:● Piloto e co-piloto
● Problema
● Computador
● Projetor
● Cronômetro
● Comida!
5 ~ 7 min
Retrospectiva
Onde
Centro, toda quarta-feira às 18:30Av Treze de Maio, 13 – sala 616
Edifício Municipal
UFF - Niteróiquintas 19:00
TDD+
Perl
Agradecimentos
Breno (Garu) Dojo Rio
Contato about.me/rhcarvalho
dojorio.org
Recommended