我怎样才能让 pytest 不捕获异常 [英] How can I get pytest to not catch exceptions

查看:65
本文介绍了我怎样才能让 pytest 不捕获异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在 vscode 调试器中运行 pytest 时出现未捕获的异常"检查,并且有测试错误,没有未捕获的异常发生,因为pytest捕获它们做它的结果报告.我怎么能告诉 pytest 只让异常发生?以便我可以在 vscode 调试器中捕获它们?

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 do its results reporting. How can I tell pytest to just let the exceptions happen? So that I can catch them in the vscode debugger?

基本上我想要像 --pdb 这样的行为,但我希望它启动 vscode 调试器而不是 pdb.标志 --pdbcls 听起来很有希望,但不确定要给它什么 :.

Basically I want behavior like --pdb but I want it to launch the vscode debugger not pdb. The flag --pdbcls sounds promising but not sure what <module>:<class> to give it.

注意:通常我只会在引发异常时中断.但是我们的代码有大量引发但捕获的异常,所以这个选项没有用.

Note: Normally I would just have it break on Raised Exceptions. But our code has tons of raised-but-caught exceptions so this option is not useful.

这是一段关于在调试 pytest 测试时引发 AssertionError 时 vscode 不会中断的视频:

Here's a video of vscode not breaking when an AssertionError fires while debugging a pytest test:

@rioV8 下面的建议确实打破了异常,但由于某种原因没有堆栈,这意味着您无法从那里进行调试:

@rioV8's suggestion below does break the on the exception, but for some reason there is no stack which means you can't debug from there:

我一定遗漏了一些东西,因为似乎没有其他人需要这种能力.但对我来说,这似乎绝对是测试框架和调试器所能做的最基本最简单的事情:作为开发人员,我想从出现错误的地方进行调试.

I must be missing something because no one else seems to need this capability. But to me this seems like absolutely the most basic simplest thing one could do with a testing framework and a debugger: as a developer I want to debug from the point where the error is raised.

人们使用带有 pytest 的调试器的方式肯定是完全不同的,我忽略了一些明显的技术.

There must be some totally other way people are using a debugger with pytest, some obvious technique I'm overlooking.

推荐答案

我提出了一个问题 与 pytest,他们提出了一个可行的建议.将此添加到您的 conftest.py:

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

然后在您的 "request":"test" launch.json 中,您可以切换该环境变量:

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

如果设置了 _PYTEST_RAISE 并且您只选中了未捕获的异常框,您将在未捕获的异常之前未引发的地方中断.

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.

我还用 debugpy 打开了一个问题,他们有一些想法,但今天没有解决方案._PYTEST_RAISE 技巧可以 100% 为我解决这个问题.

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.

这篇关于我怎样才能让 pytest 不捕获异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