Continuous Testing in Rails using Guard and MiniTest

Test Case with success notification

Testing is important in any software project. The faster the code is tested and the shorter the feedback cycle, the better. Here’s how to set up Guard and Guard::MiniTest to provide notifications for the fastest feedback loop possible. Anytime a file watched by Guard is saved it will run the test suite and pop up a notification. I’m using Fedora with Gnome 3 for the notifications bit, so the details might vary if you’re using something else like Ubuntu with Unity. I’ll start from scratch here.

Let’s get started. Open up a shell and create a new Rails project.

Add the three gems to the Gemfile at the root of the project inside the :development group.

Back at the shell, install the gems:

Initialize Guard. This will create a file called Guardfile at the root of the project.

Open up the Guardfile and uncomment the lines near # Rails 4. This tells Guard which files and directories to watch for changes. That section should look like this:

Let’s use the scaffolding to generate a model, and a matching test. Also run rake db:migrate to initialize the database.

Now you can run the generated test by running

You should notice a notification popup indicating success.

Now to make this truly continuous, Guard will watch for file changes and run the tests when something changes. It will popup a notification with the results.

At the root of the project, simply run Guard like this

Note that you can stop Guard by typing ‘exit’.

Now let’s change something to see if it worked. Open up the generated test in test/models/some_model_test.rb. There is a bit of code that is commented out. Uncomment it, and save the file. After a second you should see the test results notification. The useless test case should look like this:

Upon saving the file you should see something like this notification. Again, I’m using Fedora, so it may vary depending on what platform you’re on.
Screenshot showing notifications

Happy frictionless testing!


No Comments

A name is required.
An email is required.
Invalid URL

No comments yet