Switch from 'sub base :Path :Args(0)' to 'sub index : Private' for
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Manual / Tutorial / Testing.pod
CommitLineData
4d583dd8 1=head1 NAME
2
64ccd8a8 3Catalyst::Manual::Tutorial::Testing - Catalyst Tutorial - Part 7: Testing
4d583dd8 4
4d583dd8 5=head1 OVERVIEW
6
7This is B<Part 7 of 9> for the Catalyst tutorial.
8
64ccd8a8 9L<Tutorial Overview|Catalyst::Manual::Tutorial>
4d583dd8 10
11=over 4
12
13=item 1
14
15L<Introduction|Catalyst::Manual::Tutorial::Intro>
16
17=item 2
18
19L<Catalyst Basics|Catalyst::Manual::Tutorial::CatalystBasics>
20
21=item 3
22
64ccd8a8 23L<Basic CRUD|Catalyst::Manual::Tutorial_BasicCRUD>
4d583dd8 24
25=item 4
26
27L<Authentication|Catalyst::Manual::Tutorial::Authentication>
28
29=item 5
30
31L<Authorization|Catalyst::Manual::Tutorial::Authorization>
32
33=item 6
34
35L<Debugging|Catalyst::Manual::Tutorial::Debugging>
36
37=item 7
38
39B<Testing>
40
41=item 8
42
43L<AdvancedCRUD|Catalyst::Manual::Tutorial::AdvancedCRUD>
44
45=item 9
46
3c098c71 47L<Appendices|Catalyst::Manual::Tutorial::Appendices>
4d583dd8 48
49=back
50
4d583dd8 51=head1 DESCRIPTION
52
64ccd8a8 53You may have noticed that the Catalyst Helper scripts automatically
3c098c71 54create basic C<.t> test scripts under the C<t> directory. This part of
55the tutorial briefly looks at how these tests can be used to not only
56ensure that your application is working correctly at the present time,
57but also provide automated regression testing as you upgrade various
58pieces of your application over time.
4d583dd8 59
64ccd8a8 60B<TIP>: Note that all of the code for this part of the tutorial can be
61pulled from the Catalyst Subversion repository in one step with the
62following command:
4d583dd8 63
64 svn checkout http://dev.catalyst.perl.org/repos/Catalyst/trunk/examples/Tutorial@###
65 IMPORTANT: Does not work yet. Will be completed for final version.
66
67
4d583dd8 68=head1 RUNNING THE "CANNED" CATALYST TESTS
69
64ccd8a8 70There are a variety of ways to run Catalyst and Perl tests (for example,
cc548726 71C<perl Makefile.PL> and C<make test>), but one of the easiest is with the
64ccd8a8 72C<prove> command. For example, to run all of the tests in the C<t>
73directory, enter:
4d583dd8 74
75 $ prove --lib lib t
76
64ccd8a8 77The redirection used by the Authentication plugins will cause the
78default C<t/01app.t> to fail. You can fix this by changing the line in
79C<t/01app.t> that read:
4d583dd8 80
81 ok( request('/')->is_success, 'Request should succeed' );
82
83to:
84
85 ok( request('/login')->is_success, 'Request should succeed' );
86
64ccd8a8 87So that a redirect is not necessary. Also, the C<t/controller_Books.t>
88and C<t/controller_Logout.t> default test cases will fail because of the
89authorization. You can delete these two files to prevent false error
90messages:
4d583dd8 91
92 $ rm t/controller_Books.t
93 $ rm t/controller_Logout.t
94
64ccd8a8 95As you can see in the C<prove> command line above, the C<--lib> option
96is used to set the location of the Catalyst C<lib> directory. With this
97command, you will get all of the usual development server debug output,
98something most people prefer to disable while running tests cases.
99Although you can edit the C<lib/MyApp.pm> to comment out the C<-Debug>
100plugin, it's generally easier to simply set the C<CATALYST_DEBUG=0>
101environment variable. For example:
4d583dd8 102
3c098c71 103 $ CATALYST_DEBUG=0 prove --lib lib t
4d583dd8 104
64ccd8a8 105During the C<t/02pod> and C<t/03podcoverage> tests, you might notice the
106C<all skipped: set TEST_POD to enable this test> warning message. To
107execute the Pod-related tests, add C<TEST_POD=1> to the C<prove>
108command:
4d583dd8 109
110 $ CATALYST_DEBUG=0 TEST_POD=1 prove --lib lib t
111
64ccd8a8 112If you omitted the Pod comments from any of the methods that were
113inserted, you might have to go back and fix them to get these tests to
114pass. :-)
4d583dd8 115
64ccd8a8 116Another useful option is the C<verbose> (C<-v>) option to C<prove>. It
117prints the name of each test case as it is being run:
4d583dd8 118
119 $ CATALYST_DEBUG=0 TEST_POD=1 prove --lib lib -v t
120
4d583dd8 121=head1 RUNNING A SINGLE TEST
122
64ccd8a8 123You can also run a single script by appending its name to the C<prove>
124command. For example:
4d583dd8 125
126 $ CATALYST_DEBUG=0 prove --lib lib t/01app.t
127
64ccd8a8 128Note that you can also run tests directly from Perl without C<prove>.
129For example:
4d583dd8 130
131 $ CATALYST_DEBUG=0 perl -Ilib t/01app.t
132
4d583dd8 133=head1 ADDING YOUR OWN TEST SCRIPT
134
64ccd8a8 135Although the Catalyst helper scripts provide a basic level of checks
136"for free," testing can become significantly more helpful when you write
137your own script to exercise the various parts of your application. The
cc548726 138L<Test::WWW::Mechanize::Catalyst|Test::WWW::Mechanize::Catalyst> module
139is very popular for writing these sorts of test cases. This module
140extends L<Test::WWW::Mechanize|Test::WWW::Mechanize> (and therefore
141L<WWW::Mechanize|WWW::Mechanize>) to allow you to automate the action of
64ccd8a8 142a user "clicking around" inside your application. It gives you all the
143benefits of testing on a live system without the messiness of having to
3c098c71 144use an actual web server, and a real person to do the clicking.
4d583dd8 145
64ccd8a8 146To create a sample test case, open the C<t/live_app01.t> file in your
147editor and enter the following:
4d583dd8 148
149 #!/usr/bin/perl
150
151 use strict;
152 use warnings;
153
3c098c71 154 # Load testing framework and use 'no_plan' to dynamically pick up
155 # all tests. Better to replace "'no_plan'" with "tests => 30" so it
156 # knows exactly how many tests need to be run (and will tell you if
157 # not), but 'no_plan' is nice for quick & dirty tests
be16bacd 158
4d583dd8 159 use Test::More 'no_plan';
160
161 # Need to specify the name of your app as arg on next line
162 # Can also do:
163 # use Test::WWW::Mechanize::Catalyst "MyApp";
be16bacd 164
4d583dd8 165 use ok "Test::WWW::Mechanize::Catalyst" => "MyApp";
3c098c71 166
4d583dd8 167 # Create two 'user agents' to simulate two different users ('test01' & 'test02')
168 my $ua1 = Test::WWW::Mechanize::Catalyst->new;
169 my $ua2 = Test::WWW::Mechanize::Catalyst->new;
170
171 # Use a simplified for loop to do tests that are common to both users
172 # Use get_ok() to make sure we can hit the base URL
173 # Second arg = optional description of test (will be displayed for failed tests)
174 # Note that in test scripts you send everything to 'http://localhost'
175 $_->get_ok("http://localhost/", "Check redirect of base URL") for $ua1, $ua2;
176 # Use title_is() to check the contents of the <title>...</title> tags
177 $_->title_is("Login", "Check for login title") for $ua1, $ua2;
178 # Use content_contains() to match on test in the html body
179 $_->content_contains("You need to log in to use this application",
180 "Check we are NOT logged in") for $ua1, $ua2;
181
182 # Log in as each user
183 $ua1->get_ok("http://localhost/login?username=test01&password=mypass", "Login 'test01'");
184 $ua2->get_ok("http://localhost/login?username=test02&password=mypass", "Login 'test02'");
185
186 # Go back to the login page and it should show that we are already logged in
187 $_->get_ok("http://localhost/login", "Return to '/login'") for $ua1, $ua2;
188 $_->title_is("Login", "Check for login page") for $ua1, $ua2;
189 $_->content_contains("Please Note: You are already logged in as ",
190 "Check we ARE logged in" ) for $ua1, $ua2;
191
192 # 'Click' the 'Logout' link
193 $_->follow_link_ok({n => 1}, "Logout via first link on page") for $ua1, $ua2;
194 $_->title_is("Login", "Check for login title") for $ua1, $ua2;
195 $_->content_contains("You need to log in to use this application",
196 "Check we are NOT logged in") for $ua1, $ua2;
197
198 # Log back in
199 $ua1->get_ok("http://localhost/login?username=test01&password=mypass", "Login 'test01'");
200 $ua2->get_ok("http://localhost/login?username=test02&password=mypass", "Login 'test02'");
201 # Should be at the Book List page... do some checks to confirm
202 $_->title_is("Book List", "Check for book list title") for $ua1, $ua2;
203
204 $ua1->get_ok("http://localhost/books/list", "'test01' book list");
205 $ua1->get_ok("http://localhost/login", "Login Page");
206 $ua1->get_ok("http://localhost/books/list", "'test01' book list");
207
208 $_->content_contains("Book List", "Check for book list title") for $ua1, $ua2;
209 # Make sure the appropriate logout buttons are displayed
210 $_->content_contains("/logout\">Logout</a>",
211 "Both users should have a 'User Logout'") for $ua1, $ua2;
212 $ua1->content_contains("/books/form_create\">Create</a>",
213 "Only 'test01' should have a create link");
214
215 $ua1->get_ok("http://localhost/books/list", "View book list as 'test01'");
216
217 # User 'test01' should be able to create a book with the "formless create" URL
218 $ua1->get_ok("http://localhost/books/url_create/TestTitle/2/4",
219 "'test01' formless create");
220 $ua1->title_is("Book Created", "Book created title");
be16bacd 221 $ua1->content_contains("Added book 'TestTitle'", "Check title added OK");
222 $ua1->content_contains("by 'Stevens'", "Check author added OK");
4d583dd8 223 $ua1->content_contains("a rating of 2.", "Check rating added");
224
225 # Make sure the new book shows in the list
226 $ua1->get_ok("http://localhost/books/list", "'test01' book list");
227 $ua1->title_is("Book List", "Check logged in and at book list");
228 $ua1->content_contains("Book List", "Book List page test");
229 $ua1->content_contains("TestTitle", "Look for 'TestTitle'");
230
231 # Make sure the new book can be deleted
232 # Get all the Delete links on the list page
233 my @delLinks = $ua1->find_all_links(text => 'Delete');
234 # Use the final link to delete the last book
235 $ua1->get_ok($delLinks[$#delLinks]->url, 'Delete last book');
236 # Check that delete worked
237 $ua1->content_contains("Book List", "Book List page test");
238 $ua1->content_contains("Book deleted.", "Book was deleted");
239
240 # User 'test02' should not be able to add a book
241 $ua2->get_ok("http://localhost/books/url_create/TestTitle2/2/5", "'test02' add");
242 $ua2->content_contains("Unauthorized!", "Check 'test02' cannot add");
243
64ccd8a8 244The C<live_app.t> test cases uses copious comments to explain each step
245of the process. In addition to the techniques shown here, there are a
cc548726 246variety of other methods available in
247L<Test::WWW::Mechanize::Catalyst|Test::WWW::Mechanize::Catalyst> (for
248example, regex-based matching). Consult the documentation for more
64ccd8a8 249detail.
4d583dd8 250
64ccd8a8 251B<TIP>: For I<unit tests> vs. the "full application tests" approach used
cc548726 252by L<Test::WWW::Mechanize::Catalyst|Test::WWW::Mechanize::Catalyst>, see
253L<Catalyst::Test|Catalyst::Test>.
4d583dd8 254
64ccd8a8 255B<Note:> The test script does not test the C<form_create> and
256C<form_create_do> actions. That is left as an exercise for the reader
257(you should be able to complete that logic using the existing code as a
258template).
4d583dd8 259
260To run the new test script, use a command such as:
261
262 $ CATALYST_DEBUG=0 prove --lib lib -v t/live_app01.t
263
264or
265
266 $ DBIX_CLASS_STORAGE_DBI_DEBUG=0 CATALYST_DEBUG=0 prove --lib lib -v t/live_app01.t
267
64ccd8a8 268Experiment with the C<DBIX_CLASS_STORAGE_DBI_DEBUG>, C<CATALYST_DEBUG>
269and C<-v> settings. If you find that there are errors, use the
270techniques discussed in the "Catalyst Debugging" section (Part 6) to
3c098c71 271isolate and fix any problems.
4d583dd8 272
64ccd8a8 273If you want to run the test case under the Perl interactive debugger,
274try a command such as:
4d583dd8 275
276 $ DBIX_CLASS_STORAGE_DBI_DEBUG=0 CATALYST_DEBUG=0 perl -d -Ilib t/live_app01.t
277
3c098c71 278Note that although this tutorial uses a single custom test case for
64ccd8a8 279simplicity, you may wish to break your tests into different files for
280better organization.
4d583dd8 281
4d583dd8 282=head1 SUPPORTING BOTH PRODUCTION AND TEST DATABASES
283
64ccd8a8 284You may wish to leverage the techniques discussed in this tutorial to
285maintain both a "production database" for your live application and a
286"testing database" for your test cases. One advantage to
3c098c71 287L<Test::WWW::Mechanize::Catalyst> is that
64ccd8a8 288it runs your full application; however, this can complicate things when
289you want to support multiple databases. One solution is to allow the
290database specification to be overridden with an environment variable.
291For example, open C<lib/MyApp/Model/MyAppDB.pm> in your editor and
292change the C<__PACKAGE__-E<gt>config(...> declaration to resemble:
4d583dd8 293
294 my $dsn = $ENV{MYAPP_DSN} ||= 'dbi:SQLite:myapp.db';
295 __PACKAGE__->config(
296 schema_class => 'MyAppDB',
297 connect_info => [
298 $dsn,
299 '',
300 '',
301 { AutoCommit => 1 },
302
303 ],
304 );
305
306Then, when you run your test case, you can use commands such as:
307
308 $ cp myapp.db myappTEST.db
309 $ CATALYST_DEBUG=0 MYAPP_DSN="dbi:SQLite:myappTEST.db" prove --lib lib -v t/live_app01.t
310
64ccd8a8 311This will modify the DSN only while the test case is running. If you
312launch your normal application without the C<MYAPP_DSN> environment
313variable defined, it will default to the same C<dbi:SQLite:myapp.db> as
314before.
4d583dd8 315
cc548726 316
4d583dd8 317=head1 AUTHOR
318
319Kennedy Clark, C<hkclark@gmail.com>
320
eed93301 321Please report any errors, issues or suggestions to the author. The
322most recent version of the Catlayst Tutorial can be found at
323L<http://dev.catalyst.perl.org/repos/Catalyst/trunk/Catalyst-Runtime/lib/Catalyst/Manual/Tutorial/>.
4d583dd8 324
64ccd8a8 325Copyright 2006, Kennedy Clark, under Creative Commons License
326(L<http://creativecommons.org/licenses/by-nc-sa/2.5/>).
4d583dd8 327