TestNav: Easier test file navigation in Vim

Automatically navigate from a production code file to its corresponding test code file in Vim

This past year, I switched my everyday text editor from Sublime Text to Vim. One of the key features that I missed from Sublime Text was the ability to quickly and automatically navigate from a production code file to its corresponding test code file and back again. In Sublime Text, this functionality was provided by a plugin called RubyTest.

Upon switching to Vim, I immediately started looking for a replacement for toggling between production and test code. I started out using Gary Bernhardt’s excellent .vimrc which includes a script that functions very similarly to RubyTest. However, the script is Rails specific and didn’t work for any other language besides Ruby.

These limitations inspired me to build my first plugin, TestNav. Similar to the plugin and script that inspired TestNav’s creation, the plugin by default binds <Leader>. to a function that will do a best guess to navigate you from the current file to the corresponding test or production file.

Currently, the plugin only works assuming you follow a few conventions:

  1. You launch Vim in the root directory of your project (or the Vim working directory is set to the root directory of your project).
  2. Your test file names end with -test, _test, -spec or _spec and the corresponding production file name matches without the ending. For example, user_spec.rb contains the specs for the logic defined in user.rb.
  3. Your Vim is compiled with ruby support and the Unix find command is available on your system.

I’ve tested the plugin in a variety of ruby, javascript and elixir projects and I believe it is flexible enough that it should work with most languages assuming your file-naming for tests and production files are consistent and follow the expected conventions.

Install TestNav

To install the plugin, I recommend using pathogen. If you have pathogen installed, all you need to do is execute the following command to get the latest version of TestNav:

cd ~/.vim/bundle && git clone git@github.com:davebrace/vim-testnav.git

I would appreciate any and all feedback (and GitHub stars for the repository!). Feel free to contact me on Twitter at @DaveBrace.