Skip to content

Instantly share code, notes, and snippets.

@kesor
Created May 22, 2012 10:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kesor/2768065 to your computer and use it in GitHub Desktop.
Save kesor/2768065 to your computer and use it in GitHub Desktop.
Python nosestests running in vim
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" SWITCH BETWEEN TEST AND PRODUCTION CODE
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
function! OpenTestAlternate()
let new_file = AlternateForCurrentFile()
exec ':e ' . new_file
endfunction
function! AlternateForCurrentFile()
let current_file = expand("%")
let current_path = expand("%:h")
let new_file = expand("%:t")
let in_tests = match(current_file, '^tests/') != -1
let going_to_tests = !in_tests
if going_to_tests
let new_file = substitute(new_file, '^', 'test_', '')
let new_file = 'tests/' . current_path . '/' . new_file
else
let new_file = substitute(new_file, 'test_', '', '')
let new_path = substitute(current_path, '^tests/', '', '')
let new_file = new_path . '/' . new_file
endif
return new_file
endfunction
nnoremap <leader>. :call OpenTestAlternate()<cr>
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" RUNNING TESTS
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
map <leader>t :call RunTestFile()<cr>
map <leader>T :call RunNearestTest()<cr>
map <leader>a :call RunTests('')<cr>
map <leader>c :w\|:!script/features<cr>
map <leader>w :w\|:!script/features --profile wip<cr>
function! RunTestFile(...)
if a:0
let command_suffix = a:1
else
let command_suffix = ""
endif
" Run the tests for the previously-marked file.
let in_test_file = match(expand("%:t"), '^test_.*\.py$') != -1
if in_test_file
call SetTestFile()
elseif !exists("t:grb_test_file")
return
end
call RunTests(t:grb_test_file . command_suffix)
endfunction
function! RunNearestTest()
let tests_line_number = line('.')
call RunTestFile(":" . tests_line_number . " -b")
endfunction
function! SetTestFile()
" Set the tests file that tests will be run for.
let t:grb_test_file=@%
endfunction
function! RunTests(filename)
" Write the file and run tests for the given filename
:w
:silent !echo;echo;echo;echo;echo;echo;echo;echo;echo;echo
:silent !echo;echo;echo;echo;echo;echo;echo;echo;echo;echo
:silent !echo;echo;echo;echo;echo;echo;echo;echo;echo;echo
:silent !echo;echo;echo;echo;echo;echo;echo;echo;echo;echo
:silent !echo;echo;echo;echo;echo;echo;echo;echo;echo;echo
:silent !echo;echo;echo;echo;echo;echo;echo;echo;echo;echo
exec "!nosetests " . a:filename
endfunction
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment