=head1 Catalyst Advent - Day 19 - Testing Catalyst provides a convenient way of testing your application during development and before deployment in a real environment. C makes it possible to run the same tests both locally (without an external daemon) and against a remote server via HTTP. =head3 Tests Let's examine a skeleton application's C directory: mundus:~/MyApp chansen$ ls -l t/ total 24 -rw-r--r-- 1 chansen chansen 95 18 Dec 20:50 01app.t -rw-r--r-- 1 chansen chansen 190 18 Dec 20:50 02pod.t -rw-r--r-- 1 chansen chansen 213 18 Dec 20:50 03podcoverage.t =over 4 =item C<01app.t> Verifies that the application loads, compiles, and returns a successful response. =item C<02pod.t> Verifies that all POD is free from errors. Only executed if the C environment variable is true. =item C<03podcoverage.t> Verifies that all methods/functions have POD coverage. Only executed if the C environment variable is true. =back =head3 Creating tests mundus:~/MyApp chansen$ cat t/01app.t | perl -ne 'printf( "%2d %s", $., $_ )' 1 use Test::More tests => 2; 2 use_ok( Catalyst::Test, 'MyApp' ); 3 4 ok( request('/')->is_success ); The first line declares how many tests we are going to run, in this case two. The second line tests and loads our application in test mode. The fourth line verifies that our application returns a successful response. C exports two functions, C and C. Each can take three different arguments: =over 4 =item A string which is a relative or absolute URI. request('/my/path'); request('http://www.host.com/my/path'); =item An instance of C. request( URI->new('http://www.host.com/my/path') ); =item An instance of C. request( HTTP::Request->new( GET => 'http://www.host.com/my/path') ); =back C returns an instance of C and C returns the content (body) of the response. =head3 Running tests locally mundus:~/MyApp chansen$ CATALYST_DEBUG=0 TEST_POD=1 prove --lib lib/ t/ t/01app............ok t/02pod............ok t/03podcoverage....ok All tests successful. Files=3, Tests=4, 2 wallclock secs ( 1.60 cusr + 0.36 csys = 1.96 CPU) C ensures that debugging is off; if it's enabled you will see debug logs between tests. C enables POD checking and coverage. C A command-line tool that makes it easy to run tests. You can find out more about it from the links below. =head3 Running tests remotely mundus:~/MyApp chansen$ CATALYST_SERVER=http://localhost:3000/ prove --lib lib/ t/01app.t t/01app....ok All tests successful. Files=1, Tests=2, 0 wallclock secs ( 0.40 cusr + 0.01 csys = 0.41 CPU) C is the absolute deployment URI of your application. In C or C it should be the host and path to the script. =head3 C and Catalyst Be sure to check out C. It makes it easy to test HTML, forms and links. A short example of usage: use Test::More tests => 6; use_ok( Test::WWW::Mechanize::Catalyst, 'MyApp' ); my $mech = Test::WWW::Mechanize::Catalyst->new; $mech->get_ok("http://localhost/", 'Got index page'); $mech->title_like( qr/^MyApp on Catalyst/, 'Got right index title' ); ok( $mech->find_link( text_regex => qr/^Wiki/i ), 'Found link to Wiki' ); ok( $mech->find_link( text_regex => qr/^Mailing-List/i ), 'Found link to Mailing-List' ); ok( $mech->find_link( text_regex => qr/^IRC channel/i ), 'Found link to IRC channel' ); =head3 Further Reading =over 4 =item Catalyst::Test L =item Test::WWW::Mechanize::Catalyst L =item Test::WWW::Mechanize L =item WWW::Mechanize L =item LWP::UserAgent L =item HTML::Form L =item HTTP::Message L =item HTTP::Request L =item HTTP::Request::Common L =item HTTP::Response L =item HTTP::Status L =item URI L =item Test::More L =item Test::Pod L =item Test::Pod::Coverage L =item prove (Test::Harness) L =back --Christian Hansen