You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用nvim-dap运行单个Python测试及配置Django项目调试命令?

LunarVim Python调试配置优化方案

一、修复变量悬停查看功能

你的配置里<leader>di键位被重复绑定,导致原有的变量悬停失效,以下是修正后的键位配置,同时保留视觉模式下的变量悬停:

-- 变量悬停:普通模式下查看光标处变量,视觉模式下查看选中内容
lvim.keys.normal_mode["<leader>di"] = "lua require'dap.ui.widgets'.hover()<CR>"
lvim.keys.visual_mode["<leader>di"] = "lua require'dap.ui.variables'.visual_hover()<CR>"
-- 查看当前所有作用域的变量列表
lvim.keys.normal_mode["<leader>d?"] = "lua local widgets=require'dap.ui.widgets';widgets.centered_float(widgets.scopes)<CR>"

调试时,普通模式下按<leader>di就能查看光标所在变量的详情;视觉模式选中变量后按相同组合键,可查看选中内容的变量信息。

二、配置单个测试运行调试

针对pytest或unittest框架,添加专门的调试配置,支持运行当前测试函数或整个测试文件:

require("dap").configurations.python = {
  -- 原有单文件调试配置
  {
    type = 'python';
    request = 'launch';
    name = "Launch file";
    program = "${file}";
    pythonPath = function()
      return '/usr/bin/python3'
    end;
  },
  -- 运行当前测试函数(适配pytest)
  {
    type = "python",
    request = "launch",
    name = "Run current test function",
    program = vim.fn.exepath("pytest"),
    args = { "-k", "${selected_text}", "${file}" },
    pythonPath = function()
      return '/usr/bin/python3'
    end,
    -- 仅在测试文件中显示该配置
    condition = function()
      return vim.fn.expand("%:t"):match("test_.*%.py") ~= nil or vim.fn.expand("%:t"):match(".*_test%.py") ~= nil
    end,
  },
  -- 运行当前测试文件(适配pytest)
  {
    type = "python",
    request = "launch",
    name = "Run current test file",
    program = vim.fn.exepath("pytest"),
    args = { "${file}" },
    pythonPath = function()
      return '/usr/bin/python3'
    end,
    condition = function()
      return vim.fn.expand("%:t"):match("test_.*%.py") ~= nil or vim.fn.expand("%:t"):match(".*_test%.py") ~= nil
    end,
  },
}

使用方法:

  • 打开测试文件,光标定位到测试函数名上,启动调试时选择"Run current test function",即可调试单个测试函数
  • 直接选择"Run current test file",可调试整个测试文件

如果使用unittest框架,将program改为"/usr/bin/python3",args改为["-m", "unittest", "${file}:${selected_text}"]即可适配。

三、Django项目调试配置

修改dap配置,添加Django专属调试项,支持运行开发服务器或指定测试:

require("dap").configurations.python = {
  -- 保留原有其他配置...
  -- Django 启动开发服务器
  {
    type = "python",
    request = "launch",
    name = "Django Runserver",
    program = "${workspaceFolder}/manage.py",
    args = { "runserver", "--noreload" },
    pythonPath = function()
      -- 优先使用项目虚拟环境的Python,无虚拟环境则用系统Python
      local venv_path = vim.fn.getcwd() .. "/venv/bin/python"
      if vim.fn.filereadable(venv_path) == 1 then
        return venv_path
      else
        return "/usr/bin/python3"
      end
    end,
    django = true,
    -- 仅在Django项目中显示该配置
    condition = function()
      return vim.fn.filereadable(vim.fn.getcwd() .. "/manage.py") == 1
    end,
  },
  -- Django 运行指定测试
  {
    type = "python",
    request = "launch",
    name = "Django Run Test",
    program = "${workspaceFolder}/manage.py",
    args = { "test", "${file}:${selected_text}" },
    pythonPath = function()
      local venv_path = vim.fn.getcwd() .. "/venv/bin/python"
      if vim.fn.filereadable(venv_path) == 1 then
        return venv_path
      else
        return "/usr/bin/python3"
      end
    end,
    django = true,
    condition = function()
      return vim.fn.filereadable(vim.fn.getcwd() .. "/manage.py") == 1
    end,
  },
}

使用说明:

  • 在Django项目中启动调试时,选择"Django Runserver"即可启动开发服务器并进入调试模式
  • 打开Django测试文件,定位到目标测试函数,选择"Django Run Test"可调试单个测试函数

内容的提问来源于stack exchange,提问作者Axeltherabbit

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 05:03:36