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 50 forks source link

Debugging pytest test method fails on dap.continue() but works with dep.test_method() #127

Closed PhilippFeO closed 10 months ago

PhilippFeO commented 10 months ago

Hey,

I recently set up nvim-dap and nvim-dap-python and everything works fine on normal Python files.

When I want to debug a pytest test function with dap.continue() it starts, ie. dap-ui loads but everything is terminated after a short flash of the UI printing 'Session terminated'. In contrast, invoking with dap.test_method() does the job, ie. dap-ui loads and the program halts on the breakpoint. Any hints are welcome!

My configuration

nvim-dap-python

local dap_python = require('dap-python')
-- dap.continue does not work, test_method() does
dap_python.setup('~/.venv/debugpy/bin/python')
dap_python.test_runner = 'pytest'

I have no pytest.ini in my project.

pip list

I tinkered around with different virtual environments. Here is the one of the project I wish to be able to debug with. (To be honest, currently it is not obvious to me when nvim-dap-python finds the virtual env via $VIRTUAL_ENV itself and when not.)

Package        Version
-------------- -------
debugpy        1.8.0
exceptiongroup 1.2.0
iniconfig      2.0.0
markdown-it-py 3.0.0
mdurl          0.1.2
packaging      23.2
pip            22.0.2
pluggy         1.4.0
Pygments       2.17.2
pytest         7.4.4
PyYAML         6.0.1
rich           13.7.0
setuptools     59.6.0
tomli          2.0.1
wcwidth        0.2.12

And also pip list of ~/.venv/debugpy/bin/activate:

Package    Version
---------- -------
debugpy    1.8.0
pip        22.0.2
setuptools 59.6.0

I hope these information are sufficient. Let me know, in case you need more!


Thank you for your work!

PhilippFeO commented 10 months ago

Time passed and I have figured it out. I had to add the following to my config:

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",
    }
}

I was also able to reproduce my issue above on non-test methods. dap.continue() runs the file, ie. debugging a method doesn't work if it is not called, fi. in a if __name__ == '__main__': block. dap.test_method() does what I expected from dap.continue(). Although spending a decent amount of time on informing about debugging in Neovim (YouTube, docs). This was not obvious to me – but makes sense now (like everything makes sense retrospectively). And to be honest, when my issue arised, I hadn't perfect working examples – again, obvious retrospectively.


I will leave this issue open in case some flaws arise since I am still exploring the plugin and don't feel confident enough. You can either close the issue, when I provided the correct solution. Otherwise, I will do it next week. And again, thank you for making debugging in Neovim possible :rocket:

mfussenegger commented 10 months ago

The default "Launch file" configuration from nvim-dap-python will run the file similarly to if it would've been launched in the command line via python <myfile>.py, so if you use it on a test module it won't work unless it has an entry point.

E.g. for plain unittest you could import the main function and call that within a if __name__ == '__main__': block (https://docs.python.org/3/library/unittest.html#unittest.main)

But your solution to add a dedicated pytest configuration is also fine and has the advantage that it works without changes to pytest files

PhilippFeO commented 9 months ago

Thank you for answering such a beginners question :)