How can I get pytest to not catch exceptions

前端 未结 2 1584
生来不讨喜
生来不讨喜 2021-01-20 08:15

When I run pytest in the vscode debugger with "Uncaught Exceptions" checked, and there are test errors, no uncaught exceptions occur, because pytest catches them d

2条回答
  •  迷失自我
    2021-01-20 08:48

    I raised an issue with pytest and they made a suggestion that works. Add this 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
    

    Then in your "request": "test" launch.json you can toggle that env var:

        {
            "name": "Debug Tests",
            "type": "python",
            "request": "test",
            "console": "integratedTerminal",
            "justMyCode": true,
            "env": {
                "_PYTEST_RAISE": "1"
            },
        }
    

    If _PYTEST_RAISE is set and you check only the Uncaught Exceptions box you will break right where the uncaught exception was raised not before.

    I also opened an issue with debugpy and they had some ideas, but no solution today. The _PYTEST_RAISE trick works 100% solves this for me.

提交回复
热议问题