richardhapb/pytest.nvim: Neovim plugin for Python testing

by oqtey
richardhapb/pytest.nvim: Neovim plugin for Python testing

Testing integrated in neovim with pytest. Include Docker support. This project is in progress, I will be adding more features in the future and I open to contributions.

These instructions will help you set up and use pytest.nvim in your Neovim environment.

  • Neovim 0.5.0 or later
  • pytest in your environment (pip install pytest)
  1. Install the pytest.nvim plugin using your preferred plugin manager:

    Lazyvim:

    {
      "richardhapb/pytest.nvim",
      opts = {}
    }

    Packer:

    use {
      "richardhapb/pytest.nvim",
      opt = true
    }

    Vim-Plug:

    Plug 'richardhapb/pytest.nvim'
  1. Load the pytest plugin in your Neovim configuration if you haven’t already done so. For example:

    require('pytest').setup()
  2. Use the :Pytest command to run the tests in the current buffer.

    • To check the entire buffer:

    • To check the output of the tests:

    • You can attach the test to the current buffer, this runs test on save:

    • You can detach the test from the current buffer:

    • Docker enable on the way

    • Docker disabled on the way

The default keybinding that runs :Pytest is T.

The plugin provides the following default keymap:

  • TT – Run pytest for the current file (normal mode)
  • Ta – Attach pytest to the current buffer (normal mode)
  • Td – Detach pytest from the current buffer (normal mode)

Default settings, is not necessary to set up, but you can change the settings in your configuration file.

docker_compose_file=”docker-compose.yml”, — This is the default docker compose file name
docker_compose_service=”app”, — This is for looking for the docker path in docker compose
enable_docker_compose = true, — Enable docker compose support
},

— You can overwrite this callback with your custom keymaps,
— this is called when open a Python file and buffer number is passed as an argument
keymaps_callback = function(bufnr)
vim.keymap.set(‘n’, ‘TT’, ‘Pytest‘, { buffer = bufnr, desc=”Run Pytest” })
vim.keymap.set(‘n’, ‘Ta’, ‘PytestAttach‘, { buffer = bufnr, desc=”Attach Pytest to buffer” })
vim.keymap.set(‘n’, ‘Td’, ‘PytestDetach‘, { buffer = bufnr, desc=”Detach Pytest” })
end
}”>

require 'pytest'.setup {
   docker = {
      enabled = true,  -- Enable docker support
      container = 'app-1',  -- Container where the tests will be run
      docker_path = '/usr/src/app',  -- This is the default path, if you use docker compose this is obtained from the docker compose file
      docker_path_prefix = 'app', -- This is the prefix for the path in the cwd in your local, for example: root/app/
      docker_compose_file = 'docker-compose.yml',  -- This is the default docker compose file name
      docker_compose_service = 'app',  -- This is for looking for the docker path in docker compose
      enable_docker_compose = true,  -- Enable docker compose support
   },

   -- You can overwrite this callback with your custom keymaps,
   -- this is called when open a Python file and buffer number is passed as an argument
   keymaps_callback = function(bufnr)
      vim.keymap.set('n', 'TT', 'Pytest', { buffer = bufnr, desc = 'Run Pytest' })
      vim.keymap.set('n', 'Ta', 'PytestAttach', { buffer = bufnr, desc = 'Attach Pytest to buffer' })
      vim.keymap.set('n', 'Td', 'PytestDetach', { buffer = bufnr, desc = 'Detach Pytest' })
   end
}

Options can be callbacks, for example:

require 'pytest'.setup {
   docker = {
      enabled = function()
         return vim.fn.getcwd():match(".*/(.*)$") == "work"  -- Only enable docker if the last dir of cwd is "work"
      end,

      container = function()
         local app = utils.get_my_awesome_app()
         return app .. '-version-2'
      end
   },
}

Related Posts

Leave a Comment