m (Switching to unit test module for python moved to Switch to unit test module for Python: Page moved by JohnBot to improve title) |
(Change to TipImported template + severe manual clean) |
||
Line 1: | Line 1: | ||
{{review}} |
{{review}} |
||
+ | {{TipImported |
||
− | {{Tip |
||
|id=658 |
|id=658 |
||
+ | |previous=656 |
||
− | |title=Switching to unit test module for python |
||
+ | |next=659 |
||
− | |created=February 18, 2004 |
+ | |created=February 18, 2004 |
|complexity=basic |
|complexity=basic |
||
|author=Max Ischenko |
|author=Max Ischenko |
||
|version=6.0 |
|version=6.0 |
||
|rating=2/2 |
|rating=2/2 |
||
⚫ | |||
− | |text= |
||
− | Doing a lot of programming in Python, I need to switch quickly between Python module and corresponding unit-test module. Often there is one unit-testing module for multiple python modules. |
+ | Doing a lot of programming in Python, I need to switch quickly between Python module and corresponding unit-test module. Often there is one unit-testing module for multiple python modules. |
− | This function allows you to switch to correct unit testing module quickly, using the filename hardcoded at the end of the file. Idea is stolen from Twisted sources. |
+ | This function allows you to switch to correct unit testing module quickly, using the filename hardcoded at the end of the file. Idea is stolen from Twisted sources. |
− | Put this to your ftplugin/python.vim: |
+ | Put this to your ftplugin/python.vim: |
<pre> |
<pre> |
||
nmap <buffer> <F5> :call JumpToTestFile()<CR> |
nmap <buffer> <F5> :call JumpToTestFile()<CR> |
||
− | |||
fun! JumpToTestFile() |
fun! JumpToTestFile() |
||
− | let line = getline("$") |
+ | let line = getline("$") |
− | if line =~ "^### testfile: " |
+ | if line =~ "^### testfile: " |
− | let filename = strpart(line, 14) |
+ | let filename = strpart(line, 14) |
− | execute ":e " . filename |
+ | execute ":e " . filename |
− | else |
+ | else |
− | echo "TEST PATTERN ### testfile: NOT FOUND!" |
+ | echo "TEST PATTERN ### testfile: NOT FOUND!" |
− | endif |
+ | endif |
− | endfun |
+ | endfun |
</pre> |
</pre> |
||
⚫ | |||
⚫ | |||
+ | '''TODO''' Fix indents in following, and combine/clean. |
||
⚫ | I created this general purpose script (some may disagree since I dropped down to python scripting; sorry, just don't have time to hunt through docs for vim script info!) ... but if you have python compiled in with your vim, and happen to use a test framework, this with very little tweaking may help. |
||
⚫ | |||
⚫ | I created this general purpose script (some may disagree since I dropped down to python scripting; sorry, just don't have time to hunt through docs for vim script info!) ... but if you have python compiled in with your vim, and happen to use a test framework, this with very little tweaking may help. |
||
− | I generally have a 1:1 correspondence between modules and test scripts, so this approach saves me having to put test file names in my source. |
+ | I generally have a 1:1 correspondence between modules and test scripts, so this approach saves me having to put test file names in my source. |
+ | <pre> |
||
⚫ | |||
− | + | inoremap <M-t> <ESC>:call UTestToggle()<CR> |
|
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
+ | </pre> |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
− | |||
− | |||
− | '''Anonymous''' |
||
− | , September 26, 2006 11:46 |
||
---- |
---- |
||
− | Minor change, my fault for not testing it a bit. This rev switches to a buffer if the file is already in vim's buffer list, rather than calling the edit command. Was seeing instances where unsaved changes were lost. |
+ | Minor change, my fault for not testing it a bit. This rev switches to a buffer if the file is already in vim's buffer list, rather than calling the edit command. Was seeing instances where unsaved changes were lost. |
⚫ | |||
⚫ | |||
⚫ | |||
− | UTEST_PREFIX='test/utest_' |
+ | UTEST_PREFIX='test/utest_' |
− | def edit(target): |
+ | def edit(target): |
− | + | # first try to load from buffer |
|
− | for buf in vim.buffers: |
+ | for buf in vim.buffers: |
− | if target == buf.name: |
+ | if target == buf.name: |
− | vim.command('b!%s' % buf.number) |
+ | vim.command('b!%s' % buf.number) |
− | return |
+ | return |
− | + | # must not be in buffers, open file |
|
− | vim.command('e! %s' % target) |
+ | vim.command('e! %s' % target) |
− | return |
+ | return |
− | curfile = vim.current.buffer.name |
+ | curfile = vim.current.buffer.name |
− | if curfile: |
+ | if curfile: |
− | if UTEST_PREFIX in curfile: |
+ | if UTEST_PREFIX in curfile: |
− | + | # switch to the matching source |
|
− | edit(curfile.replace(UTEST_PREFIX, '')) |
+ | edit(curfile.replace(UTEST_PREFIX, '')) |
− | else: |
+ | else: |
− | + | # switch to the matching utest_filename.py|qpy |
|
− | filepath, filename = path.split(curfile) |
+ | filepath, filename = path.split(curfile) |
− | edit(path.join(filepath, UTEST_PREFIX+filename)) |
+ | edit(path.join(filepath, UTEST_PREFIX+filename)) |
− | |||
− | '''Anonymous''' |
||
− | , September 26, 2006 12:24 |
||
---- |
---- |
||
⚫ | |||
− | <!-- parsed by vimtips.py in 0.603449 seconds--> |
Revision as of 03:37, 10 November 2007
created February 18, 2004 · complexity basic · author Max Ischenko · version 6.0
Doing a lot of programming in Python, I need to switch quickly between Python module and corresponding unit-test module. Often there is one unit-testing module for multiple python modules.
This function allows you to switch to correct unit testing module quickly, using the filename hardcoded at the end of the file. Idea is stolen from Twisted sources.
Put this to your ftplugin/python.vim:
nmap <buffer> <F5> :call JumpToTestFile()<CR> fun! JumpToTestFile() let line = getline("$") if line =~ "^### testfile: " let filename = strpart(line, 14) execute ":e " . filename else echo "TEST PATTERN ### testfile: NOT FOUND!" endif endfun
Comments
TODO Fix indents in following, and combine/clean.
I created this general purpose script (some may disagree since I dropped down to python scripting; sorry, just don't have time to hunt through docs for vim script info!) ... but if you have python compiled in with your vim, and happen to use a test framework, this with very little tweaking may help.
I generally have a 1:1 correspondence between modules and test scripts, so this approach saves me having to put test file names in my source.
inoremap <M-t> <ESC>:call UTestToggle()<CR> nnoremap <M-t> :call UTestToggle()<CR> function! UTestToggle() python << EOF # toggles between a path like: /myproject/person.py and /myproject/test/utest_person.py # could mkdir the 'test' directory if not exists, but chances are the user will # be doing that outside of vim anyway. import vim from os import path UTEST_PREFIX='test/utest_' curfile = vim.current.buffer.name if curfile: if UTEST_PREFIX in curfile: # switch to the matching source vim.command('e %s' % curfile.replace(UTEST_PREFIX, '')) else: # switch to the matching utest_filename.py|qpy filepath, filename = path.split(curfile) vim.command('e %s' % path.join(filepath, UTEST_PREFIX+filename)) EOF endfunction
Minor change, my fault for not testing it a bit. This rev switches to a buffer if the file is already in vim's buffer list, rather than calling the edit command. Was seeing instances where unsaved changes were lost. Note: vim complains about free() when I run this; gvim doesn't appear to, although that may just be surpressed output.
UTEST_PREFIX='test/utest_'
def edit(target):
# first try to load from buffer for buf in vim.buffers: if target == buf.name: vim.command('b!%s' % buf.number) return # must not be in buffers, open file vim.command('e! %s' % target) return
curfile = vim.current.buffer.name if curfile:
if UTEST_PREFIX in curfile: # switch to the matching source edit(curfile.replace(UTEST_PREFIX, )) else: # switch to the matching utest_filename.py|qpy filepath, filename = path.split(curfile) edit(path.join(filepath, UTEST_PREFIX+filename))