vimtricks.wiki Concise Vim tricks, one at a time.

How do I run tests from inside Vim?

Answer

:TestNearest (vim-test)

Explanation

vim-test provides commands to run tests from within Vim. It detects the test framework and runs the appropriate test commands.

How it works

  • :TestNearest runs the test nearest to the cursor
  • :TestFile runs all tests in the current file
  • :TestSuite runs the entire test suite
  • :TestLast re-runs the last test

Example

nmap <leader>t :TestNearest<CR>
nmap <leader>T :TestFile<CR>
nmap <leader>a :TestSuite<CR>
nmap <leader>l :TestLast<CR>

Tips

  • Install: Plug 'vim-test/vim-test'
  • Supports many frameworks: pytest, jest, go test, rspec, etc.
  • let test#strategy = 'neovim' runs tests in a Neovim terminal
  • :TestVisit opens the last test file
  • Configure custom test commands per project

Next

How do you yank a single word into a named register?