RT #68376
[catagits/Catalyst-Manual.git] / lib / Catalyst / Manual / Tutorial / 08_Testing.pod
1 =head1 NAME
2
3 Catalyst::Manual::Tutorial::08_Testing - Catalyst Tutorial - Chapter 8: Testing
4
5
6 =head1 OVERVIEW
7
8 This is B<Chapter 8 of 10> for the Catalyst tutorial.
9
10 L<Tutorial Overview|Catalyst::Manual::Tutorial>
11
12 =over 4
13
14 =item 1
15
16 L<Introduction|Catalyst::Manual::Tutorial::01_Intro>
17
18 =item 2
19
20 L<Catalyst Basics|Catalyst::Manual::Tutorial::02_CatalystBasics>
21
22 =item 3
23
24 L<More Catalyst Basics|Catalyst::Manual::Tutorial::03_MoreCatalystBasics>
25
26 =item 4
27
28 L<Basic CRUD|Catalyst::Manual::Tutorial::04_BasicCRUD>
29
30 =item 5
31
32 L<Authentication|Catalyst::Manual::Tutorial::05_Authentication>
33
34 =item 6
35
36 L<Authorization|Catalyst::Manual::Tutorial::06_Authorization>
37
38 =item 7
39
40 L<Debugging|Catalyst::Manual::Tutorial::07_Debugging>
41
42 =item 8
43
44 B<08_Testing>
45
46 =item 9
47
48 L<Advanced CRUD|Catalyst::Manual::Tutorial::09_AdvancedCRUD>
49
50 =item 10
51
52 L<Appendices|Catalyst::Manual::Tutorial::10_Appendices>
53
54 =back
55
56
57 =head1 DESCRIPTION
58
59 You may have noticed that the Catalyst Helper scripts automatically
60 create basic C<.t> test scripts under the C<t> directory.  This chapter
61 of the tutorial briefly looks at how these tests can be used not only to
62 ensure that your application is working correctly at the present time,
63 but also provide automated regression testing as you upgrade various
64 pieces of your application over time.
65
66 You can check out the source code for this example from the Catalyst
67 Subversion repository as per the instructions in
68 L<Catalyst::Manual::Tutorial::01_Intro>.
69
70 For an excellent introduction to learning the many benefits of testing
71 your Perl applications and modules, you might want to read 'Perl
72 Testing: A Developer's Notebook' by Ian Langworth and chromatic.
73
74
75 =head1 RUNNING THE "CANNED" CATALYST TESTS
76
77 There are a variety of ways to run Catalyst and Perl tests (for example,
78 C<perl Makefile.PL> and C<make test>), but one of the easiest is with
79 the C<prove> command.  For example, to run all of the tests in the C<t>
80 directory, enter:
81
82     $ prove -wl t
83
84 There will be a lot of output because we have the C<-Debug> flag enabled
85 in C<lib/MyApp.pm> (see the C<CATALYST_DEBUG=0> tip below for a quick
86 and easy way to reduce the clutter).  Look for lines like this for
87 errors:
88
89     #   Failed test 'Request should succeed'
90     #   at t/controller_Books.t line 8.
91     # Looks like you failed 1 test of 3.
92
93 The redirection used by the Authentication plugins will cause several
94 failures in the default tests.  You can fix this by making the following
95 changes:
96
97 1) Change the line in C<t/01app.t> that reads:
98
99     ok( request('/')->is_success, 'Request should succeed' );
100
101 to:
102
103     ok( request('/login')->is_success, 'Request should succeed' );
104
105 2) Change the line in C<t/controller_Logout.t> that reads:
106
107     ok( request('/logout')->is_success, 'Request should succeed' );
108
109 to:
110
111     ok( request('/logout')->is_redirect, 'Request should succeed' );
112
113 3) Change the line in C<t/controller_Books.t> that reads:
114
115     ok( request('/books')->is_success, 'Request should succeed' );
116
117 to:
118
119     ok( request('/books')->is_redirect, 'Request should succeed' );
120
121 4) Add the following statement to the top of C<t/view_HTML.t>:
122
123     use MyApp;
124
125 As you can see in the C<prove> command line above, the C<--lib> option
126 is used to set the location of the Catalyst C<lib> directory.  With this
127 command, you will get all of the usual development server debug output,
128 something most people prefer to disable while running tests cases.
129 Although you can edit the C<lib/MyApp.pm> to comment out the C<-Debug>
130 plugin, it's generally easier to simply set the C<CATALYST_DEBUG=0>
131 environment variable.  For example:
132
133     $ CATALYST_DEBUG=0 prove -wl t
134
135 During the C<t/02pod> and C<t/03podcoverage> tests, you might notice the
136 C<all skipped: set TEST_POD to enable this test> warning message.  To
137 execute the Pod-related tests, add C<TEST_POD=1> to the C<prove>
138 command:
139
140     $ CATALYST_DEBUG=0 TEST_POD=1 prove -wl t
141
142 If you omitted the Pod comments from any of the methods that were
143 inserted, you might have to go back and fix them to get these tests to
144 pass. :-)
145
146 Another useful option is the C<verbose> (C<-v>) option to C<prove>.  It
147 prints the name of each test case as it is being run:
148
149     $ CATALYST_DEBUG=0 TEST_POD=1 prove -vwl t
150
151
152 =head1 RUNNING A SINGLE TEST
153
154 You can also run a single script by appending its name to the C<prove>
155 command. For example:
156
157     $ CATALYST_DEBUG=0 prove -wl t/01app.t
158
159 Also note that you can also run tests directly from Perl without
160 C<prove>.  For example:
161
162     $ CATALYST_DEBUG=0 perl -w -Ilib t/01app.t
163
164
165 =head1 ADDING YOUR OWN TEST SCRIPT
166
167 Although the Catalyst helper scripts provide a basic level of checks
168 "for free," testing can become significantly more helpful when you write
169 your own script to exercise the various parts of your application.  The
170 L<Test::WWW::Mechanize::Catalyst> module is very popular for writing
171 these sorts of test cases.  This module extends L<Test::WWW::Mechanize>
172 (and therefore L<WWW::Mechanize>) to allow you to automate the action of
173 a user "clicking around" inside your application.  It gives you all the
174 benefits of testing on a live system without the messiness of having to
175 use an actual web server, and a real person to do the clicking.
176
177 To create a sample test case, open the C<t/live_app01.t> file in your
178 editor and enter the following:
179
180     #!/usr/bin/perl
181     
182     use strict;
183     use warnings;
184     use Test::More;
185     
186     # Need to specify the name of your app as arg on next line
187     # Can also do:
188     #   use Test::WWW::Mechanize::Catalyst "MyApp";
189     
190     BEGIN { use_ok("Test::WWW::Mechanize::Catalyst" => "MyApp") }
191     
192     # Create two 'user agents' to simulate two different users ('test01' & 'test02')
193     my $ua1 = Test::WWW::Mechanize::Catalyst->new;
194     my $ua2 = Test::WWW::Mechanize::Catalyst->new;
195     
196     # Use a simplified for loop to do tests that are common to both users
197     # Use get_ok() to make sure we can hit the base URL
198     # Second arg = optional description of test (will be displayed for failed tests)
199     # Note that in test scripts you send everything to 'http://localhost'
200     $_->get_ok("http://localhost/", "Check redirect of base URL") for $ua1, $ua2;
201     # Use title_is() to check the contents of the <title>...</title> tags
202     $_->title_is("Login", "Check for login title") for $ua1, $ua2;
203     # Use content_contains() to match on text in the html body
204     $_->content_contains("You need to log in to use this application",
205         "Check we are NOT logged in") for $ua1, $ua2;
206     
207     # Log in as each user
208     # Specify username and password on the URL
209     $ua1->get_ok("http://localhost/login?username=test01&password=mypass", "Login 'test01'");
210     # Could make user2 like user1 above, but use the form to show another way
211     $ua2->submit_form(
212         fields => {
213             username => 'test02',
214             password => 'mypass',
215         });
216     
217     # Go back to the login page and it should show that we are already logged in
218     $_->get_ok("http://localhost/login", "Return to '/login'") for $ua1, $ua2;
219     $_->title_is("Login", "Check for login page") for $ua1, $ua2;
220     $_->content_contains("Please Note: You are already logged in as ",
221         "Check we ARE logged in" ) for $ua1, $ua2;
222     
223     # 'Click' the 'Logout' link (see also 'text_regex' and 'url_regex' options)
224     $_->follow_link_ok({n => 4}, "Logout via first link on page") for $ua1, $ua2;
225     $_->title_is("Login", "Check for login title") for $ua1, $ua2;
226     $_->content_contains("You need to log in to use this application",
227         "Check we are NOT logged in") for $ua1, $ua2;
228     
229     # Log back in
230     $ua1->get_ok("http://localhost/login?username=test01&password=mypass", "Login 'test01'");
231     $ua2->get_ok("http://localhost/login?username=test02&password=mypass", "Login 'test02'");
232     # Should be at the Book List page... do some checks to confirm
233     $_->title_is("Book List", "Check for book list title") for $ua1, $ua2;
234     
235     $ua1->get_ok("http://localhost/books/list", "'test01' book list");
236     $ua1->get_ok("http://localhost/login", "Login Page");
237     $ua1->get_ok("http://localhost/books/list", "'test01' book list");
238     
239     $_->content_contains("Book List", "Check for book list title") for $ua1, $ua2;
240     # Make sure the appropriate logout buttons are displayed
241     $_->content_contains("/logout\">User Logout</a>",
242         "Both users should have a 'User Logout'") for $ua1, $ua2;
243     $ua1->content_contains("/books/form_create\">Admin Create</a>",
244         "'test01' should have a create link");
245     $ua2->content_lacks("/books/form_create\">Admin Create</a>",
246         "'test02' should NOT have a create link");
247     
248     $ua1->get_ok("http://localhost/books/list", "View book list as 'test01'");
249     
250     # User 'test01' should be able to create a book with the "formless create" URL
251     $ua1->get_ok("http://localhost/books/url_create/TestTitle/2/4",
252         "'test01' formless create");
253     $ua1->title_is("Book Created", "Book created title");
254     $ua1->content_contains("Added book 'TestTitle'", "Check title added OK");
255     $ua1->content_contains("by 'Stevens'", "Check author added OK");
256     $ua1->content_contains("with a rating of 2.", "Check rating added");
257     # Try a regular expression to combine the previous 3 checks & account for whitespace
258     $ua1->content_like(qr/Added book 'TestTitle'\s+by 'Stevens'\s+with a rating of 2./, "Regex check");
259     
260     # Make sure the new book shows in the list
261     $ua1->get_ok("http://localhost/books/list", "'test01' book list");
262     $ua1->title_is("Book List", "Check logged in and at book list");
263     $ua1->content_contains("Book List", "Book List page test");
264     $ua1->content_contains("TestTitle", "Look for 'TestTitle'");
265     
266     # Make sure the new book can be deleted
267     # Get all the Delete links on the list page
268     my @delLinks = $ua1->find_all_links(text => 'Delete');
269     # Use the final link to delete the last book
270     $ua1->get_ok($delLinks[$#delLinks]->url, 'Delete last book');
271     # Check that delete worked
272     $ua1->content_contains("Book List", "Book List page test");
273     $ua1->content_contains("Book deleted", "Book was deleted");
274     
275     # User 'test02' should not be able to add a book
276     $ua2->get_ok("http://localhost/books/url_create/TestTitle2/2/5", "'test02' add");
277     $ua2->content_contains("Unauthorized!", "Check 'test02' cannot add");
278     
279     done_testing;
280
281 The C<live_app.t> test cases uses copious comments to explain each step
282 of the process.  In addition to the techniques shown here, there are a
283 variety of other methods available in L<Test::WWW::Mechanize::Catalyst>
284 (for example, regex-based matching). Consult the documentation for more
285 detail.
286
287 B<TIP>: For I<unit tests> vs. the "full application tests" approach used
288 by L<Test::WWW::Mechanize::Catalyst>, see L<Catalyst::Test>.
289
290 B<Note:> The test script does not test the C<form_create> and
291 C<form_create_do> actions.  That is left as an exercise for the reader
292 (you should be able to complete that logic using the existing code as a
293 template).
294
295 To run the new test script, use a command such as:
296
297     $ CATALYST_DEBUG=0 prove -vwl t/live_app01.t
298
299 or
300
301     $ DBIC_TRACE=0 CATALYST_DEBUG=0 prove -vwl t/live_app01.t
302
303 Experiment with the C<DBIC_TRACE>, C<CATALYST_DEBUG> and C<-v> settings.
304 If you find that there are errors, use the techniques discussed in the
305 "Catalyst Debugging" section (Chapter 7) to isolate and fix any
306 problems.
307
308 If you want to run the test case under the Perl interactive debugger,
309 try a command such as:
310
311     $ DBIC_TRACE=0 CATALYST_DEBUG=0 perl -d -Ilib t/live_app01.t
312
313 Note that although this tutorial uses a single custom test case for
314 simplicity, you may wish to break your tests into different files for
315 better organization.
316
317 B<TIP:> If you have a test case that fails, you will receive an error
318 similar to the following:
319
320     #   Failed test 'Check we are NOT logged in'
321     #   in t/live_app01.t at line 31.
322     #     searched: "\x{0a}<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Tran"...
323     #   can't find: "You need to log in to use this application."
324
325 Unfortunately, this only shows us the first 50 characters of the HTML
326 returned by the request -- not enough to determine where the problem
327 lies.  A simple technique that can be used in such situations is to
328 temporarily insert a line similar to the following right after the
329 failed test:
330
331     diag $ua1->content;
332
333 This will cause the full HTML returned by the request to be displayed.
334
335 Another approach to see the full HTML content at the failure point in a
336 series of tests would be to insert a "C<$DB::single=1;> right above the
337 location of the failure and run the test under the perl debugger (with
338 C<-d>) as shown above.  Then you can use the debugger to explore the
339 state of the application right before or after the failure.
340
341
342 =head1 SUPPORTING BOTH PRODUCTION AND TEST DATABASES
343
344 You may wish to leverage the techniques discussed in this tutorial to
345 maintain both a "production database" for your live application and a
346 "testing database" for your test cases.  One advantage to
347 L<Test::WWW::Mechanize::Catalyst> is that it runs your full application;
348 however, this can complicate things when you want to support multiple
349 databases.
350
351 =head2 DATABASE CONFIG SWITCHING IN YOUR MODEL CLASS
352
353 One solution is to allow the database specification to be overridden
354 with an environment variable.  For example, open
355 C<lib/MyApp/Model/DB.pm> in your editor and change the
356 C<__PACKAGE__-E<gt>config(...> declaration to resemble:
357
358     my $dsn = $ENV{MYAPP_DSN} ||= 'dbi:SQLite:myapp.db';
359     __PACKAGE__->config(
360         schema_class => 'MyApp::Schema',
361     
362         connect_info => {
363             dsn => $dsn,
364             user => '',
365             password => '',
366             on_connect_do => q{PRAGMA foreign_keys = ON},
367         }
368     );
369
370 Then, when you run your test case, you can use commands such as:
371
372     $ cp myapp.db myappTEST.db
373     $ CATALYST_DEBUG=0 MYAPP_DSN="dbi:SQLite:myappTEST.db" prove -vwl t/live_app01.t
374
375 This will modify the DSN only while the test case is running.  If you
376 launch your normal application without the C<MYAPP_DSN> environment
377 variable defined, it will default to the same C<dbi:SQLite:myapp.db> as
378 before.
379
380
381 =head2 DATABASE CONFIG SWITCHING USING MULTIPLE CONFIG FILES
382
383 By utilizing L<Catalyst::Plugin::ConfigLoader>s functionality for
384 loading multiple config files based on environment variables you can
385 override your default (production) database connection settings.
386
387 Setting C<$ENV{ MYAPP_CONFIG_LOCAL_SUFFIX }> to 'testing' in your test
388 script results in loading of an additional config file named
389 C<myapp_testing.conf> after C<myapp.conf> which will override any
390 parameters in C<myapp.conf>.
391
392 You should set the environment variable in the BEGIN block of your test
393 script to make sure it's set before your Catalyst application is
394 started.
395
396 The following is an example for a config and test script for a
397 DBIx::Class model named MyDB and a controller named Foo:
398
399 myapp_testing.conf:
400
401     <Model::MyDB>
402         <connect_info>
403             dsn dbi:SQLite:myapp.db
404         </connect_info>
405     </Model::MyDB>
406
407
408 t/controller_Foo.t:
409
410     use strict;
411     use warnings;
412     use Test::More;
413     
414     BEGIN {
415         $ENV{ MYAPP_CONFIG_LOCAL_SUFFIX } = 'testing';
416     }
417     
418     eval "use Test::WWW::Mechanize::Catalyst 'MyApp'";
419     plan $@
420         ? ( skip_all => 'Test::WWW::Mechanize::Catalyst required' )
421         : ( tests => 2 );
422     
423     ok( my $mech = Test::WWW::Mechanize::Catalyst->new, 'Created mech object' );
424     
425     $mech->get_ok( 'http://localhost/foo' );
426
427
428 =head1 AUTHOR
429
430 Kennedy Clark, C<hkclark@gmail.com>
431
432 Feel free to contact the author for any errors or suggestions, but the
433 best way to report issues is via the CPAN RT Bug system at
434 <https://rt.cpan.org/Public/Dist/Display.html?Name=Catalyst-Manual>.
435
436 The most recent version of the Catalyst Tutorial can be found at
437 L<http://dev.catalyst.perl.org/repos/Catalyst/Catalyst-Manual/5.80/trunk/lib/Catalyst/Manual/Tutorial/>.
438
439 Copyright 2006-2010, Kennedy Clark, under the
440 Creative Commons Attribution Share-Alike License Version 3.0
441 (L<http://creativecommons.org/licenses/by-sa/3.0/us/>).