Skip to content
Edit this page

Testing

CodeArt runs tests from the editor with neotest. Each language pack brings a neotest adapter, so the tests of every language you turn on work without extra setup. neotest loads only when you press a <leader>T key or run :CodeArtTest, so it costs nothing at startup.

Quick start

  1. Open a test file.
  2. Press <leader>Tn to run the test under the cursor, or <leader>Tf for the whole file.
  3. Passed and failed tests get a sign and a short result at the end of the line. <leader>To shows the output of the test under the cursor and <leader>Ts opens the summary of every test in the project.
Keys Command What it does
<leader>Tn :CodeArtTest nearest Run the test under the cursor (or the nearest one above it)
<leader>Tf :CodeArtTest file Run every test in this file
<leader>Ta :CodeArtTest project Run every test in the project (the working directory)
<leader>Tl :CodeArtTest last Run the last test again
<leader>Td :CodeArtTest debug Debug the nearest test (see Debugging a test)
<leader>Ts :CodeArtTest summary Toggle the summary window
<leader>To :CodeArtTest output Show the output of the test under the cursor
<leader>TO :CodeArtTest panel Toggle the output panel (output of every run)
<leader>Tw :CodeArtTest watch Watch this file and rerun its tests when it or what it imports changes
<leader>TS :CodeArtTest stop Stop the running tests

:CodeArtTest alone runs the nearest test. neotest's own :Neotest command works too.

Failed tests also show up as diagnostics in the file, so <leader>lgj / <leader>lgk jump between them.

Per language

Pack Adapter Test runner Needs
lua neotest-plenary plenary.nvim's busted-style harness (describe / it in *_spec.lua) – (plenary.nvim is already installed). A tests/minimal_init.lua in the project is used when there is one
python neotest-python pytest, or unittest when pytest is missing Python; pytest in the project's environment for pytest tests. The virtual environment (VIRTUAL_ENV, CONDA_PREFIX, .venv, venv) is used, like the debugger
go neotest-golang go test Go
web neotest-jest and neotest-vitest Jest or Vitest from the project's node_modules Node.js and npm install in the project. Each adapter only takes projects that list its runner in package.json
rust rustaceanvim's adapter cargo test, through rust-analyzer's runnables cargo and rust-analyzer. Tests are found once rust-analyzer has loaded the project; save the file if the summary is empty right after opening it
cpp neotest-ctest CTest: GoogleTest, Catch2, doctest and CppUTest tests that CMake registers CMake. See C and C++
java neotest-java JUnit 5 (JUnit Platform console launcher) Java, jdtls running for the project (the class path comes from it) and :NeotestJava setup once to download the launcher
csharp neotest-vstest The .NET SDK's test platform (xUnit, NUnit, MSTest, ...; C# and F#) .NET SDK (dotnet)
php neotest-phpunit PHPUnit (vendor/bin/phpunit) PHP and composer install in the project
ruby neotest-rspec RSpec (*_spec.rb), with bundle exec in a Bundler project. Minitest runs from the Rakefile's tasks (:CodeArtRun) Ruby and bundle install in the project
dart neotest-dart flutter test when Flutter is installed, else dart test The Dart or Flutter SDK
elixir neotest-elixir ExUnit (mix test) Elixir

:checkhealth codeart has a "Testing (neotest)" section that shows each active pack's adapter and whether its test runner is on PATH.

C and C++

neotest-ctest runs what CTest knows about, so:

  • The project must use CMake and register its tests, for example with gtest_discover_tests() (GoogleTest) or catch_discover_tests() (Catch2).
  • The adapter does not build. Build the tests first (cmake --build build), then run them. cmake-tools.nvim is a good companion if you want builds from the editor.
  • CTestTestfile.cmake must be at most two folders below the project root (for example build/ or build/Debug/).
  • Projects without CMake (plain Makefiles, Meson, Bazel) are not covered.

Java

Run :NeotestJava setup once after turning the Java pack on; it downloads the JUnit Platform console launcher. Open a Java file first so jdtls starts: neotest-java asks it for the class path and compiles through it.

C

neotest-dotnet, the older C# adapter, is looking for a new maintainer and its author points to neotest-vstest, which CodeArt uses. Options go in vim.g.neotest_vstest (for example in config.other_configs), see its README.

Debugging a test

<leader>Td runs the nearest test under the debugger with the pack's debug adapter: debugpy (Python), delve (Go), codelldb (Rust, C/C++), js-debug (JavaScript and TypeScript), java-debug (Java) and netcoredbg (C#). It needs nvim-dap (on by default) and the adapter installed by the pack. Set breakpoints first with <F9> or <leader>dbt. See Debugging.

Options

neotest options go in config.neotest and are merged over CodeArt's:

M.config = {
  neotest = {
    -- open the output window after every run
    output = { open_on_run = true },
    -- options for a pack's adapter, by its module name
    adapter_options = {
      ["neotest-python"] = { runner = "unittest", args = { "--verbose" } },
      ["neotest-golang"] = { runner = "gotestsum" },
    },
  },
}
  • adapter_options["<module>"] is passed to that adapter when neotest loads, over the pack's own options (see each adapter's README for its options).
  • adapters in config.neotest adds adapters to the ones from the packs instead of replacing them. For an adapter of your own it is usually simpler to write a language pack with a test field.
  • CodeArt's defaults: results as virtual text and signs, the output window does not open by itself and the quickfix list is filled but not opened.

To turn neotest off: M.disable_plugins = { neotest = true }. The <leader>T keys, :CodeArtTest and the adapters go away.

Adapters in your own packs

A pack lists the adapter plugin in test_plugins and returns the adapter from test:

-- ~/.config/nvim/user/languages/haskell.lua
return {
  description = "Haskell",
  filetypes = { "haskell" },
  lsp = { "hls" },
  mason = { "haskell-language-server" },
  test_plugins = { { "mrcjkb/neotest-haskell" } },
  test = function()
    return require("neotest-haskell")({})
  end,
}

test_plugins are lazy.nvim specs; they are installed and loaded only while the pack is in languages and neotest is on.