Friday, April 9, 2010

Getting to TDD for Rails Developers on a Mac

Do you need to be convinced how beneficial it is to have a good suite of continuously running tests being developed alongside your application?  Think less uncertainty, less anxiety, higher quality, less rework, better adaptability to external factors such as version dependencies...etc, etc.  Of course its not panacea or remover of what is interesting and challenging about writing software, but the list of benefits of discipline in this area is enormous, if not in number, at least in value to the project and to your peace of mind.

Getting started in TDD is the hardest thing tho, especially if you're a bit long in the tooth as a developer like me and haven't been brought up starting a project that way.  Or if for some reason you think that coding is a totally creative exercise like painting that involves no more that broad brush strokes of cleverness applied to a wonderfully private white canvas whose purpose is to express to the world how wonderful you are.

I'm not questioning how wonderful you are by the way :-).  That is a given, however you spend your time.  But I charge that TDD will improve the quality of your programming output, so here high level strategy for beginning to  incorporate it into your Rails development workflow.

You will need:-
- to be a Rails developer on a Mac
- an app with no tests or only tests that you know to work (if you have non-working tests, move them away for now)
- This article for instructions on how to configure autotest for 1) Your environment, 2) an app
- Growl installed on your mac

STEP 1 - Rails Installed

Ensure Rails is installed of course.  This is documented all over the Internet and you will hopefully have Rails installed already as it comes installed with Leopard.  I recommend installing it under RVM (Ruby Version Manager) though for better flexibility and safety.  There is an excellent screen cast by Ryan Bates for installing RVM with Rails 3 here.

STEP 2 - Ensure no failing tests

You will presumably be starting this on some Rails app.  I would recommend that you now move all tests out of that app for now, just so that you don't get confused by any failing as you set up your test infrastructure.  The simplest way to do this may be to use Finder to drag the contents of the app's test folder onto your desktop or somewhere else temporary and easy to get to on your system.

STEP 3 - Install the test infrastructure

As mentioned earlier, this article by Philippe Hanrigou gives the best and most complete instructions I have seen for installing AutoTest.  Follow it carefully and you will have a personalized setup that continuously runs all tests in the current app as soon you type "autotest" from the root folder of the app, notifying you of test results thru Growl.  Since you moved all tests away from the current app in the previous step, no tests will run yet, but we're about to change that.

STEP 4 - Re-install the existing tests (or create them) gradually

Starting with the Unit Tests carefully reinstate each test or test file that you extracted in step 3, making sure they work.  If Step 4 went well, every change you make to the test files and folders during this process will trigger your tests to run and show you the results of these via Growl.  

Be warned that the algorithm used by AutoTest to detect changes and rerun relevant tests is not perfect and you may need to trigger the tests manually (by pressing Ctrl-C in the Terminal shell in which the tests are running) from time to time.

STEP 5 - Develop with stress-free(ish) discipline

Once all of your existing tests are back and working and the AutoTest infrastructure is in place, every change you make to your source in your Rails app should trigger relevant tests.  Promise yourself that your tests will always stay running as you work on any app and will run through without errors.  Try to develop tests for at least all critical code as you complete/enhance/repair your app and enjoy a new sense of trust in your code and the quality of your work.

I hope this is useful to you.

No comments:

Post a Comment

Followers