Michael Dyrynda
Home Blog Podcasts
 
Getting interactive with testing in Vim March 9th, 2020

When I first started programming, I had two options; Vim or Notepad.

All of my peers in my first programming job were Vim users (except that one that was using Emacs), so the natural draw for me was to use what everybody else was using, in order to lean on their experience.

Don't get me wrong, learning Vim has a steep learning curve. This isn't a post about learning Vim, though. For that, you can check out the excellent series by Ben Orenstein and Chris Toomey - Onramp to Vim (it's free!)

Because Vim is highly configurable, it can take a while to get it just so, but it is well worth it in my opinion. I'm not here to sell you on using Vim, though - use whatever works for you - but if you're interested in how I go about running tests from Vim, read on!

I've been using vim-test for a long time now, and have always been a little bothered by the fact that it drops into the terminal to run the tests. It breaks me out of my Vim flow, but I just figured this is how it was, so I left it as it is.

Using the default vim-test execution strategy

Yesterday, I wanted to see if there was a better way to go about this, that was a more seamless integration for executing tests from Vim, without interrupting the otherwise smooth experience.

Turns out, the vim-test configuration gives you a number of different execution "strategies" right there in their readme file, which you can configure in your .vimrc.

let test#strategy = "basic"

If, like me, you're using Vim 8 the easiest way to use something a little more seamless is the vimterminal strategy.

let test#strategy = "vimterminal"
Using the vimterminal execution strategy

The vimterminal strategy is closer to what I'm after, because it stays within the Vim window, however, it shifts my cursor to the new terminal split it creates, and I have to press Enter to close the split and go back to my code. It's not quite there for me.

As somebody who spends a great deal of my time in the terminal, I use tmux. tmux is a terminal multiplexer, which means I can create, access, and use multiple terminal instances from the same tab and window in my terminal emulator of choice, iTerm 2. Thoughtbot also has a great tmux crash course.

Luckily, vim-test does provide an option to execute tests within a separate pane in the same tmux window. This is my perfect scenario.

You'll first need to install the vimux plugin, which lets Vim send input to a tmux pane. Use your plugin manager of choice (I'm using Vundle).

Plugin 'benmills/vimux'

Then set your execution strategy to vimux.

let test#strategy = "vimux"
Using the vimux execution strategy

This is my perfect scenario because I can easily run tests - whether a single file, individual test, or entire suite - without leaving Vim and interrupting my workflow. tmux allows me to switch between panes and scrollback through test output if I have to, or reposition the pane.

I'm a real developer ™
Michael Dyrynda

@michaeldyrynda

I am a Lead Software Developer at Superloop by day, and a freelancer, blogger, and podcaster by night.

Proudly hosted with Vultr