Testing in the Terminal

Michael Park published on
2 min, 252 words

Find yourself running a pytest command repeatedly trying to work out what a problem is and getting sick of clicking back on the terminal window where the output is and pressing arrow up to re-run the test? Or even running tests in an IDE and sick of re-running the test time after time with your mouse?

This tip is short and sweet. I have been using this for ages and don't see it listed too often. What you will need is fd (or just find will do), entr, if you are in a terminal with multiplexing/window splitting your're done. If not tmux will probably do fine.

When you are running your test suite and want to re-run tests as you iterate through your changes, just run the following.

(The example is in python but this applies to anything)

fd -e py | entr -c pytest tests/

This basically equates to the following in plain English/behaviour testing language

  • WHEN any file with the file extension py gets saved/modified.
  • THEN clear the screen (clear, cls, or ctrl+l equivalent) and
  • THEN re-run the supplied pytest command

This is a split with vim/neovim/whatever on the left and the test output on the right is amazingly convenient when debugging, checking test coverage, etc... Anything you run repeatedly in the terminal, just pipe those files to entr and let it do the work for you!