Focused Testing in Visual Studio Code

Zachery Moneypenny
Adorable
Published in
3 min readAug 17, 2017
Photo by Roman Trifonov on Unsplash

I’ve been trying out Visual Studio Code for the last few weeks and it’s been a really enjoyable experience! For the last two years I’ve been a pretty devoted MacVim user and had tweaked my setup to exactly my preferences, but the customization available in newer editors like Atom and Code, along with the comparable pain of customizing in VimScript has always left me checking them out every few months to see where the performance is at.

My recent adventures in Elixir have thus been undertaken using Code and though I have the configuration mostly where I want it, testing was proving to be a frustrating experience. Specifically, I would be working on tests in a single file, but would be ranging all over the code base to check other things, or fix a config value somewhere, but then I’d have to keep focusing back on the Terminal pane to hit up arrow + Enter to rerun the tests. It really took me out my flow.

What I really wanted from my testing experience was to be able to set a single test file as focused and then be able to easily run the tests in that file no matter what other file I happened to be working in.

Starting with the sample tasks on the vscode-elixir README I iterated on them until I got a series of tasks I was happy with, and then I bound them to various hot keys that worked for me. In addition to hot keys you can also access all of the tasks via the command palette by typing Cmd+Shift+P and then Run Test Task, at which point you’ll be able to select from the list of custom tasks.

Now that my tasks are bound the way I want them, my process is to hit F4 in the test window I want to focus on, then no matter where I am in the editor I can hit F5 to run the tests in that file or F6 to debug the tests in that file. I also use Cmd+T Cmd+T to run all of the tests and Cmd+T Cmd+F to run the tests in the file where my cursor currently is.

As a bonus, now that I’m running my test tasks via shell instead of the default process type they show up in the terminal with full color support.

To install these tasks you can copy the tasks.json file from the Gist into the .vscode directory in your project. Then open the keybindings by typing/selecting Preferences: Open Keyboard Shortcuts File from the command palette. In that file you can define your own keybindings to invoke the tasks.

After a few days of F5ing my way to happiness I’m definitely happy with this solution, and I have some additional ideas about how to enable focusing even further, on a particular line/test rather than a whole file.

Zachery Moneypenny is a Principal Developer at adorable.io. Rubyist for a long time, with experience in Golang and Javascript as well but moving towards Elixir and loving it.

Code is available at the Gist but embedded below as well.

--

--

Zachery Moneypenny
Adorable

engineering manager at cars.com | opinions on tech interviewing | woodworking | song-and-dance man