Open test file belonging to current file

1 Point

Arjen Dijkstra Arjen Dijkstra

9 years ago

When you have a file open, say my_awesome_script.py, this function will find and open test_my_awesome_script.py. You have to open vim in the root directory of your project, though.

:call OpenTestFile()

function! OpenTestFile(split)
    let l:test_file = '**/[tT]est*' . expand('%:t')
    " echom l:test_file
    if a:split ==# 'vertical'
        vsplit
    elseif a:split ==# 'horizontal'
        split
    endif
    try
        execute "next" . ' ' . l:test_file
    catch
        if a:split ==# "horizontal" || a:split ==# "vertical"
            quit
        endif
        echom "No test file found."
    endtry
endfunc

nnoremap <Leader>ovt :callOpenTestFile("vertical")<cr>
nnoremap <Leader>oht :call OpenTestFile("horizontal")<cr>
nnoremap <Leader>ot :call OpenTestFile("")<cr>