Summary : How to get a nice green/red bar for you python unit tests in vim

Recently I have started following my friend Bram advice to do units testing for my python programming. Obviously, I soon wanted to have those unit tests run into vim and get that nice bar that go green or red depending if the code pass or not, the one you see in the nice Gary Bernhardt's screencasts.

Fortunately ReinH made the work easier by already extracting the code to make the red/green bar in a plugin called vim-makegreen (originally RubyRedGreen as it was for ruby)

Here is how I made it works with my python's unit tests, note that I'm using the standard python unit test module but that it work with nose-type tests too.

Pre-requisites

You will need to install nose, a unit testing framework for Python which more importantly provide a nosetests command that detect all the tests in the current directory and run them, this avoid having us create our own python script to do the testing as Tip 280 suggests.

On debian based system it's a simple matter of apt-get install python-nose or you can use easy_install nose

Note that if you don't want to install nose you can still use the Tip 280 way and modify the makeprg option

PyFlakes interference

I use pyflakes-vim to automatically check my python code constantly, if you don't do it too you should it's awesome and make you gain a lot of time. Unfortunately this plugin use the quickfix list too and so always hide the errors list from the unit test which is quite annoying (and it took me a lot of time to find out was wrong with my quickfix).

Fortunately the release tarball provide a patch to disable the quickfix integration, which you can apply with patch -p1 -R <quickfix.diff - just ignore the error about the readme.rst.

The proper way to fix this in the future would be to have an option to disable populating the quickfix, see Issue 13 of pyflakes-vim

Update : This have been fixed in upstream pyflakes-vim, now you just need to put let g:pyflakes_use_quickfix = 0 in your .vimrc

Compiler file

We are going to handle nosetests in Vim as if it was a compiler since Vim integrate them well. For that we need a 'compiler file' which tell Vim what program to run and hot to parse it's output to detect errors.

I made one for nosetests, based on the pyunit one and which you can download here and install by copying the nose.vim file in '.vim/compiler'

Inside Vim use :compiler nose to select it then running :make will run all the tests nosetests find in the current directory and jump you to the first error.

There is also a git repository for the compiler file.

Vim-makegreen

The last piece to install is vim-makegreen. You will need the latest git version as it contain my patch to make it works with nosetests. Download it in zip format or retrieve it via git and install it like any other vim plugin.

If you now press \t (or rather <Leader>t) all your tests will be run and a red or green bar will be displayed depending if they fail. (Don't forget to set :compiler)

Automatically set nose as compiler (Optional)

If you don't want to set the :compiler every-time you edit a file you can have it set automatically for all python file by adding the following line to your .vimrc :

 autocmd BufNewFile,BufRead *.py compiler nose

Mini quickfix primer (Appendix)

Since this use quickfix you can use the regular quickfix commands, here are the most useful ones:

  • :cl display the list of error
  • :cn jump to the next error
  • :cp jump to the previous error
  • :copen open a buffer with the list of error
  • :cw close it

See also the :help quickfix for more information on it.

Mendatory screenshot:

makegreenpython.png