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