nvim-dap-python icon indicating copy to clipboard operation
nvim-dap-python copied to clipboard

Pytest tests do not break on error

Open Thieso opened this issue 3 years ago • 1 comments

Hi,

When debugging pytest tests and the test contains an error than the debugger does not stop there but the test just results in a fail and exits. I think it would be much better to let the debugger stop and the point of failure such that it can be debugged correctly.

A possible solution is mentioned here: https://stackoverflow.com/questions/62419998/how-can-i-get-pytest-to-not-catch-exceptions/62563106#62563106

There I think you basically tell pytest to not raise exceptions such that the debugger can handle them. For this you need to add the following to your conftest.py:

import os
import pytest

if os.getenv('_PYTEST_RAISE', "0") != "0":

    @pytest.hookimpl(tryfirst=True)
    def pytest_exception_interact(call):
        raise call.excinfo.value

    @pytest.hookimpl(tryfirst=True)
    def pytest_internalerror(excinfo):
        raise excinfo.value

And then also add the env dict in the test configuration in dap-python.lua

load_dap().run({                                                                                                                                                                       
name = table.concat(prune_nil({classname, methodname}), '.'),                                                                                                                        
type = 'python',                                                                                                                                                                     
request = 'launch',                                                                                                                                                                  
module = test_runner,                                                                                                                                                                
args = args,                                                                                                                                                                         
console = opts.console,                                                                                                                                                              
env = {                                                                                                                                                                              
       ["_PYTEST_RAISE"] = "1",                                                                                                                                                         
     },                                                                                                                                                                                   
})    

This seems to be not the best solutions but are there alternatives?

Thieso avatar Jan 21 '22 09:01 Thieso

I would be open to add a config_overrides option to the opts so that you can extend the configuration table as you want and define the env as part of your test_method / test_class calls.

So you could do something like test_method { config_overrides = { env = { ["_PYTEST_RAISE"] = 1 }} (Or maybe just overrides instead of config_overrides, not sure)

Not sure if there are any other alternatives other than what you're doing via the _PYTEST_RAISE variable.

mfussenegger avatar Apr 28 '22 09:04 mfussenegger

Allowed config overrides in https://github.com/mfussenegger/nvim-dap-python/commit/16e1cab806b7b43c5c7eda8f416bedb2f48ef60e

mfussenegger avatar Aug 18 '22 08:08 mfussenegger