DEV Community

Ben Halpern
Ben Halpern Subscriber

Posted on

One of my favorite testing/productivity tools: guard

guard is not a new library. It has been a staple in many areas of Ruby development for years. Many of us use it, but I felt like shedding a bit of light on it as a great too folks should know about in general.

GitHub logo guard / guard

Guard is a command line tool to easily handle events on file system modifications.

Guard

Gem Version Build Status Code Climate Test Coverage Inline docs Reviewed by Hound

Guard Icon

Guard automates various tasks by running custom rules whenever file or directories are modified

It's frequently used by software developers, web designers, writers and other specialists to avoid mundane, repetitive actions and commands such as "relaunching" tools after changing source files or configurations.

Common use cases include: an IDE replacement, web development tools, designing "smart" and "responsive" build systems/workflows, automating various project tasks and installing/monitoring various system services.

For a full categorized list of known Guard plugins, look here: https://github.com/guard/guard/wiki/Guard-Plugins

If you have any questions about Guard or want to share some information with the Guard community, please go to one of the following places:

Before you file an issue, make sure you have read the known issues and file an issue sections that contains some important information.

Features

  • File system changes handled by our awesome…

Guard automates various tasks by running custom rules whenever file or directories are modified.

At DEV, we use guard to run tests as files are saved in order to optimize our test-driven-development practices.

I say "we" and "our" without total certainty that it is used across the team and among contributors because it is not needed in our flow. This is probably one of the better things about its use with rspec. It is the cherry on top.

It's not a heavy-handed default, but so engrained in my own workflow, I almost forget it's not the default.

The gem is most popular in conjunction with Rails and testing, but I'd advise folks to check out its usefulness in any situation where saving a file can kick off a task. The guard ecosystem is vast.

Are there similar tools in other language environments folks check out?

Top comments (10)

Collapse
 
gypsydave5 profile image
David Wickes

I've often used entr for this sort of thing:

eradman.com/entrproject/

It's a simple command line tool. You pipe a list of files to entr, and append a command that you'd like to run when any of those files change.

It's nice and Unix-y, language agnostic, quick to set up, and can be used for more than just test watching. Good tool.

Collapse
 
pavelloz profile image
Paweł Kowalski • Edited

I use watch, npm package :)

yarnpkg.com/en/package/watch

Collapse
 
ben profile image
Ben Halpern

Looks nice

Collapse
 
cristinaruth profile image
Cristina Ruth

We use NCrunch for .NET ecosystem. So nice coz it auto runs unit tests on file changes without saving, and also has an intelligent option where it would only run tests that were impacted by your code changes. It isn't free but well worth the price. 🙂

Collapse
 
jsn1nj4 profile image
Elliot Derhay • Edited

Does it wait a few seconds or so before it runs your tests?

Collapse
 
cristinaruth profile image
Cristina Ruth

Yeah, about a second or two coz it auto builds the project before running the tests.

Collapse
 
andrewbrown profile image
Andrew Brown 🇨🇦

guard is not a new library.

I think that's an understatement.
It's incredibly old. Github would make you believe its only 7 years old when I believe the repo was previously hosted on SourceForge.

Collapse
 
aimerib profile image
Aimeri Baddouh

I can't even think about coding in RoR without guard these days. Our CI/CD pipeline isn't the fastest, so the feedback loop between submitting a PR and receiving the dreaded github warnings that the PR didn't pass all the tests is sometimes over 20 minutes.
Having to go through that for a silly typo, to just get another error almost an hour later kills my soul, but fortunately when I joined the team they had guard setup already so adopting it early on was purely a matter of valuing my own time. I can say with certainty that guard and the rails console are two of my most often used tools any given day.

Collapse
 
dansilcox profile image
Dan Silcox

I love all these sorts of tools to automate things on "the machine building the machine"! PHPStorm has this functionality built into the IDE (at least when working with PHP). They call it "auto-test". You can customise the time between save and (re)run of tests as well. Very useful :)

On a related but separate note, you can also add custom file watchers to do things like regenerating static cache files when the source changes.

Collapse
 
biros profile image
Boris Jamot ✊ /

Modd is another file watcher written in Go. I use it to automate the build and test of my apps.