Run your Ruby tests quickly and easily using Vim and Tmux
<p>In order for testing to become part of your development workflow, it needs to become a habit. And like any habit, its biggest enemy is neglect. Too often I’ll be in a rush and not add tests to my code for a day, and that turns into a week and then a month, and suddenly I have an app where half of my codebase is untested and the other half has breaking tests. There are many things you can do to help keep this habit (hooking up a CI server immediately comes to mind), but an important one is to make running your tests as quick and easy as possible.</p> <p>One way I do this is by making my ruby tests (either Rspec or Minitest) extremely easy to run while I’m using <a href="http://www.vim.org">Vim</a> (and <a href="https://tmux.github.io">Tmux</a>). With one quick keystroke, I can run the current test file or individual test in a new Tmux pane.</p> <p></p> <p>To set this up, first install the following 2 Vim plugins using a plugin manager such as <a href="https://github.com/Shougo/neobundle.vim">NeoBundle</a> or <a href="https://github.com/VundleVim/Vundle.vim">Vundle</a>.</p> <ul> <li><a href="https://github.com/benmills/vimux">Vimux</a>: Enables Vim to interact with Tmux</li> <li><a href="https://github.com/jgdavey/vim-turbux">Turbux</a>: Run ruby tests from Vim</li> </ul> <p>Next, you’ll probably want to configure your test command to be prefixed with <code>bundle exec</code>, so add the following to your Vimrc:</p> <div class="highlight"><pre class="highlight viml"><code><span class="k">let</span> <span class="nv">g:turbux_command_prefix</span> <span class="p">=</span> <span class="s1">'bundle exec'</span> </code></pre></div> <p>Now that everything is installed (and you’ve reloaded Vim if needed), you can open your first test file. Then just hit <code><leader> t</code> to run all of the tests. It will then open up a new tmux pane (or reuse an existing one if already open) and run them just as if you had run the command yourself. And then if you want to run a single test at a time, move the cursor to inside the test and hit <code><leader> T</code>.</p> <p>You can change these default keyboard shortcuts if you’d like. Here’s what I use:</p> <div class="highlight"><pre class="highlight viml"><code><span class="k">let</span> <span class="nv">g:no_turbux_mappings</span> <span class="p">=</span> <span class="m">1</span> <span class="nb">map</span> <span class="p"><</span>leader<span class="p">></span><span class="k">t</span> <span class="p"><</span>Plug<span class="p">></span>SendTestToTmux <span class="nb">map</span> <span class="p"><</span>leader<span class="p">></span>s <span class="p"><</span>Plug<span class="p">></span>SendFocusedTestToTmux </code></pre></div> <p>By combining these handy shortcuts with the speed increase provided by <a href="http://guides.rubyonrails.org/4_1_release_notes.html#spring-application-preloader">Spring</a>, the application preloader added in Rails 4.1, you’ll have no excuse not to be running your tests quickly and frequently.</p> <p>Here’s a quick gif of how the whole process looks:</p> <p><img src="/assets/images/vim_tmux_testing-9485262f.gif" alt="Demo of running mintest from Vim and Tmux" /></p>