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