mfussenegger / nvim-dap-python

An extension for nvim-dap, providing default configurations for python and methods to debug individual test methods or classes.
GNU General Public License v3.0
571 stars 51 forks source link

Breakpoints not working (with pytest-cov) #74

Closed jeanlucthumm closed 1 year ago

jeanlucthumm commented 1 year ago

I set a breakpoint in my test file and it seems to pop up in the DAP logs:

[ DEBUG ] 2022-12-11T12:13:01Z-0800 ] ...nvim/site/pack/packer/start/nvim-dap/lua/dap/session.lua:1450 ]    "request"   {
  arguments = {
    breakpoints = { {
        line = 8
      } },
    lines = { 8 },
    source = {
      name = "waiter_test.py",
      path = "/home/jeanluc/Code/waiter_test.py"
    },
    sourceModified = false
  },
  command = "setBreakpoints",
  seq = 3,
  type = "request"
}
[ DEBUG ] 2022-12-11T12:13:01Z-0800 ] ...nvim/site/pack/packer/start/nvim-dap/lua/dap/session.lua:915 ] {
  body = {
    breakpoints = { {
        id = 0,
        line = 8,
        source = {
          name = "waiter_test.py",
          path = "/home/jeanluc/Code/waiter_test.py"
        },
        verified = true
      } }
  },
  command = "setBreakpoints",
  request_seq = 3,
  seq = 6,
  success = true,
  type = "response"
}

But when I launch via the test_method() function, the program runs but never breaks.

I looked at #17 and tried to change my python version. At first I was using /usr/bin/python (Python 3.10.8) since my global install had debugpy. Then I created custom virtual env with Python 3.9.2 but the problem persists.

My setup for the plugin:

    use {
        'mfussenegger/nvim-dap-python',
        requires = {'mfussenegger/nvim-dap'},
        config = function()
            require'dap-python'.setup('~/.virtualenv/debug/bin/python')
            require'dap-python'.test_runner = 'pytest'
        end
    }
jeanlucthumm commented 1 year ago

The debugger works when I'm inside a non-test, main() function, set a breakpoint, and do lua require'dap'.continue(), so maybe there's something wrong with pytest setups specifically?

philkoch commented 1 year ago

I'm having a similar issue. Configured nvim-dap-python with examples from the docs

local mason_path = vim.fn.glob(vim.fn.stdpath "data" .. "/mason/")
require("dap-python").setup(mason_path .. "packages/debugpy/venv/bin/python")
require("dap-python").test_runner = "pytest"
-- mappings
lvim.builtin.which_key.mappings["dm"] = { "<cmd>lua require('dap-python').test_method()<cr>", "Test Method" }
lvim.builtin.which_key.mappings["df"] = { "<cmd>lua require('dap-python').test_class()<cr>", "Test Class" }

When invoking the test_method-action in a pytest-unittest file, the debugger starts as expected, but does not stop at breakpoints. Same as for the others, the debugger and breakpoints work fine when used in "normal" python modules.

mfussenegger commented 1 year ago

A minimal example with reproduction steps would be good.

Using a simple:

def inc(x):
    return x + 1

def test_answer():
    assert inc(3) == 5

And running :lua require('dap-python').test_method() with the cursor in test_answer, test_runner set to pytest, and a breakpoint in the inc body works fine for me.

philkoch commented 1 year ago

Sorry for the late reply, I didn't find time to test it earlier. I found out what broke it for me. I'm using a poetry environment in which all dev-tools are configured via the pyproject.toml. PyTest is also configured in there:

[tool.pytest.ini_options]
addopts = "--cov" # <-- no stopping at breakpoints with --cov
testpaths = ["tests"]

When the --cov is added nvim-dap-python does not stop at breakpoints, when I remove it it works fine.

mfussenegger commented 1 year ago

Looks like this is a pytest-cov / debugpy limitation. See https://github.com/microsoft/debugpy/issues/863#issuecomment-1059040881