Continuous Testing for Minitest with Guard

Iuliu Pop
3 min readDec 27, 2020

Have you ever wished your Minitest test suites re-ran automatically? This is a guide to setup continuous testing for Ruby Minitest in minutes.

We’ll use Guard, a Ruby gem that handles events from file system modifications. Basically, you can run a script when a file is changed. Paired with the guard plugin for minitest, you can watch the files under test and re-run their respective tests automatically!

I’ll show you the steps to set it up and configure it for your own Launch School exercises repository or any Ruby project.

First, we need to configure our project gems. For that, create a file named Gemfile at root directory of your project and add the guard and guard-minitest gems to the development group:

Second, install the gems by running the bundle command in the project root directory.

Third, we have to configure Guard. You can run the bundle exec guard init , then the guard init minitest commands to create a configuration automatically, but the config probably won’t work as is and they’ll be lots of noise. So, we won’t do that.

Instead, I’ll break down a simple config example, so you can quickly learn the Guard DSL to configure Guard for your project.

The Guard config is stored in a Guardfile at your project’s root directory. Here is the Guardfile configuration I use for my Launch School exercises repo:

The guard method adds a plugin. Here I configure the minitest plugin to look for test in the exercises/ folder by passing an array to the test_folders: option. This is important, because in my case I keep the test files in the same place as the exercise solutions. More info about options for the Minitest plugins.

The directories method specifies the directories Guard will watch. As a result, Guard will record an event if any of the files in those directories change. By contrast, thewatch method specifies which of those file changes a particular plugin should respond to.

The watch method takes a regex. If a file that is watched changes and if the regex matches the file path, the minitest plugin will respond to the event. In this case “respond” means it will run the file with Minitest.

Optionally, you can pass in a block to the watch method. There’s a lot you can do in the block, since Guard allows you to run any arbitrary command. I’ll focus just on our use case.

I’ll show how the watch method works with an example:
1) I change the `exercises/ruby_small_problems/advanced_1/rotate90.rb` file and save it.
2) Since the exercises/ folde is watched, Guard records an event.
4) The regex passed towatch on line 10 matches the file path of the file that changed.
3) The minitest Guard plugin responds to the event by running the `exercises/ruby_small_problems/advanced_1/test_rotate90.rb` test file, which I specify in the block on line 12.

If you pass in a block, the match data object is passed to the block parameter. You can use that match data to dynamically create a path string. For example, I rebuilt the path to file that was matched, but append test_ to the file name on line 12. That string is returned by the block and minitest runs the test file instead of the original file the regex matched.

That file must start with test_ to be run by Minitest. Luckily, nothing happens if it doesn’t exist, so not all of your files need a test file.

More info about the Guard DSL, which the methods directories, guardand watch belong to.

The Guard configuration above can work as is for your exercises repository if:
1) Your exercises are in an exercises/ folder in the root directory of your project.
2) The test for a given exercise is in the same folder as the exercise file and it has the same name except test_ is prepended.
3) The sub-directories in your exercises/ folder are no more than two layers deep.

However, you can easily change it to suit your needs if you understand Regex. For a simple but complete intro to Regex, see here.

Finally, after you’ve configured Guard, run guard with the bundle exec guard command. Voila! Continuous testing is running.

I hope this guide proved useful to you! It’s really neat to have continuous feedback while you’re solving exercises.

--

--

Iuliu Pop

What do you want to be different at the end of your life as a result of your actions?