Pass a Python unittest if an exception isn't raised

前端 未结 3 1973
再見小時候
再見小時候 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:15

    Many of the comments on this page treat errors and failures as equivalent, which they are not. The right solution in my opinion is to explicitly fail the test if the exception is raised. E.g.:

    def test_does_not_error(self):
        try:
            code_under_test()
        except ThatException:
            self.fail("code_under_test raised ThatException")
    

提交回复
热议问题