Ruby Single Test

I, like many developers, do my best to follow the practice of Test Driven Development (TDD). In practical terms this means you write a test to test your code before you write the code itself. This approach forces your mindset into having a clearer sense of how the code will be used since you’re writing code to use the code first.  Got it?  Increased test coverage also means more robust and maintainable code.

My first formal introduction to TDD was a three-day TDD workshop put on by Jim Weirich and Joe O’Brien while I was at the Los Angeles Times. Jim, an expert EMACS user, had his system setup to run tests quickly from within the editor, even showing a red or green notice indicating the pass/fail status of the test.

This may not seem like a big deal to people using any number of IDEs but personally, I’m a Vim user (specifically MacVim, it’s an extremely well done port of Vim using Cocoa). I love it. I’m way faster editing inside of Vim; my brain has mapped around all of the commands because I’ve been editing with it for so long. I’ve been using vi variants in one form or another since my Freshman year of college when my Computer Science 101 professor forced us all to code with it for our programming projects on the school’s Sun server. While doing Java work two or so years ago, I developed mostly on Eclipse and eventually broke down and purchased a license for the Eclipse vi keybinding plugin. Now that I’m employed for Ruby work I’ve switched back to Vim full-time (after a 1/2 hour dabbling with Textmate).

Jim’s EMACS setup made me jealous. During the class I figured out how to set the make program to use ruby (or spec for rspec tests) and Quickfix will run the tests for the file. Here’s the command:

:set makeprg=ruby\ %

The \ escapes the space and the % gets expanded to the current buffer’s filename when Quickfix is activated. Quickfix was originally used for a “quick” way of compiling your program to see if there’s any errors. Vim will even move your cursor to the file and line number of the first error if there are any. This works for ruby tests too! Another Vim setting, errorformat, is how Vim parses errors from running makeprg.

OK, so this is all great, but what if my test file is huge? What if it takes 10+ seconds to run? What if I’m only interested in a single test because I’m being a good developer and practicing TDD?

This is where the Ruby Single Test plugin comes in. The plugin is an extraction of some functions I’ve been playing with in my .vimrc file for the past couple of weeks. All it does is provide a command for running the test block your cursor is hovering over in your test file (<leader>. by default.  <leader> is usually comma or backslash). Most of the time you’re only interested in a single test while you’re editing code so turn-around time is vastly improved. No more jumping back to the console to run a test.

To install just drop the ruby_single_test.vim file in your .vim/plugin directory.

Currently it works for Test::Unit as well as Rspec tests.  I’ve been using this quite a bit recently and I think I’ve worked out most of the bugs. My setup isn’t necessarily the same any everyone’s so please let me know if you have any problems with it! The code is up on Github, so feel free to play with it.

This entry was posted in Coding, Plugins, Vim and tagged , , . Bookmark the permalink.

5 Responses to Ruby Single Test

  1. Adam says:

    Hello,

    Would you mind publishing a small howto that explains how to use this plugin? I’m new to both ruby and vim but sense this plugin offers some great functionality.

  2. Doug,
    I don’t know who Ruby is, but I don’t see a “contact Doug” button on your site. So I’m commenting to get your attention. DOUG! OVER HERE!

    Anyway, my wife and I watched My Blue Heaven last night and I thought about how we used to merengue like Moranis and Martin. That made me think of taking dates to the Sheraton Tech Center because we thought the bottomless chex mix, Shirley Temples and piano player were so suave. We sure were young then.

    By the looks of it, you are doing well. I am too. I’d love to catch up.

    Josh

  3. Doug says:

    We were so cool :)

  4. Rex Pierce says:

    Is there a way to debug ruby code running inside vim (like printing the backtrace, etc) ?

    • doug says:

      Ruby code running like VimScript? I’m not sure — I’ve never done written any Ruby vimscripts.

      This plugin uses the QuickFix feature but that’s more for running tests on a Ruby project and parsing its output.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>