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&rsquo;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&rsquo;m using <a href="">Vim</a> (and <a href="">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="">NeoBundle</a> or <a href="">Vundle</a>.</p> <ul> <li><a href="">Vimux</a>: Enables Vim to interact with Tmux</li> <li><a href="">Turbux</a>: Run ruby tests from Vim</li> </ul> <p>Next, you&rsquo;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&rsquo;ve reloaded Vim if needed), you can open your first test file. Then just hit <code>&lt;leader&gt; 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>&lt;leader&gt; T</code>.</p> <p>You can change these default keyboard shortcuts if you&rsquo;d like. Here&rsquo;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">&lt;</span>leader<span class="p">&gt;</span><span class="k">t</span> <span class="p">&lt;</span>Plug<span class="p">&gt;</span>SendTestToTmux <span class="nb">map</span> <span class="p">&lt;</span>leader<span class="p">&gt;</span>s <span class="p">&lt;</span>Plug<span class="p">&gt;</span>SendFocusedTestToTmux </code></pre></div> <p>By combining these handy shortcuts with the speed increase provided by <a href="">Spring</a>, the application preloader added in Rails 4.1, you&rsquo;ll have no excuse not to be running your tests quickly and frequently.</p> <p>Here&rsquo;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>