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