0
点赞
收藏
分享

微信扫一扫

软件测试学习笔记丨Pytest常用的异常处理方法

本文转自测试人社区,原文链接:https://ceshiren.com/t/topic/30126

pytest常用的异常处理方法

  • try…except
  • pytest.raise()

异常处理方法try…except

try:
    #可能产生异常的代码块
except Error1,Error2,... as e:
    #处理异常的代码块1
except Error3, Error4,... as e:
    #处理异常的代码块2
except Exception:
    #处理其他异常

异常处理方法pytest.raise()

  • 可以捕获特定的异常
  • 获取捕获的异常的细节(异常类型,异常信息)
  • 发生异常,后面的代码将不会被执行

软件测试学习笔记丨Pytest常用的异常处理方法_自测试

def test_raise():
    with pytest.raises(ValueError,match='must be 0 or None'):
        raise ValueError("value must be 0 or None")

def test_raise1():
    with pytest.raises(ValueError) as exc_info:
        raise ValueError("value must be 42")
    assert exc_info.type is ValueError
    assert exc_info.value.args[0] == "value must be 42"

举报

相关推荐

0 条评论