Integration with PyUnit testing framework
From Vim Tips Wiki
Tip 280 Previous Next Created: July 10, 2002 Complexity: intermediate Author: Stefan Roemer, Max Ischenko Version: 6.0
Vim has a wonderful ability to integrate with external tools, like compilers, make, ctags etc.
That's one of the reasons we love it.
PyUnit can be seen as a "compiler" for the Python test code.
To understand it, Vim should be told about the language the PyUnit speaks. This could be done with 'errorformat' option:
setlocal efm=%C\ %.%#,%A\ \ File\ \"%f\"\\,\ line\ %l%.%#,%Z%[%^\ ]%\\@=%m
This magic spell enables Vim to parse unittest.TextRunner's output and to enter quick-fix mode.
To run all your unit tests at once you'll need to setup 'makeprg' option and provide a runner.
I'm using this setup:
setlocal makeprg=./alltests.py
And contents of the alltests.py (for the sake of completeness):
#!/usr/bin/env python2
import unittest
import sys
sys.path.append('unittests')
modules_to_test = (
'fooTest',
'barTest',
'bazTest',
)
def suite():
alltests = unittest.TestSuite()
for module in map(__import__, modules_to_test):
alltests.addTest(unittest.findTestCases(module))
return alltests
if __name__ == '__main__':
unittest.main(defaultTest='suite')
While talking about it, I'd also suggest to add a couple of mappings.
In the end, my vim/files/ftplugin/python.vim looks like this:
setlocal makeprg=./alltests.py\ -q setlocal efm=%C\ %.%#,%A\ \ File\ \"%f\"\\,\ line\ %l%.%#,%Z%[%^\ ]%\\@=%m iabbr <buffer> sae self.assertEquals iabbr <buffer> sar self.assertRaises
[edit] References
[edit] Comments
pyunit compiler will be included in Vim 6.2 release.
