Vim unit test plug-in

For more sharing, please visit my personal blog

https://www.niuiic.top/

This paper introduces a flexible unit testing tool in vim.

demand

For large-scale projects, unit testing is essential. There are many ways to do unit testing in vim. For example, using the built-in terminal, write your own test script and pass asynctasks VIM et al. However, the above methods can not be tested accurately and flexibly. For example, there are 10 test files and 100 test functions in total. It will be difficult to rely on the above methods if you only want to test the second function in the third file.

Solution

vim-test Plug in is the best choice at present. The biggest advantage of the plug-in is that it can accurately specify the content to be tested. The plug-in does not need to install dependencies, so just install the plug-in itself. Here is a simple configuration.

nnoremap <silent><nowait> <space>tn :<C-u>:TestNearest<CR>
nnoremap <silent><nowait> <space>tf :<C-u>:TestFile<CR>
nnoremap <silent><nowait> <space>ts :<C-u>:TestSuite<CR>
nnoremap <silent><nowait> <space>tl :<C-u>:TestLast<CR>
nnoremap <silent><nowait> <space>tg :<C-u>:TestVisit<CR>

let test#strategy = {
            \ 'nearest': 'asyncrun',
            \ 'file':    'asyncrun',
            \ 'suite':   'asyncrun',
            \ 'last':   'asyncrun',
            \}

let g:test#echo_command = 0

The plug-in has four test options: test the function closest to the cursor, test the current file, test all and retest the last content.

strategy determines the test running mode and the result display form. It is set to asyncrun, that is, it runs asynchronously in the background and automatically opens quickfix to display the results. This should be the most appropriate option.

Generally, the above configuration is sufficient. You can further specify the test tool and set parameters for display and operation. Therefore, the plug-in can also be tested for performance. See its details github home page.

optimization

The configuration of vim test is very complex, vim-ultest Based on the plug-in, the configuration is optimized and new functions are added.

Here is a simple configuration. After the test is completed, the mark of whether the test passes will appear before the line number. output is the specific result of the unit test. Since the automatic display of the result after running is not effective, turn it off here and use the shortcut key to see the result. Summary is a summary of the results of unit tests passed and failed, with jump function.

nnoremap <silent><nowait> <leader>ta :<C-u>:Ultest<CR>
nmap <silent><nowait> <leader>tf <Plug>(ultest-run-file)
nnoremap <silent><nowait> <leader>tn :<C-u>:UltestNearest<CR>
nnoremap <silent><nowait> <leader>tl :<C-u>:UltestLast<CR>
nnoremap <silent><nowait> <leader>ts :<C-u>:UltestStop<CR>
nmap <silent><nowait> <leader>to <Plug>(ultest-output-jump)
nmap <silent><nowait> <leader>tj <Plug>(ultest-summary-jump)
nmap ;j <Plug>(ultest-next-fail)
nmap ;k <Plug>(ultest-prev-fail)

let g:which_key_map2.t ={
            \ 'name' : '+test',
            \ 'a' : 'run all tests',
            \ 'f' : 'run tests in the current file',
            \ 'n' : 'run the test nearest to the cursor',
            \ 'l' : 'run the last test',
            \ 's' : 'stop all tests in the current file',
            \ 'o' : 'show output of the nearest test',
            \ 'j' : 'jump to the summary window',
            \}

let g:ultest_max_threads=8
let g:ultest_use_pty = 1
let g:ultest_summary_height=10
let g:ultest_summary_open="botright split | resize".g:ultest_summary_height
let g:ultest_output_on_run=v:false

VIM ultimate plug-in also has debugging function, but its configuration is more complex and its function is not as good as vimspector, so it is not recommended.

Keywords: vim unit testing

Added by b-real on Sat, 29 Jan 2022 04:01:24 +0200