Pass a Python unittest if an exception isn't raised

前端 未结 3 1970
再見小時候
再見小時候 2020-12-03 07:54

In the Python unittest framework, is there a way to pass a unit test if an exception wasn\'t raised, and fail with an AssertRaise otherwise?

3条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-03 08:32

    Simply call your functionality, e.g. do_something(). If an unhandled exception gets raised, the test automatically fails! There is really no reason to do anything else. This is also the reason why assertDoesNotRaise() does not exist.


    Credit: comment by Sven

提交回复
热议问题