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

How do I run tests directly from Vim without switching to a terminal?

Answer

:TestNearest

Explanation

The vim-test plugin by Janko Marohnić provides a universal interface for running tests from within Vim. It auto-detects your testing framework, figures out which test to run based on your cursor position, and executes it — all without you needing to manually type test commands or switch to a terminal.

Core commands

:TestNearest    " run the test nearest to the cursor
:TestFile       " run all tests in the current file
:TestSuite      " run the entire test suite
:TestLast       " re-run the last test that was executed
:TestVisit      " open the last test file that was run

The most powerful command is :TestNearest — it parses the current file, finds the test function or block closest to your cursor, and runs just that single test. This is the fastest feedback loop possible.

Supported frameworks

Vim-test supports a huge number of testing frameworks out of the box:

Language Frameworks
JavaScript Jest, Mocha, Vitest, Cypress
Python pytest, unittest, nose, django
Ruby RSpec, Minitest, Cucumber
Go go test, Ginkgo, richgo
Rust cargo test
Elixir ExUnit, ESpec
PHP PHPUnit, Pest
Java Maven, Gradle

The plugin auto-detects which framework to use based on your project structure and configuration files.

Recommended key mappings

Add these to your vimrc for rapid test execution:

nmap <Leader>tn :TestNearest<CR>
nmap <Leader>tf :TestFile<CR>
nmap <Leader>ts :TestSuite<CR>
nmap <Leader>tl :TestLast<CR>
nmap <Leader>tv :TestVisit<CR>

Choosing a test strategy

Vim-test supports multiple strategies for how tests are executed:

let test#strategy = 'dispatch'     " async via vim-dispatch (recommended)
let test#strategy = 'vimterminal'  " Vim's built-in terminal
let test#strategy = 'neovim'       " Neovim's built-in terminal
let test#strategy = 'toggleterm'   " via toggleterm.nvim
let test#strategy = 'basic'        " synchronous (blocks Vim)

Using dispatch as the strategy sends test output to the quickfix list, so you can jump directly to failing test lines with :cnext and :cprev.

The :TestLast workflow

One of the most productive patterns with vim-test:

  1. Write a failing test and run it with :TestNearest
  2. Switch to your implementation file and write code
  3. Press :TestLast to re-run that same test without leaving your implementation file
  4. Iterate until the test passes

This eliminates the context-switch cost of jumping back to the test file just to re-run it.

Transforming test commands

Customize the generated test command with transformations:

" Add --verbose flag to all pytest runs
let test#python#pytest#options = '--verbose'

" Use a custom test runner script
let test#custom_transformations = {'docker': function('DockerTransform')}
function! DockerTransform(cmd) abort
  return 'docker-compose exec app ' . a:cmd
endfunction
let test#transformation = 'docker'

Tips

  • Combine vim-test with vim-dispatch for the best experience: tests run asynchronously and errors populate the quickfix list
  • Use :TestVisit to quickly jump back to the last test you ran — great for bouncing between implementation and test files
  • vim-test works with vim-projectionist for alternate file navigation between source and test files
  • The plugin is framework-agnostic — switching between projects with different test tools requires zero reconfiguration
  • For monorepo setups, vim-test respects the nearest test configuration file when determining how to run tests

Next

How do I edit multiple lines at once using multiple cursors in Vim?