1Catalyst::Manual::TutorUisaelr::C0o8n_tTreisbtuitnegdC(a3Pt)earllysDto:c:uMmaennutaalt:i:oTnutorial::08_Testing(3)
2
3
4

NAME

6       Catalyst::Manual::Tutorial::08_Testing - Catalyst Tutorial - Chapter 8:
7       Testing
8

OVERVIEW

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

DESCRIPTION

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       You can check out the source code for this example from the Catalyst
43       Subversion repository as per the instructions in
44       Catalyst::Manual::Tutorial::01_Intro.
45
46       For an excellent introduction to learning the many benefits of testing
47       your Perl applications and modules, you might want to read 'Perl
48       Testing: A Developer's Notebook' by Ian Langworth and chromatic.
49

RUNNING THE "CANNED" CATALYST TESTS

51       There are a variety of ways to run Catalyst and Perl tests (for
52       example, "perl Makefile.PL" and "make test"), but one of the easiest is
53       with the "prove" command.  For example, to run all of the tests in the
54       "t" directory, enter:
55
56           $ prove -wl t
57
58       There will be a lot of output because we have the "-Debug" flag enabled
59       in "lib/MyApp.pm" (see the "CATALYST_DEBUG=0" tip below for a quick and
60       easy way to reduce the clutter).  Look for lines like this for errors:
61
62           #   Failed test 'Request should succeed'
63           #   at t/controller_Books.t line 8.
64           # Looks like you failed 1 test of 3.
65
66       The redirection used by the Authentication plugins will cause several
67       failures in the default tests.  You can fix this by making the
68       following changes:
69
70       1) Change the line in "t/01app.t" that reads:
71
72           ok( request('/')->is_success, 'Request should succeed' );
73
74       to:
75
76           ok( request('/login')->is_success, 'Request should succeed' );
77
78       2) Change the line in "t/controller_Logout.t" that reads:
79
80           ok( request('/logout')->is_success, 'Request should succeed' );
81
82       to:
83
84           ok( request('/logout')->is_redirect, 'Request should succeed' );
85
86       3) Change the line in "t/controller_Books.t" that reads:
87
88           ok( request('/books')->is_success, 'Request should succeed' );
89
90       to:
91
92           ok( request('/books')->is_redirect, 'Request should succeed' );
93
94       4) Add the following statement to the top of "t/view_TT.t":
95
96           use MyApp;
97
98       As you can see in the "prove" command line above, the "--lib" option is
99       used to set the location of the Catalyst "lib" directory.  With this
100       command, you will get all of the usual development server debug output,
101       something most people prefer to disable while running tests cases.
102       Although you can edit the "lib/MyApp.pm" to comment out the "-Debug"
103       plugin, it's generally easier to simply set the "CATALYST_DEBUG=0"
104       environment variable.  For example:
105
106           $ CATALYST_DEBUG=0 prove -wl t
107
108       During the "t/02pod" and "t/03podcoverage" tests, you might notice the
109       "all skipped: set TEST_POD to enable this test" warning message.  To
110       execute the Pod-related tests, add "TEST_POD=1" to the "prove" command:
111
112           $ CATALYST_DEBUG=0 TEST_POD=1 prove -wl t
113
114       If you omitted the Pod comments from any of the methods that were
115       inserted, you might have to go back and fix them to get these tests to
116       pass. :-)
117
118       Another useful option is the "verbose" ("-v") option to "prove".  It
119       prints the name of each test case as it is being run:
120
121           $ CATALYST_DEBUG=0 TEST_POD=1 prove -vwl t
122

RUNNING A SINGLE TEST

124       You can also run a single script by appending its name to the "prove"
125       command. For example:
126
127           $ CATALYST_DEBUG=0 prove -wl t/01app.t
128
129       Also note that you can also run tests directly from Perl without
130       "prove".  For example:
131
132           $ CATALYST_DEBUG=0 perl -w -Ilib t/01app.t
133

ADDING YOUR OWN TEST SCRIPT

