pythondebuggingneovimlunarvim

How do I run a single python test with nvim-dap?


This is my config for lunarvim, I wanted to be able to hover and run a single test.

Also how do I set the project command that I want to run if for instance it's a django project? At the moment it's just trying to run the current file

-- nvim-dap
require("dap").adapters.python = {
  type = 'executable';
  command = 'python3';
  args = { '-m', 'debugpy.adapter' };
}
require("dap").configurations.python = {
  {
    type = 'python';
    request = 'launch';
    name = "Launch file";
    program = "${file}";
    pythonPath = function()
      return '/usr/bin/python3'
    end;
  },
}


vim.fn.sign_define('DapBreakpoint', { text = '🟥', texthl = '', linehl = '', numhl = '' })
vim.fn.sign_define('DapStopped', { text = '⭐️', texthl = '', linehl = '', numhl = '' })
lvim.keys.normal_mode["<leader>dh"] = "lua require'dap'.toggle_breakpoint()<CR>"
-- lvim.keys.normal_mode["S-k>"] = "lua require'dap'.step_out()<CR>"
-- lvim.keys.normal_mode["S-l>"] = "lua require'dap'.step_into()<CR>"
-- lvim.keys.normal_mode["S-j>"] = "lua require'dap'.step_over()<CR>"
lvim.keys.normal_mode["<leader>ds"] = "lua require'dap'.stop()<CR>"
lvim.keys.normal_mode["<leader>dn"] = "lua require'dap'.continue()<CR>"
lvim.keys.normal_mode["<leader>dk"] = "lua require'dap'.up()<CR>"
lvim.keys.normal_mode["<leader>dj"] = "lua require'dap'.down()<CR>"
lvim.keys.normal_mode["<leader>d_"] = "lua require'dap'.disconnect();require'dap'.stop();require'dap'.run_last()<CR>"
lvim.keys.normal_mode["<leader>dr"] = "lua require'dap'.repl.open({}, 'vsplit')<CR><C-w>l"
lvim.keys.normal_mode["<leader>di"] = "lua require'dap.ui.variables'.hover()<CR>"
lvim.keys.visual_mode["<leader>di"] = "lua require'dap.ui.variables'.visual_hover()<CR>"
lvim.keys.normal_mode["<leader>d?"] = "lua require'dap.ui.variables'.scopes()<CR>"
-- lvim.keys.normal_mode["leader>de"] = "lua require'dap'.set_exception_breakpoints({"all "})<CR>"
lvim.keys.normal_mode["<leader>da"] = "lua require'debugHelper'.attach()<CR>"
lvim.keys.normal_mode["<leader>dA"] = "lua require'debugHelper'.attachToRemote()<CR>"
lvim.keys.normal_mode["<leader>di"] = "lua require'dap.ui.widgets'.hover()<CR>"
lvim.keys.normal_mode["<leader>d?"] = "lua local widgets=require'dap.ui.widgets';widgets.centered_float(widgets.scopes)<CR>"

Solution

  • require('dap').configurations.python=
    {
        name= "Pytest: Current File",
        type= "python",
        request= "launch",
        module= "pytest",
        args= {
            "${file}",
            "-sv",
            "--log-cli-level=INFO",
            "--log-file=test_out.log"
        },
        console= "integratedTerminal",
      }
    

    You can add this configuration and put a breakpoint on your particular test and run it