test automatici^2 per applicazioni web
DESCRIPTION
An introduction to testing tecniques for web applications, using WWW::Mechanize and HTTP::RecorderTRANSCRIPT
![Page 1: Test Automatici^2 per applicazioni Web](https://reader034.vdocuments.pub/reader034/viewer/2022042713/5491a296b4795927058b55b5/html5/thumbnails/1.jpg)
Test automatici2 per applicazioni WebStefano Rodighiero - [email protected]
![Page 2: Test Automatici^2 per applicazioni Web](https://reader034.vdocuments.pub/reader034/viewer/2022042713/5491a296b4795927058b55b5/html5/thumbnails/2.jpg)
Scrivere software =
Gestire la complessità
![Page 3: Test Automatici^2 per applicazioni Web](https://reader034.vdocuments.pub/reader034/viewer/2022042713/5491a296b4795927058b55b5/html5/thumbnails/3.jpg)
![Page 4: Test Automatici^2 per applicazioni Web](https://reader034.vdocuments.pub/reader034/viewer/2022042713/5491a296b4795927058b55b5/html5/thumbnails/4.jpg)
![Page 5: Test Automatici^2 per applicazioni Web](https://reader034.vdocuments.pub/reader034/viewer/2022042713/5491a296b4795927058b55b5/html5/thumbnails/5.jpg)
![Page 6: Test Automatici^2 per applicazioni Web](https://reader034.vdocuments.pub/reader034/viewer/2022042713/5491a296b4795927058b55b5/html5/thumbnails/6.jpg)
Complessità
You might think of this as mental juggling - the more mental balls the program requires you to keep in the air at once, the
more likely you’ll drop one of the balls, leading to a design or coding error.
-- Steve McConnell
![Page 7: Test Automatici^2 per applicazioni Web](https://reader034.vdocuments.pub/reader034/viewer/2022042713/5491a296b4795927058b55b5/html5/thumbnails/7.jpg)
• Loose coupling
• Tecniche standard
• KISS
• Test?
Gestire la complessità
![Page 8: Test Automatici^2 per applicazioni Web](https://reader034.vdocuments.pub/reader034/viewer/2022042713/5491a296b4795927058b55b5/html5/thumbnails/8.jpg)
Test?
«Testing by itself does not improve software quality. [...] Trying to improve software by increasing the amount of testing is like trying to lose weight by weighting yourself more often»
-- Steve McConnell
![Page 9: Test Automatici^2 per applicazioni Web](https://reader034.vdocuments.pub/reader034/viewer/2022042713/5491a296b4795927058b55b5/html5/thumbnails/9.jpg)
Test!
• Aumenta indirettamente la qualità del software
• Design for testing
• Refactoring
• Wishful thinking
• Confidenza con il proprio codice
![Page 10: Test Automatici^2 per applicazioni Web](https://reader034.vdocuments.pub/reader034/viewer/2022042713/5491a296b4795927058b55b5/html5/thumbnails/10.jpg)
Esempio
#!/usr/bin/perl -w
use Test::Simple tests => 2; use Date::ICal;
my $ical = Date::ICal->new; # make an objectok( defined $ical ); # check we got something ok( $ical->isa('Date::ICal') ); # and it's the right class
![Page 11: Test Automatici^2 per applicazioni Web](https://reader034.vdocuments.pub/reader034/viewer/2022042713/5491a296b4795927058b55b5/html5/thumbnails/11.jpg)
Gradualmente
• prova.pl → t/01_base.t
• diag()
• Usare gli occhi
• map <F8> :! prove %<C-M>
• YMMV :)
![Page 12: Test Automatici^2 per applicazioni Web](https://reader034.vdocuments.pub/reader034/viewer/2022042713/5491a296b4795927058b55b5/html5/thumbnails/12.jpg)
Applicazioni web
![Page 13: Test Automatici^2 per applicazioni Web](https://reader034.vdocuments.pub/reader034/viewer/2022042713/5491a296b4795927058b55b5/html5/thumbnails/13.jpg)
Paradigma MVC
Presentazione Model
Controllo
![Page 14: Test Automatici^2 per applicazioni Web](https://reader034.vdocuments.pub/reader034/viewer/2022042713/5491a296b4795927058b55b5/html5/thumbnails/14.jpg)
![Page 15: Test Automatici^2 per applicazioni Web](https://reader034.vdocuments.pub/reader034/viewer/2022042713/5491a296b4795927058b55b5/html5/thumbnails/15.jpg)
• Situazioni difficili da riprodurre
• Grande complessità
• Chi effettua il test non è il programmatore
Test di videogame
![Page 16: Test Automatici^2 per applicazioni Web](https://reader034.vdocuments.pub/reader034/viewer/2022042713/5491a296b4795927058b55b5/html5/thumbnails/16.jpg)
?
![Page 17: Test Automatici^2 per applicazioni Web](https://reader034.vdocuments.pub/reader034/viewer/2022042713/5491a296b4795927058b55b5/html5/thumbnails/17.jpg)
![Page 18: Test Automatici^2 per applicazioni Web](https://reader034.vdocuments.pub/reader034/viewer/2022042713/5491a296b4795927058b55b5/html5/thumbnails/18.jpg)
• Workflow complessi
• Quello che non è automatico tende a non essere fatto
Test di applicazioni web
![Page 19: Test Automatici^2 per applicazioni Web](https://reader034.vdocuments.pub/reader034/viewer/2022042713/5491a296b4795927058b55b5/html5/thumbnails/19.jpg)
WWW::Mechanize
#!/usr/bin/perl
use strict;use warnings;
use WWW::Mechanize;
my $agent = WWW::Mechanize->new();$agent->get( 'http://www.google.com' );print $agent->content();
![Page 20: Test Automatici^2 per applicazioni Web](https://reader034.vdocuments.pub/reader034/viewer/2022042713/5491a296b4795927058b55b5/html5/thumbnails/20.jpg)
Test::WWW::Mechanize
#!/usr/bin/perl
use strict;use warnings;
use Test::More qw/ no_plan /;use Test::WWW::Mechanize;
my $agent = Test::WWW::Mechanize->new();$agent->get_ok( 'http://www.google.com' );$agent->title_is( 'Google' );
![Page 21: Test Automatici^2 per applicazioni Web](https://reader034.vdocuments.pub/reader034/viewer/2022042713/5491a296b4795927058b55b5/html5/thumbnails/21.jpg)
WWW::Mechanize::Shell
calvin:~ larsen$ perl -MWWW::Mechanize::Shell -e shell>g http://www.google.comRetrieving http://www.google.com(200)http://www.google.com>script mech3.plhttp://www.google.com>quitcalvin:~ larsen$
![Page 22: Test Automatici^2 per applicazioni Web](https://reader034.vdocuments.pub/reader034/viewer/2022042713/5491a296b4795927058b55b5/html5/thumbnails/22.jpg)
WWW::Mechanize::Shell
#!perl -wuse strict;use WWW::Mechanize;use WWW::Mechanize::FormFiller;use URI::URL;
my $agent = WWW::Mechanize->new( autocheck => 1 );my $formfiller = WWW::Mechanize::FormFiller->new();$agent->env_proxy();
$agent->get('http://www.google.com');$agent->form(1) if $agent->forms and scalar @{$agent->forms};
mech3.pl
![Page 23: Test Automatici^2 per applicazioni Web](https://reader034.vdocuments.pub/reader034/viewer/2022042713/5491a296b4795927058b55b5/html5/thumbnails/23.jpg)
![Page 24: Test Automatici^2 per applicazioni Web](https://reader034.vdocuments.pub/reader034/viewer/2022042713/5491a296b4795927058b55b5/html5/thumbnails/24.jpg)
HTTP::Recorder
![Page 25: Test Automatici^2 per applicazioni Web](https://reader034.vdocuments.pub/reader034/viewer/2022042713/5491a296b4795927058b55b5/html5/thumbnails/25.jpg)
proxy.pl#!/usr/bin/perl
use strict;use warnings;
use HTTP::Proxy qw/ :log /;use HTTP::Recorder;
my $proxy = HTTP::Proxy->new;my $agent = HTTP::Recorder->new( file => "/tmp/tmpfile" , showwindow => 1 );
$proxy->logmask( STATUS | PROCESS | HEADERS );$proxy->port( 3128 );$proxy->host( '' );$proxy->agent( $agent );$proxy->start();
![Page 26: Test Automatici^2 per applicazioni Web](https://reader034.vdocuments.pub/reader034/viewer/2022042713/5491a296b4795927058b55b5/html5/thumbnails/26.jpg)
Demo
![Page 27: Test Automatici^2 per applicazioni Web](https://reader034.vdocuments.pub/reader034/viewer/2022042713/5491a296b4795927058b55b5/html5/thumbnails/27.jpg)
Idee
• Usare proxy.pl con un sistema di bugtracking (il ticket contiene il codice per riprodurre l’errore)
• Testare applicazioni non scritte in Perl
• Screenscraping
![Page 28: Test Automatici^2 per applicazioni Web](https://reader034.vdocuments.pub/reader034/viewer/2022042713/5491a296b4795927058b55b5/html5/thumbnails/28.jpg)
Conclusioni
• Integrate le pratiche di testing nel vostro workflow lavorativo
• Gradualmente!
• Sono pratiche che pagano da subito
• Come posso progettare il codice per facilitarne il test?
![Page 29: Test Automatici^2 per applicazioni Web](https://reader034.vdocuments.pub/reader034/viewer/2022042713/5491a296b4795927058b55b5/html5/thumbnails/29.jpg)
Monday, I could wait till TuesdayIf I make up my mindWednesday would be fine, Thursday’s on my mindFriday’d give me time, Saturday could waitBut Sunday’d be too late-- Sting
![Page 31: Test Automatici^2 per applicazioni Web](https://reader034.vdocuments.pub/reader034/viewer/2022042713/5491a296b4795927058b55b5/html5/thumbnails/31.jpg)
Riferimenti• Web Testing with HTTP::Recorder http://www.perl.com/pub/a/2004/06/04/recorder.html
• Test di applicazioni web http://www.perl.it/blog/archives/000036.html
• A Perl Testing Tutorial (PDF) http://www.wgz.org/chromatic/perl/IntroTestMore.pdf
• Migliorare la navigazione sul web con Perl http://www.perl.it/blog/archives/000076.html
• Documentazione dei vari moduli menzionati http://search.cpan.org/