135       Although the Catalyst helper scripts provide a basic level of checks
136       "for free," testing can become significantly more helpful when you
137       write your own script to exercise the various parts of your
138       application.  The Test::WWW::Mechanize::Catalyst module is very popular
139       for writing these sorts of test cases.  This module extends
140       Test::WWW::Mechanize (and therefore WWW::Mechanize) to allow you to
141       automate the action of a user "clicking around" inside your
142       application.  It gives you all the benefits of testing on a live system
143       without the messiness of having to use an actual web server, and a real
144       person to do the clicking.
145
146       To create a sample test case, open the "t/live_app01.t" file in your
147       editor and enter the following:
148
149           #!/usr/bin/perl
150
151           use strict;
152           use warnings;
153           use Test::More;
154
155           # Need to specify the name of your app as arg on next line
156           # Can also do:
157           #   use Test::WWW::Mechanize::Catalyst "MyApp";
158
159           use ok "Test::WWW::Mechanize::Catalyst" => "MyApp";
160
161           # Create two 'user agents' to simulate two different users ('test01' & 'test02')
162           my $ua1 = Test::WWW::Mechanize::Catalyst->new;
163           my $ua2 = Test::WWW::Mechanize::Catalyst->new;
164
165           # Use a simplified for loop to do tests that are common to both users
166           # Use get_ok() to make sure we can hit the base URL
167           # Second arg = optional description of test (will be displayed for failed tests)
168           # Note that in test scripts you send everything to 'http://localhost'
169           $_->get_ok("http://localhost/", "Check redirect of base URL") for $ua1, $ua2;
170           # Use title_is() to check the contents of the <title>...</title> tags
171           $_->title_is("Login", "Check for login title") for $ua1, $ua2;
172           # Use content_contains() to match on text in the html body
173           $_->content_contains("You need to log in to use this application",
174               "Check we are NOT logged in") for $ua1, $ua2;
175
176           # Log in as each user
177           # Specify username and password on the URL
178           $ua1->get_ok("http://localhost/login?username=test01&password=mypass", "Login 'test01'");
179           # Could make user2 like user1 above, but use the form to show another way
180           $ua2->submit_form(
181               fields => {
182                   username => 'test02',
183                   password => 'mypass',
184               });
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 (see also 'text_regex' and 'url_regex' options)
193           $_->follow_link_ok({n => 4}, "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\">User Logout</a>",
211               "Both users should have a 'User Logout'") for $ua1, $ua2;
212           $ua1->content_contains("/books/form_create\">Admin Create</a>",
213               "'test01' should have a create link");
214           $ua2->content_lacks("/books/form_create\">Admin Create</a>",
215               "'test02' should NOT have a create link");
216
217           $ua1->get_ok("http://localhost/books/list", "View book list as 'test01'");
218
219           # User 'test01' should be able to create a book with the "formless create" URL
220           $ua1->get_ok("http://localhost/books/url_create/TestTitle/2/4",
221               "'test01' formless create");
222           $ua1->title_is("Book Created", "Book created title");
223           $ua1->content_contains("Added book 'TestTitle'", "Check title added OK");
224           $ua1->content_contains("by 'Stevens'", "Check author added OK");
225           $ua1->content_contains("with a rating of 2.", "Check rating added");
226           # Try a regular expression to combine the previous 3 checks & account for whitespace
227           $ua1->content_like(qr/Added book 'TestTitle'\s+by 'Stevens'\s+with a rating of 2./, "Regex check");
228
229           # Make sure the new book shows in the list
230           $ua1->get_ok("http://localhost/books/list", "'test01' book list");
231           $ua1->title_is("Book List", "Check logged in and at book list");
232           $ua1->content_contains("Book List", "Book List page test");
233           $ua1->content_contains("TestTitle", "Look for 'TestTitle'");
234
235           # Make sure the new book can be deleted
236           # Get all the Delete links on the list page
237           my @delLinks = $ua1->find_all_links(text => 'Delete');
238           # Use the final link to delete the last book
239           $ua1->get_ok($delLinks[$#delLinks]->url, 'Delete last book');
240           # Check that delete worked
241           $ua1->content_contains("Book List", "Book List page test");
242           $ua1->content_contains("Book deleted", "Book was deleted");
243
244           # User 'test02' should not be able to add a book
245           $ua2->get_ok("http://localhost/books/url_create/TestTitle2/2/5", "'test02' add");
246           $ua2->content_contains("Unauthorized!", "Check 'test02' cannot add");
247
248           done_testing;
249
250       The "live_app.t" test cases uses copious comments to explain each step
251       of the process.  In addition to the techniques shown here, there are a
252       variety of other methods available in Test::WWW::Mechanize::Catalyst
253       (for example, regex-based matching). Consult the documentation for more
254       detail.
255
256       TIP: For unit tests vs. the "full application tests" approach used by
257       Test::WWW::Mechanize::Catalyst, see Catalyst::Test.
258
259       Note: The test script does not test the "form_create" and
260       "form_create_do" actions.  That is left as an exercise for the reader
261       (you should be able to complete that logic using the existing code as a
262       template).
263
264       To run the new test script, use a command such as:
265
266           $ CATALYST_DEBUG=0 prove -vwl t/live_app01.t
267
268       or
269
270           $ DBIC_TRACE=0 CATALYST_DEBUG=0 prove -vwl t/live_app01.t
271
272       Experiment with the "DBIC_TRACE", "CATALYST_DEBUG" and "-v" settings.
273       If you find that there are errors, use the techniques discussed in the
274       "Catalyst Debugging" section (Chapter 7) to isolate and fix any
275       problems.
276
277       If you want to run the test case under the Perl interactive debugger,
278       try a command such as:
279
280           $ DBIC_TRACE=0 CATALYST_DEBUG=0 perl -d -Ilib t/live_app01.t
281
282       Note that although this tutorial uses a single custom test case for
283       simplicity, you may wish to break your tests into different files for
284       better organization.
285
286       TIP: If you have a test case that fails, you will receive an error
287       similar to the following:
288
289           #   Failed test 'Check we are NOT logged in'
290           #   in t/live_app01.t at line 31.
291           #     searched: "\x{0a}<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Tran"...
292           #   can't find: "You need to log in to use this application."
293
294       Unfortunately, this only shows us the first 50 characters of the HTML
295       returned by the request -- not enough to determine where the problem
296       lies.  A simple technique that can be used in such situations is to
297       temporarily insert a line similar to the following right after the
298       failed test:
299
300           diag $ua1->content;
301
302       This will cause the full HTML returned by the request to be displayed.
303
304       Another approach to see the full HTML content at the failure point in a
305       series of tests would be to insert a ""$DB::single=1;" right above the
306       location of the failure and run the test under the perl debugger (with
307       "-d") as shown above.  Then you can use the debugger to explore the
308       state of the application right before or after the failure.
309

SUPPORTING BOTH PRODUCTION AND TEST DATABASES

311       You may wish to leverage the techniques discussed in this tutorial to
312       maintain both a "production database" for your live application and a
313       "testing database" for your test cases.  One advantage to
314       Test::WWW::Mechanize::Catalyst is that it runs your full application;
315       however, this can complicate things when you want to support multiple
316       databases.  One solution is to allow the database specification to be
317       overridden with an environment variable.  For example, open
318       "lib/MyApp/Model/DB.pm" in your editor and change the
319       "__PACKAGE__->config(..." declaration to resemble:
320
321           my $dsn = $ENV{MYAPP_DSN} ||= 'dbi:SQLite:myapp.db';
322           __PACKAGE__->config(
323               schema_class => 'MyApp::Schema',
324
325               connect_info => {
326                   dsn => $dsn,
327                   user => '',
328                   password => '',
329                   on_connect_do => q{PRAGMA foreign_keys = ON},
330               }
331           );
332
333       Then, when you run your test case, you can use commands such as:
334
335           $ cp myapp.db myappTEST.db
336           $ CATALYST_DEBUG=0 MYAPP_DSN="dbi:SQLite:myappTEST.db" prove -vwl t/live_app01.t
337
338       This will modify the DSN only while the test case is running.  If you
339       launch your normal application without the "MYAPP_DSN" environment
340       variable defined, it will default to the same "dbi:SQLite:myapp.db" as
341       before.
342

AUTHOR

344       Kennedy Clark, "hkclark@gmail.com"
345
346       Please report any errors, issues or suggestions to the author.  The
347       most recent version of the Catalyst Tutorial can be found at
348       http://dev.catalyst.perl.org/repos/Catalyst/Catalyst-Manual/5.80/trunk/lib/Catalyst/Manual/Tutorial/
349       <http://dev.catalyst.perl.org/repos/Catalyst/Catalyst-
350       Manual/5.80/trunk/lib/Catalyst/Manual/Tutorial/>.
351
352       Copyright 2006-2008, Kennedy Clark, under Creative Commons License
353       (http://creativecommons.org/licenses/by-sa/3.0/us/
354       <http://creativecommons.org/licenses/by-sa/3.0/us/>).
355
356
357
358perl v5.12.0                      2010C-a0t2a-l1y7st::Manual::Tutorial::08_Testing(3)
Impressum