Running tests automatically with Guard

Another way to get test feedback quickly is to allow the tests to run automatically when our code changes. In Ruby, the Guard gem allows us to trigger arbitrary events when files change. It’s a powerful system, and running tests is only a fraction of what it can do. We’re just going to manage the basic setup here; the full documentation is available online. Mac users should check out the Mac-specific wiki page to ensure Guard receives file events correctly.

Guard is a generic system for triggering events, and it has separate libraries that make it easy to trigger specific kinds of events, such as starting a test.

Setting up Guard

To set up Guard, we’ll add the Guard gem and any of the dependent libraries to the Gemfile. Note that these all go in the development group, not the test group:

Get hands-on with 1200+ tech skills courses.