Inside Out

Notes on seeking wisdom and crafting software

Test driven setup with tmux and entr

Table of contents

This post provides a setup for test driven development with instant feedback. You can use any editor with your favorite test runner for any language. Here’s a screencast of our end goal (opens in a new tab).

Let’s go step by step now. Our setup requires:

  • Edit file in a terminal
  • Monitor if there are any changes in filesystem
  • In case a file has changed, run tests
  • Pipe the test run status to the terminal where we’re writing code

Before we start…

We’ll install a few pre-requisites.

  • tmux is a terminal multiplexer. It allows us to run tests and edit code in the same terminal.
  • entr is a simple command line tool to watch files for any changes and run an arbitary command on such an event.
$ sudo pacman -S tmux
$ yaourt -S entr

These are installation commands for Arch Linux. Please replace them with your favorite package manager on the box.

Watch for changes

Enter entr. Jokes aside, this tool is one of the most simple and best available for the job at hand. An alternative is the facebook watchman. While it seems awesome as well, the setup (daemon, client etc.) seemed too much for this task.

Our oneliner is here.

$ ls **/*.py | entr -s 'pytest; [[ $? -eq 0 ]] && bg="colour22" || bg="colour124"; tmux set-window-option -g window-status-current-bg $bg'

Breaking it apart…

  • ls **/*.py lists all python source code in the directory (replace with your source code files)
  • entr -s evaluate the command we provide in a shell, we need to evaluate the exit code of pytest and provide feedback
  • pytest; run the test runner (replace with your favorite runner)
  • [[ $? -eq 0 ]] && bg="colour22" || bg="colour124" choose a background color based on the exit code of pytest command. color22 is green and color124 is red.
  • tmux set-window-option -g window-status-current-bg $bg sets the tmux tab background color with the feedback color we’ve set earlier

Please experiment to choose the appropriate colors for your terminal/tmux theme :)

That’s all for today. Hope you enjoy the test driven setup!