A lot of people here uses CGI::Application, Class::DBI and HTML::Template for their development, and i wonder how do they write their tests suites.

Testing Class::DBI its easy and I usually use a pipe to get CGI::Application STDOUT:
$dades=run('rm=login login=test password=foo'); ok( $dades =~ /No such login/,'Webapp login bad login successful'); $dades=run('rm=stats'); ok( $dades =~ /Invalid cookie/,'Webapp stats no cookie successful'); $dades=run('rm=stats',1); ok( $dades =~ /Resume for/,'Webapp stats running successful'); ##################################### sub run { pipe(STDIN,STDOUT); ### unless(fork()) { close(STDIN); local $_ = shift; if(shift) { *CGI::cookie = sub { 'a_valid_session_for_test' }; } while (/([^=&\s]+)=([^=&\s]+)/g) { $webapp->query->param($1,$2); } $webapp->run(); $_ = *CGI::cookie; # Just to avoid warnings exit; } else { close(STDOUT); return join "\n",<STDIN>; } }

How do you write your own tests?

$anarion=\$anarion;

s==q^QBY_^=,$_^=$[x7,print

Replies are listed 'Best First'.
Re: Tests for CGI::Application
by Corion (Patriarch) on Nov 06, 2003 at 12:17 UTC

    I usually fake a query object (using Test::MockObject and reusing some setup I did in a script before :-)), and then run the tests against the query object and my CGI::Application subclass. There I can easily collect the output via the run method:

    my $output = $app->run(@params); is( $output, $expected, $name ); # and so on ...

    This is not necessarily a full end-to-end test though, as this might miss some of the headers, but I found it good enough for my needs.

    perl -MHTTP::Daemon -MHTTP::Response -MLWP::Simple -e ' ; # The $d = new HTTP::Daemon and fork and getprint $d->url and exit;#spider ($c = $d->accept())->get_request(); $c->send_response( new #in the HTTP::Response(200,$_,$_,qq(Just another Perl hacker\n))); ' # web
Re: Tests for CGI::Application
by Anonymous Monk on Nov 06, 2003 at 15:26 UTC
    Here's how I do it
    use MyFoo; use Test::More tests => 1; my $app = MyFoo->new(); ... $app->query( foo => 2); $app->query( op => 'jump'); ok( $app->run() =~ /you need to add bar/, 'missing bar' ); ...