Automatically Run Unit Tests With entr

March 31, 2016

Introduction

I do a lot of test driven development, and generally use tests a lot when I’m writing code. It’s nice to have your tests automatically run every time you save a file, so you know if you broke something.

This guide will show you how to make that happen. Since I mostly do PHP development, this guide will focus on using PHPUnit, but this would work for any test runner.

entr

Entr is a great little unix utility that does one thing well - automatically execute a task when something changes. There are other utilities like watchman plus a million node projects, but I like entr because it’s simple.

Installation

Mac:

brew install entr

Ubuntu/Debian:

sudo apt install entr

For other platforms refer to the docs. it looks like entr doesn’t seem to work on Windows. Sorry!

Usage

Now that entr is installed, lets use it to run phpunit!

find src/ | entr -c phpunit

Ok so here is what’s happening.

find src/

This command will list all the files in my src directory. You can change this to include whatever directories you want to watch.

|

This is a unix pipe to pipe the output of find in to entr, so entr knows what files to watch.

entr -c

The -c flag tells entr to clear the terminal before invoking the command you specified. For more flags, checkout man entr.

phpunit

The command to run.

In Action

This is what it looks like:

Pretty cool huh?

More Complicated Commands

If you need to run a more complicated command, you can use sh and put the command in quotes. Here’s an example that uses osx say to let you know out loud if you break something.

find src/ | entr sh -c "phpunit && say 'good work' || say 'oh no'"

File Limits

You might try to run entr and get a message like this:

entr: Too many files listed; the hard limit for your login class is 2560. Please consult http://entrproject.org/limits.html

That means you ran out of file descriptors. Go read the limit docs and bump your file descriptor limit up and it should work fine (hint: you can get your osx version with sw_vers).