ZenTest 3.7.2 → 3.8.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,532 @@
1
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
2
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3
+ <html xml:lang="en-US" lang="en-US" xmlns=
4
+ "http://www.w3.org/1999/xhtml">
5
+ <head>
6
+ <title>PH7 - Getting started with Autotest - Continuous
7
+ Testing</title>
8
+ <link href="Article.css" media="all" rel="Stylesheet" type="text/css" />
9
+ </head>
10
+ <body>
11
+ <div class="content">
12
+ <h1 style="text-align: center;">Getting started with Autotest -
13
+ Continuous Testing</h1>
14
+ <div id="abstract" class="Abstract">
15
+ <p class="first">Why manually run your tests, when the computer can
16
+ do it for you! <a href=
17
+ "http://www.zenspider.com/ZSS/Products/ZenTest/">Autotest</a> is a
18
+ great tool to speed up test-driven development with Ruby or Ruby on
19
+ Rails. Autotest makes your coding session even more productive as
20
+ <strong>it automatically runs a subset of your test suite each time
21
+ you change a file. Autotest is smart -- it figures out which subset
22
+ to run based on the files you've changed.</strong> Think of it as
23
+ <strong><q>Continuous Testing</q></strong>.</p>
24
+ <p>Autotest source code is well-documented (<a href=
25
+ "http://zentest.rubyforge.org/ZenTest/" title=
26
+ "ZenTest documentation">rdoc</a>) but finding a high level overview
27
+ online is a little more challenging. This article will get you up
28
+ and running in no time, so that you may concentrate on writing
29
+ code. <a href="getting_started_with_autotest">Let's get
30
+ started!</a></p>
31
+ </div>
32
+ <h2 id="why_autotest">Why Autotest?</h2>
33
+ <h3 id="continuous_testing">Continuous Testing</h3>
34
+ <p>The cool thing about Autotest is that you have <strong>instant
35
+ feedback on your code</strong> (tests run within a second). Even
36
+ better, <strong>the testing happens on its own</strong> so
37
+ <strong>you no longer have to switch back and forth</strong> from
38
+ the coding context to the testing context anymore (both wise
39
+ cognitively and from a <acronym title="User Interface">UI</acronym>
40
+ perspective). This effortless and immediate feedback on your code
41
+ as well as the automated and unattended test runs are quite similar
42
+ to the characteristics of <a href=
43
+ "http://www.martinfowler.com/articles/continuousIntegration.html"
44
+ title=
45
+ "Martin Fowler's legendary introduction to Continuous Integration">Continuous
46
+ Integration</a> at the team level. However, continuous integration
47
+ concentrates on improving <em>integration</em> at a <em>team</em>
48
+ level while Autotest concentrates on facilitating the
49
+ <em>development</em> for a <em>single</em> developer (or
50
+ programming-pair) <em>before</em> the code gets integrated -- hence
51
+ the term <strong><dfn>Continuous Testing</dfn></strong>.</p>
52
+ <p>As this is highly visual, <strong>have a look at Nuby on Rails'
53
+ <a href="http://topfunky.com/clients/blog/autotest-tm.mov">autotest
54
+ screencast</a></strong>.</p>
55
+ <h3 id="quicker_test_runs">Quicker Test Runs</h3>
56
+ <p>Autotest can also provide <strong>quicker test runs</strong>
57
+ than standard convention since it intelligently monitors the
58
+ changes and only runs the tests that are impacted by these changes.
59
+ In practice this is relevant only for <q>classic</q> Rails
60
+ applications because:</p>
61
+ <ul>
62
+ <li><strong>Rails conventions provide good heuristics</strong> for
63
+ Autotest to decide which tests to run when a file changes. If your
64
+ application does not stick to the classic Rails layout and naming
65
+ conventions the <q>magic</q> does not work so well anymore. In this
66
+ case, it is probably better to have Autotest <a href=
67
+ "getting_started_with_autotest#running_whole_test_suite">run your
68
+ whole test suite for all changes</a>.</li>
69
+ <li>There is value in running a <em>subset</em> of the whole test
70
+ suite <strong>only because running <q>classic</q> Rails unit tests
71
+ can be slow</strong>. This is mostly since Rails approach to unit
72
+ testing is quite unconventional and involves database access. This
73
+ approach goes against general <q>agile</q> wisdom that you should
74
+ make sure your unit tests run quickly, and do not have any
75
+ dependency for external systems. Also note that there are <a href=
76
+ "http://jayfields.blogspot.com/2006/06/ruby-on-rails-unit-tests.html">
77
+ well-documented ways</a> to have your Rails unit tests not depend
78
+ on the database and have them run blazing fast!</li>
79
+ </ul>
80
+ <h3 id="lack_of_proper_ide">Make Up for a Lack of a Proper Ruby
81
+ IDE</h3>
82
+ <p>Autotest can come in handy if your favorite IDE has limited Ruby
83
+ support, or if you prefer a more ligthweight development
84
+ environment (text editor + terminal + Autotest): it gives you an
85
+ easy and automated way to run your tests.</p>
86
+ <h2 id="install_autotest">Install Autotest</h2>
87
+ <h3>Make Sure You Already have RubyGem Installed</h3>
88
+ <p>The easiest way to install <code>Autotest</code> is to use the
89
+ <strong>ZenTest gem</strong>. If you have no idea of what a
90
+ <dfn>gem</dfn> is, or you have not installed the RubyGem packaging
91
+ system yet, please have a look at the <a href=
92
+ "http://rubygems.org/">RubyGem official website</a>. If you are
93
+ serious about Ruby development, it will be hard <em>not</em> to use
94
+ RubyGem.</p>
95
+ <h3 id="install_osx_ubuntu">OS X and Ubuntu</h3>
96
+ <p>On OS X or Ubuntu launch from a terminal:</p>
97
+ <pre class="command-box">
98
+ $ sudo gem install ZenTest
99
+ </pre>
100
+ <h3 id="install_other_unix">Other Unix flavors</h3>
101
+ <p>For other *Nix flavors, try</p>
102
+ <pre class="command-box">
103
+ $ suPassword:<span class=
104
+ "placeholder">Type root password here</span>$ gem install ZenTest
105
+ </pre>
106
+ <h2 id="run_autotest">Run autotest</h2>
107
+ <h3 id="run_rails">Ruby on Rails project</h3>
108
+ <p>Consistent with Rails principles, Autotest does not require any
109
+ configuration to run. Provided you follow classic Rails
110
+ conventions, Autotest will figure things out on its own.
111
+ <strong>Simply launch Autotest from the base directory of your Ruby
112
+ on Rails project.</strong></p>
113
+ <pre class="command-box">
114
+ $ cd <span class=
115
+ "placeholder">base directory of your Ruby on Rails project</span>$ autotest
116
+ </pre>
117
+ <p>Autotest will then run all your tests (the first time), and wait
118
+ for you to modify some code:</p>
119
+ <pre class="command-box">
120
+ $ autotest
121
+ /usr/bin/ruby1.8 -I.:lib:test -rtest/unit -e "%w[test/functional/tasks_controller_test.rb test/unit/quarter_test.rb test/unit/task_test.rb].each { |f| require f }" | unit_diff -u
122
+ Loaded suite -e
123
+ Started
124
+ .......................
125
+ Finished in 0.672928 seconds.
126
+
127
+ ================================================================================
128
+ <strong>23 tests, 60 assertions, 0 failures, 0 errors</strong>
129
+ </pre>
130
+ <p>Go ahead and <strong>modify some code in your project so that a
131
+ test fails.</strong> Save the modified file to disk and Autotest
132
+ will automatically rerun some of the tests:</p>
133
+ <pre class="command-box">
134
+ /usr/bin/ruby1.8 -I.:lib:test -rtest/unit -e "%w[test/functional/tasks_controller_test.rb test/unit/task_test.rb].each { |f| require f }" | unit_diff -u
135
+ Loaded suite -e
136
+ Started
137
+ ...F........
138
+ Finished in 0.42272 seconds.
139
+
140
+ 1) Failure:
141
+ test_should_be_found(TaskTest) [<strong>./test/unit/task_test.rb:22</strong>]:
142
+ --- /tmp/diff6647.0 2006-11-15 20:46:43.000000000 -0800
143
+ +++ /tmp/diff6647.1 2006-11-15 20:46:43.000000000 -0800
144
+ @@ -1 +1 @@
145
+ <strong>
146
+ -Expected result
147
+ +Actual result</strong>
148
+
149
+ ================================================================================
150
+ <strong>4 tests, 9 assertions, 1 failures, 0 errors</strong>
151
+ </pre>
152
+ <p>Note that autotest ran only a <em>subset</em> of your test suite
153
+ this time (4 tests out of 23 in my case). Also note that
154
+ <strong>Autotest is especially good in providing brief and relevant
155
+ feedback on the test failures</strong>.</p>
156
+ <p>Autotest focuses on running previous failures until you have
157
+ fixed them. So <strong>test failures are run until they have all
158
+ passed. Then the full test suite is run</strong> to ensure that
159
+ nothing else was inadvertently broken.</p>
160
+ <h3 id="run_ruby">Ruby Project</h3>
161
+ <p>In theory you would run Autotest the same way as you would for
162
+ any Ruby project -- even if it is not based on Rails:</p>
163
+ <pre class="command-box">
164
+ $ cd <span class=
165
+ "placeholder">base directory of your Ruby project</span>$ autotest
166
+ </pre>
167
+ <p>In practice, Autotest might have problems finding your tests or
168
+ figuring out which tests to run when you change some code. If this
169
+ is the case, take a look at the <a href=
170
+ "getting_started_with_autotest#troubleshoot_test_detection"><q>troubleshooting
171
+ test detection</q></a> section.</p>
172
+ <h3 id="full_test_run">Forcing a Full Test Run and Stopping
173
+ Autotest</h3>
174
+ <p><strong>If you want to force Autotest to run the <em>entire</em>
175
+ test suite</strong> <strong>hit <kbd>Ctrl - C</kbd> once</strong>
176
+ in the terminal running Autotest. Hitting <kbd>Ctrl - C</kbd> twice
177
+ will stop Autotest.</p>
178
+ <h2 id="configure_plugins">Configure Plugins</h2>
179
+ <p>Autotest also provides some cool plugins that enable you to
180
+ <strong>get feedback the way you want</strong>.</p>
181
+ <h3 id="create_dot_autotest">Create a <code>.autotest</code>
182
+ file</h3>
183
+ <p><strong>You configure plugins by creating a
184
+ <code>.autotest</code> file in your project base
185
+ directory</strong>. You can also provide a default configuration
186
+ for all your projects by creating a <code>.autotest</code> file in
187
+ your home directory (when present, project configuration files
188
+ override user default configuration file).</p>
189
+ <p><strong>You enable a plugin by adding a line requiring the
190
+ plugin in the <code>.autotest</code> file</strong>. For instance,
191
+ to enable the <q>Growl</q> plugin, you would add the following
192
+ line:</p>
193
+ <pre class="source-code-box">
194
+ require 'autotest/growl'
195
+ </pre>
196
+ <p>Below you will find a description of the most popular plugins
197
+ and how to enable them.</p>
198
+ <h3 id="red_green_plugin">Red / Green Plugin</h3>
199
+ <p>The <q>Red / Green</q> plugin provides color to
200
+ <code>Autotest</code> messages in the terminal window. As expected,
201
+ output is green if all the tests pass, red if some test fails.
202
+ Having red / green visual output makes it easier for one to scan
203
+ the output and quickly determine whether everything is OK or
204
+ something went wrong.</p>
205
+ <p>Visually, the <q>Red /Green</q> plugin turns</p>
206
+ <pre class="command-box">
207
+ ================================================================================
208
+ 200 tests, 520 assertions, 0 failures, 0 errors
209
+ </pre>
210
+ <p>into</p>
211
+ <pre class="command-box" style="color: green;">
212
+ ================================================================================
213
+ 200 tests, 520 assertions, 0 failures, 0 errors
214
+ </pre>
215
+ <p>or</p>
216
+ <pre class="command-box" style="color: red;">
217
+ ================================================================================
218
+ 5 tests, 20 assertions, 1 failures, 0 errors
219
+ </pre>
220
+ <p>To enable the <q>Red / Green</q> plugin add the following line
221
+ to your <code>.autotest</code> file:</p>
222
+ <pre class="source-code-box">
223
+ require 'autotest/redgreen'
224
+ </pre>
225
+ <h3 id="desktop_notification_plugins">Desktop Notification
226
+ Plugins</h3>
227
+ <p>You might not even have to look at <code>the Autotest</code>
228
+ terminal output to figure out the result of a test run.
229
+ <strong>Several plugins provide desktop notification messages
230
+ capabilities</strong>. In this way you can run Autotest in the
231
+ background and see popup messages when something fails.</p>
232
+ <h4 id="growl_plugin">Growl Plugin (OS X)</h4>
233
+ <p><a href="http://growl.info/">Growl</a> is a popular desktop
234
+ notification system for OSX. If you are developing on a Mac, enable
235
+ the Growl plugin by adding</p>
236
+ <pre class="source-code-box">
237
+ require 'autotest/growl'
238
+ </pre>
239
+ <p>to your <code>.autotest</code> file. Note that for this plugin
240
+ to work, you not only need to install <a href="http://growl.info/"
241
+ title="Growl website">Growl</a> <strong>but also its command line
242
+ interface: <a href=
243
+ "http://growl.info/documentation/growlnotify.php">growlnotify</a>.</strong></p>
244
+ <h4 id="snarl_plugin">Snarl Plugin (Windows)</h4>
245
+ <p><a href="http://www.fullphat.net/">Snarl</a> is a notification
246
+ system for Windows largely inspired by Growl. Autotest will use it
247
+ if you enable the Snarl plugin in your <code>.autotest</code>
248
+ file:</p>
249
+ <pre class="source-code-box">
250
+ require 'autotest/snarl'
251
+ </pre>
252
+ <h4 id="kde_notify_plugin">KDE Notify Plugin (Linux)</h4>
253
+ <p>If you are running Linux and use KDE as your desktop
254
+ environment, you will get desktop notification by adding to your
255
+ <code>.autotest</code> file:</p>
256
+ <pre class="source-code-box">
257
+ require 'autotest/kdenotify'
258
+ </pre>
259
+ <h4 id="gnome_notify_plugin">Gnome Notify plugin (Linux)</h4>
260
+ <p>If you are running Linux and use Gnome as your desktop
261
+ environment, unfortunately there is no official plugin for desktop
262
+ notification. You can still get desktop notifications by adding the
263
+ following code snipet in your <code>.autotest</code> file:</p>
264
+ <pre class="source-code-box">
265
+ module Autotest::GnomeNotify
266
+
267
+ # Time notification will be displayed before disappearing automatically
268
+ EXPIRATION_IN_SECONDS = 2
269
+ ERROR_STOCK_ICON = "gtk-dialog-error"
270
+ SUCCESS_STOCK_ICON = "gtk-dialog-info"
271
+
272
+ # Convenience method to send an error notification message
273
+ #
274
+ # [stock_icon] Stock icon name of icon to display
275
+ # [title] Notification message title
276
+ # [message] Core message for the notification
277
+ def self.notify stock_icon, title, message
278
+ options = "-t #{EXPIRATION_IN_SECONDS * 1000} -i #{stock_icon}"
279
+ system "notify-send #{options} '#{title}' '#{message}'"
280
+ end
281
+
282
+ Autotest.add_hook :red do |at|
283
+ notify ERROR_STOCK_ICON, "Tests failed", "#{at.files_to_test.size} tests failed"
284
+ end
285
+
286
+ Autotest.add_hook :green do |at|
287
+ notify SUCCESS_STOCK_ICON, "All tests passed, good job!", ""
288
+ end
289
+
290
+ end
291
+ </pre>
292
+ <p>For this to work <strong>you need to have <a href=
293
+ "http://trac.galago-project.org/wiki/DesktopNotifications">libnotify</a>
294
+ installed on your system and a program named
295
+ <code>notify-send</code> in your <code>PATH</code></strong>. For
296
+ most Linux distributions this simply means that you should install
297
+ the <code>libnotify-bin</code> package. If you are running Ubuntu,
298
+ run</p>
299
+ <pre class="command-box">
300
+ sudo apt-get install libnotify-bin
301
+ </pre>
302
+ <h3 id="pretty_plugin">Pretty Plugin</h3>
303
+ <p>If you are running Autotest on a Mac you can enable the
304
+ <q>pretty</q> plugin to <strong>visualize your Autotest status
305
+ history</strong> as a sequence of red and green squares:</p>
306
+ <p class="figure"><img alt="Autotest Pretty Plugin Screenshot" src=
307
+ "http://blog.zenspider.com/img/autotest_pretty.png" /></p>
308
+ <p>Of course a green square indicates passing tests while a red
309
+ square signals some test failures. If you want to get a feel of
310
+ what a session is like with this plugin enabled, ZenSpider has a
311
+ <a href=
312
+ "http://vanity.zenspider.com.nyud.net:8090/~ryan/autotest_plugins.mov"
313
+ title="ZenSpider pretty plugin video">nice video</a> demonstrating
314
+ the pretty plugin in action.</p>
315
+ <p>To enable this plugin <strong>you will need to install <a href=
316
+ "http://rubycocoa.sourceforge.net/doc/" title=
317
+ "Ruby Cocoa documentation">RubyCocoa</a></strong> and add the
318
+ folowing line to your <code>.autotest</code> file:</p>
319
+ <pre class="source-code-box">
320
+ require 'autotest/pretty'
321
+ </pre>
322
+ <h3 id="html_report_plugin">HTML Report Plugin</h3>
323
+ <p>The <q>HTML report</q> plugin <strong>publishes most recent
324
+ Autotest statuses as a web page</strong> under
325
+ <code>~/Sites/autotest.html</code>. You can then point a browser to
326
+ this page and see something like:</p>
327
+ <div class="sample-box">
328
+ <div style="COLOR:green">Sat Feb 03 14:34:09 PST 2007: 0</div>
329
+ <div style="COLOR:red">Sat Feb 03 14:33:50 PST 2007: 1</div>
330
+ <div style="COLOR:green">Sat Feb 03 14:33:45 PST 2007: 0</div>
331
+ <div style="COLOR:red">Sat Feb 03 14:33:29 PST 2007: 4</div>
332
+ <div style="COLOR:green">Sat Feb 03 14:33:19 PST 2007: 0</div>
333
+ </div>
334
+ <p>Of course the content of the web page is updated while you work.
335
+ Note that <strong>you need to have an an existing
336
+ <code>~/Sites</code> directory</strong> <em>before</em> you enable
337
+ the plugin with the following line:</p>
338
+ <pre class="source-code-box">
339
+ require 'autotest/html_report'
340
+ </pre>
341
+ <h3 id="menu_plugin">Menu Plugin</h3>
342
+ <p>Remember that, by default, hitting <code>Ctrl - C</code> once
343
+ will force Autotest to run the entire test suite, and hitting
344
+ <code>Ctrl - C</code> twice will stop Autotest? The <q>menu</q>
345
+ plugin changes this behavior: each time you hit <code>Ctrl -
346
+ C</code> it will explicitly ask you whether you want to quit,
347
+ restart or just keep going.</p>
348
+ <pre class="command-box">
349
+ c: continue q: quit r: restart menu&gt;
350
+ </pre>
351
+ <p>Enable the menu plugin by adding the following line to your
352
+ <code>.autotest</code> file.</p>
353
+ <pre class="source-code-box">
354
+ require 'autotest/menu'
355
+ </pre>
356
+ <h3 id="timestamp_plugin">Timestamp Plugin</h3>
357
+ <p>While Autotest waits for you to save a file, the timestamp
358
+ plugin prints a message with the current time. Messages look
359
+ like:</p>
360
+ <pre class="command-box">
361
+ # waiting... Sat Feb 03 15:56:23 EST 2007
362
+ </pre>
363
+ <p>To enable the timestamp plugin add the following to your
364
+ <code>.autotest</code> file:</p>
365
+ <pre class="source-code-box">
366
+ require 'autotest/timestamp'
367
+ </pre>
368
+ <h3>Getting More Information</h3>
369
+ <p>Your Autotest install comes with a sample <code>.autotest</code>
370
+ file listing all available plugins. It is named
371
+ <code>example_dot_autotest.rb</code>. You will find it in the gems
372
+ install directory. Most likely this directory will look like:</p>
373
+ <ul>
374
+ <li><code>/usr/local/lib/ruby/gems/1.8/gems/ZenTest-3.4.3/</code>
375
+ on OS X.</li>
376
+ <li><code>/usr/lib/ruby/gems/1.8/gems/ZenTest-3.4.3/</code> on
377
+ other Unix platforms</li>
378
+ </ul>
379
+ <p>Interesting plugins that are not distributed with Autotest can
380
+ also be found within <a href=
381
+ "http://rubyforge.org/tracker/?atid=1680&amp;group_id=419&amp;func=browse">
382
+ autotest pending patches</a>. The <a href=
383
+ "http://rspec.rubyforge.org/" title=
384
+ "RSpec project website">RSpec</a> patches you will find there will
385
+ be of particular interest to those of you that enjoy <a href=
386
+ "http://behaviour-driven.org/" title=
387
+ "Official Behavior Driven Development website">behavior-driven
388
+ development</a>.</p>
389
+ <h2 id="troubleshoot_test_detection">Troubleshooting Autotest Test
390
+ Detection</h2>
391
+ <p>Whether Autotest does not work out of the box for you or its
392
+ magics eludes you, it is always good to get some understanding of
393
+ the heuristics that Autotest uses to figure which test(s) to
394
+ run.</p>
395
+ <h3 id="rails_heuristics">Heuristics for Rails</h3>
396
+ <p>Autotest automatically discovers Ruby on Rails projects by
397
+ checking for a <code>config/environment.rb</code> file. If there is
398
+ one, Autotest will base its logic on standard Rails file mappings
399
+ and conventions.</p>
400
+ <p>If for some reason you want to force Ruby on Rails mode you can
401
+ always launch Autotest it with the <code>-rails</code> option:</p>
402
+ <pre class="command-box">
403
+ $ autotest -rails
404
+ </pre>
405
+ <p>A <em>simplified</em> version of Autotest heuristics in this
406
+ mode would be:</p>
407
+ <ul>
408
+ <li>When changing a test file, only this file is run (e.g.
409
+ <code>test/unit/foo_test.rb</code> &rarr;
410
+ <code>test/unit/foo_test.rb</code>).</li>
411
+ <li>When changing a model file, only associated unit test file is
412
+ run (e.g. <code>app/models/foo.rb</code> &rarr;
413
+ <code>test/unit/foo_test.rb</code>).</li>
414
+ <li>When changing a controller file, associated functional test
415
+ file is run (e.g. <code>app/controllers/foo_controller.rb</code>
416
+ &rarr; <code>test/functional/foo_controller_test.rb</code>).</li>
417
+ <li>When changing a fixture file, associated unit test and
418
+ functional test are run (e.g. <code>app/fixtures/foos.yml</code>
419
+ &rarr; <code>test/unit/foo_test.rb</code> +
420
+ <code>test/functional/foo_controller_test.rb</code>).</li>
421
+ <li>When changing a helper file, associated functional test file is
422
+ run (e.g. <code>app/helpers/foo_helper.rb</code> &rarr;
423
+ <code>test/functional/foo_controller_test.rb</code>).</li>
424
+ <li>When changing <code>application_helper.rb</code> file all
425
+ functional test files are run (e.g.
426
+ <code>application_helper.rb</code> &rarr;
427
+ <code>test/functional/*_test.rb</code>).</li>
428
+ <li>When changing a file under the <code>config</code> directory,
429
+ all tests are run.</li>
430
+ </ul>
431
+ <p>You've got the idea. Actual heuristics are a little more complex
432
+ and also handle the concept of <code>view</code> and
433
+ <code>controller</code> tests. <strong>For a more thourough
434
+ understanding have look at the <code>rails_autotest.rb</code>
435
+ file</strong> in ZenTest gem install directory.</p>
436
+ <p>In case these heuristics do not play well with your own
437
+ conventions, do not give up yet: <strong>you can always configure
438
+ Autotest to <a href=
439
+ "getting_started_with_autotest#running_whole_test_suite">run the
440
+ whole test suite for all changes</a></strong>.</p>
441
+ <h3 id="ruby_heuristics">Heuristics for Non Rails Projects</h3>
442
+ <p>For non Rails project, Autotest uses a simple naming scheme to
443
+ map implementation files to test files:</p>
444
+ <ul>
445
+ <li>Test files must be stored in the <code>test</code>
446
+ directory</li>
447
+ <li>Implementation files must be stored in the <code>lib</code>
448
+ directory</li>
449
+ <li>Test files names must start with <code>test_</code></li>
450
+ <li>Test class names must start with <code>Test</code></li>
451
+ <li>Test files corresponding to a specific implementation file must
452
+ be named <code>test_*<span class="placeholder">name of
453
+ implementation file</span>.rb</code></li>
454
+ </ul>
455
+ <p>If you can live with these conventions, Autotest will work
456
+ out-of-the-box for you. If these conventions are not your cup of
457
+ tea and you have your own, the next paragraph explains how to
458
+ configure Autotest so that it runs the whole test suite each time
459
+ you save a file.</p>
460
+ <h3 id="running_whole_test_suite">Running the Whole Test Suite for
461
+ All Changes</h3>
462
+ <p>If for some reason Autotest heuristics do not work for you, you
463
+ can customize them in your <code>.autotest</code> file with a
464
+ little bit of work.</p>
465
+ <p>For instance, if your entire test suite runs quickly (as it
466
+ should), you can easily configure Autotest to run the whole test
467
+ suite for any change by adding the following code to your
468
+ <code>.autotest</code> file:</p>
469
+ <pre class="command-box">
470
+ #
471
+ # Override autotest default magic deciding which test to run when
472
+ # a file is changed : enable more flexible naming conventions
473
+ # trading some of the efficiency: we rerun all the tests each time.
474
+ #
475
+ class Autotest
476
+
477
+ def tests_for_file(filename)
478
+ return Dir["test/**/*.rb"]
479
+ end
480
+
481
+ end
482
+ </pre>
483
+ <h2>Conclusion</h2>
484
+ <p>Autotest provides an easy and effortless way to run your tests:
485
+ just save a file. This is a great way to get quick feedback on your
486
+ code and avoid any context switch. Autotest automated test runs are
487
+ also extremelly valuable if your favorite IDE has poor Ruby
488
+ support, or if you prefer a more ligthweight development
489
+ environment (text editor + terminal + Autotest).</p>
490
+ <p>Autotest also tries hard to be smart on deciding which tests to
491
+ run:</p>
492
+ <ul>
493
+ <li>It only runs the tests affected by your latest code
494
+ changes.</li>
495
+ <li>When some tests fail, Autotest focuses on running previous
496
+ failures until you have fixed them. Once they pass, the full test
497
+ suite is run to ensure that nothing else was inadvertently
498
+ broken.</li>
499
+ </ul>
500
+ <p>On deciding which tests to run, Autotest <q>magic</q> works out
501
+ of the box if your application follows classic Ruby on Rails
502
+ conventions. If this is not your cup of tea, it is extremely easy
503
+ to customize Autotest to fit your conventions.</p>
504
+ <p>Via its plugins Autotest also offers a lot of interesting
505
+ feedback options, from terminal output to html publishing to
506
+ desktop notifications. The pretty plugin offers a highly visual
507
+ representation of your test runs history, which could be useful
508
+ when teaching Test Driven Development (green, red, green, red,
509
+ ...).</p>
510
+ <p>On the flip side, It is important to note that Autotest does not
511
+ fit all developement styles: Some developers like better control on
512
+ which tests they are running. While working on a piece of code,
513
+ they will typically focuss on a few tests (which they know they
514
+ could have broken), and then run the whole test suite just before
515
+ committing. Autotest emulates this as well as it can with his focus
516
+ on running previous failures; but ultimately a human will always
517
+ have a better intuition, especially if your project does not follow
518
+ classic Rails conventions.</p>
519
+ <p>In all cases, it is worth spending some time playing with
520
+ Autotest and experiment with its innovative, lightweight and
521
+ effortless approach to test runs, what I have been calling
522
+ <q>continuous testing</q>.</p>
523
+ </div>
524
+ <p class="Copyright">&copy; Copyright Philippe Hanrigou, all rights
525
+ reserved.</p>
526
+ <p class="License">This work is licensed under a <a rel="license"
527
+ href="http://creativecommons.org/licenses/by/2.5/">Creative Commons
528
+ Attribution 2.5 License</a>.</p>
529
+ <p class="License">Used and distributed with permission from
530
+ Philippe Hanrigou.</p>
531
+ </body>
532
+ </html>