Rerun unit tests whenever files update

In which I once again indulge my obscure command-line fetish.

I often spend hours of my day cycling through:

  • Edit code and its unit tests
  • Save my changes
  • Push a button or change window focus to explicitly re-run the code’s unit tests.

Oh frabjous day, the grinding manual labour of the last of these three steps can now be banished forever, courtesy of rerun, a command line Python script that re-runs a given command whenever it detects changes to any of the files in the current directory, or its subdirectories.

Update: It’s Python 2.7, and works great on Windows XP, Macs and Ubuntu.

For example: I had previously bound f6 in Vim to ‘run the current file’s unit tests.’ Now I’ve bound shift-f6 to ‘rerun the current file’s unit tests in a new console window.’ This pops up a new window showing the test results. I then continue editing in Vim, and whenever I hit save, the unit tests are re-run in the other window. All the while the focus stays on my editor. It’s really sweet!

Thanks for the original idea goes to to the bash command ‘watch’, and an old (now offline) blog post by Jeff Winkler.

10 thoughts on “Rerun unit tests whenever files update

  1. I’ve been using the inotify tools in a loop to do the same thing.

    while true ; do clear ; printf “Test run at %s\n” “$(date +’%F %T’)” ; python ./ test ; inotifywait -e create -e modify -e delete -t -r . ; done

  2. When you’re on Linux, you have the lovely inotify. I installed inotify-tools, and now I have this shell script:

    set -x

    while true; do
    nose whatever blah
    inotifywait -e modify *.py

  3. @Jonathan Ballet: Hey. Yeah, ‘watch’ is a great tool, in many situations it’s exactly what you need. That’s why I mentioned it in the original post! :-)

    This replacement is intended for those times I’m developing on Windows, and also I don’t want to actually re-run the tests unless something’s actually changed. Polling the file stats seems to be much cheaper, so it allows for more rapid polling without eating all the CPU.

  4. Under Linux (and MacOSX too I guess), you can use the “watch” command to keep your tests running while you code.
    I often have my screen split in half, my text editor on one side, and my terminal running “watch -n 1 ./bin/test” on the other side, very useful.

  5. Pingback: : Testwatcher

Leave a Reply