Digital Marketing & Communications

We've seen 1s and 0s you wouldn't believe

Topic: Howto

Get told immediately when your tests pass or fail with Guard and terminal-notifier-guard

📥  Communication, Development, Howto, Tools

guard-demo

Guard in action

Got your feedback loop between writing code and executing tests down to the shortest time possible? Of course you have. After all, you know your tried-and-tested shortcut keys.

But there may be something better than your years-old habit.

Just run Guard

Guard will watch your code for changes. When it detects one, it'll fire up your tests for you in the background. Sounds pretty useful, right?

Get notified

With Guard on its own, you'd still have to Cmd + Tab to your terminal to see the test results.

But Guard also supports plugins to send out notifications. The one I use for my MacBook is terminal-notifier-guard, but there are a bunch of others you could use for your platform.

The advantage to using a desktop notification, as opposed to having your terminal window, is that you can keep your editor at fullscreen. Even better, you can be looking at something else entirely but still see when your tests have finished. This is great for when your tests take a non-trivial amount of time.

Setup

It's as simple as adding this to your Gemfile:

group :development do
gem 'minitest-reporters'
gem 'guard'
gem 'guard-minitest'
gem 'terminal-notifier-guard'
end

In our case above, we've added the minitest-reporters and guard-minitest gems.

Then follow the README for Guard on how to initialise and install Guard.

Small savings add up

It might seem very tiny, but the amount of thinking required to remember your key combos and execute them through your keyboard is actually valuable brain power you could be applying to writing code instead. And if you are frequently running tests, these savings add to up to an even greater amount.

Ben Orenstein said in his talk at Bath Ruby 2015 to try and run your tests from within your code editor. He did so in Vim in his talk. Guard is just a step on from that.

I've also started using the guard-livereload gem recently. Using this with the LiveReload browser plugin, any changes made to a view will refresh my browser automatically and show me the changes immediately. Super useful.

Happy coding!