在 Pytest 中抛出异常(失败:未抛出 ValueError 类)

2024-09-28 21:23:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我在捕获unitest代码中的异常时遇到了一个问题。 下面是我的代码

def get_param(param)        
    if param is None:
        raise ValueError('param is not set')

def test_param():
    with pytest.raises(ValueError) as e:
        get_param()

问题是,当函数没有引发异常时,test_param()会失败,并出现以下错误。你知道吗

Failed: DID NOT RAISE <class 'ValueError'>

当get_param(param)函数抛出异常时,它可以正常工作。你知道吗


Tags: 函数代码testnonegetifparamis