Rake is my new F5

Posted by jeff Friday, January 11, 2008 16:47:00 GMT

rake

Photo credit

The last couple of days I’ve been doing a bunch of refactoring, as I tend to do whenever I’ve reached a kind of plateau on what I’m working on. It’s always good, once things work, to go back and improve the design.

I try to write tests before I write code, so that I’m guaranteed to have tests for every feature. I can run those tests at will to make sure my code works. If I couldn’t do that, I’d be much more reluctant to change anything once the code works, for fear I’ll break something important but not really know it until it’s too late.

But it’s funny how old habits – like pressing F5 in Visual Studio 2005 every time I made a change, just to make sure the app still compiled and worked ok – die hard. Really hard. After I made all these changes in my Rails app, my first instinct was to “run” it and make sure nothing broke. Because Ruby does not have a compilation phase, one typo in a local variable name can go undetected until runtime.

Then I remembered my tests and realized I can just run all my tests to make sure that the app still does what I want it to do:

    /dev/app$ rake

All my tests still passed, and it felt great.

Now, I’m not egomaniacal enough to believe that I have perfect test coverage, so I still fired up script/server and manually put the app through its paces. But knowing that the full regressions suite passed made a big difference when it came time to pull the trigger and check in all of my changes.

I’m probably in the minority, here. I know that writing tests is not something everyone does, and writing tests first is less common still.

I’m curious to know what you all do. Does running your app in a browser feel sufficient for you? Do you write tests? Do you wish you wrote tests? If so, what stops you?

Comments

Leave a response

  1. jeffrey   January 11, 2008 @ 05:10 PM

    dude, autotest via ZenTest and (optionally) use rspec. Then your tests are continually running and you know as soon as something breaks. You can also test all your views, etc. so you never even have to fire up your browser. Spending less time in the browser makes me more productive.

    Again, rspec is optional, but I have much love for it. Its changed the way I think about testing. Now I test how my code should behave instead of testing how my code works.

  2. James Avery   January 11, 2008 @ 05:19 PM

    Exactly, what Jeffrey said. Autotest is like a super duper little build monkey who throws poo whenever your tests fail.

  3. Brian Eng   January 11, 2008 @ 05:32 PM

    Despite all the tools in the world which empower us to do so, a lot of people still don't write tests at all. And like Jeff said, test-first is even less common.

  4. Mike Breen   January 11, 2008 @ 06:21 PM

    @jeffrey +1

    don't forget to add growl notifications to autotest!

    I'm not always good about writing a test first but I at least write a pending test to remind me to go back and write the test.

  5. Ryan   January 11, 2008 @ 06:37 PM

    I suppose I'm somewhat in the minority in that I choose to not use rspec or autotest. Actually, my process involves both tests and the browser. I'm fanatical about unit testing. Functional testing is important, but I try to build my apps for most things to be tested as unit tests. And testing views seems somewhat ridiculous to me, for whatever reason. They're extremely brittle (I don't think changing markup should ever make a test fail).

    And I'm also the "designer" too, so I definitely spend a large amount of time in the browser, considering IE is dumb and doesn't listen to standards. But even if I wasn't the designer, I would still fire up the browser for reassurance; I'm not the point where a passing test suite gives me 100% confidence that my application is flawless. Tests are only as good as the person writing them, and I'm sure I don't cover everything.

  6. Matt Blodgett   January 11, 2008 @ 07:27 PM

    "Do you write tests? Do you wish you wrote tests? If so, what stops you?"

    No, I don't write tests.

    Yes, I wish I wrote tests.

    The decision makers at my company apparently don't have an appreciation for unit testing, much less TDD. (And yes, I've tried to push for it.)

  7. anon   January 11, 2008 @ 08:52 PM

    autotest + redgreen + growl is like playing a video game about writing code. SWEEEET!

  8. Anthony Eden   January 11, 2008 @ 10:37 PM

    Autotest FTW!

  9. Andrew S.   January 11, 2008 @ 11:05 PM

    I'm a rails ruby, and just learned about rspec at our local ruby meetup yesterday. From my perspective (which will probably change once I start using rspec), it seems that you duplicate your effort by testing then coding (or vice versa). This leads me to wish for a workflow where you do both simultaneously, so that by define what your code should do (writing your tests), you actually write your code. But again, once I start using rspec, I might change my mind.

  10. Andrew S.   January 11, 2008 @ 11:06 PM

    I'm a rails nuby, and just learned about rspec at our local ruby meetup yesterday. From my perspective (which will probably change once I start using rspec), it seems that you duplicate your effort by testing then coding (or vice versa). This leads me to wish for a workflow where you do both simultaneously, so that by define what your code should do (writing your tests), you actually write your code. But again, once I start using rspec, I might change my mind.

  11. Mark Richman   January 11, 2008 @ 11:21 PM

    Should I give up on Rails+Windows? I don't know how to get autotest+zentest+rspec+redgreen working!!! Arrgh.

  12. Corbin   January 12, 2008 @ 05:28 PM

    +1 for autotest...

    And yes if you are on windows you should switch to *nix if you really want to be a serious rails developer. I gave up on it a long time ago (windows+rails) and life is much easier.

  13. Ben   January 12, 2008 @ 05:51 PM

    If you're using autotest on Mac OS X Leopard you might be interested in taking advantage of the new FSEvents feature...

    Taming the autotest Beast with FSEvents http://rails.aizatto.com/2007/11/28/taming-the-autotest-beast-with-fsevents/

  14. Shawn Oster   January 16, 2008 @ 08:24 AM

    No autotest here as I still code a lot of classic ASP, ASP.NET, PHP and Delphi (yeah, my brain hurts some days) but testing is a must. I tend to do test-first because trying to use the methods before they're coded helps me figure out naming conventions, parameter lists and various interactions.

    I also have a CruiseControl continuous integration server so on each subversion commit all the tests get run. I also try to do more atomic commits vs. the old "here's what I've been working on for the last week" and then watching 100 files go zooming into the repository. If I'm working on something major I branch and make sure my integration server runs tests on that branch.

  15. rjspotter   February 15, 2008 @ 06:04 PM

    if you think your tests cover all your code Heckle (http://ruby.sadi.st/Heckle.html) them.

    --R