Have I mentioned how much I love automated testing of my code? Like madly, obsessively love testing? It’s completely changed the way I write code and even made me more in tune with the universe (ok, so maybe not that last bit).
Now one thing I’ve found about running tests is that unless they run super-fast and with minimal thought on my part, they won’t be run at all (unless there’s a continuous integration server but that only runs tests after the code has been committed, too late to help with the coding process itself). One great way to achieve this is to have the tests run every single time I save a file. That way I can write a new test, save it and see it immediately fail, and then write the passing code and see it immediately succeed.
Doing this is actually not too hard. You just need a script to poll a directory for file changes and launch a command that runs the tests when that happens.
$ python change_runner.py -c run_tests.sh ./test ./src
It works by taking in a command you want to run and the directories you want to watch. In this example the directories “test” and “src” are watched. When files in these directories are changed, run_tests.sh is launched.
I’ve found using this script to be pretty fun. I keep it running in a terminal off to the side and every time I press C-xC-s (or :w depending on my mood) the results of the test run scroll by. It’s a pretty addictive way to do TDD.