Python unittest - opposite of assertRaises?

前端 未结 10 677
甜味超标
甜味超标 2020-12-04 05:36

I want to write a test to establish that an Exception is not raised in a given circumstance.

It\'s straightforward to test if an Exception is raise

10条回答
  •  悲&欢浪女
    2020-12-04 05:56

    I am the original poster and I accepted the above answer by DGH without having first used it in the code.

    Once I did use I realised that it needed a little tweaking to actually do what I needed it to do (to be fair to DGH he/she did say "or something similar" !).

    I thought it was worth posting the tweak here for the benefit of others:

        try:
            a = Application("abcdef", "")
        except pySourceAidExceptions.PathIsNotAValidOne:
            pass
        except:
            self.assertTrue(False)
    

    What I was attempting to do here was to ensure that if an attempt was made to instantiate an Application object with a second argument of spaces the pySourceAidExceptions.PathIsNotAValidOne would be raised.

    I believe that using the above code (based heavily on DGH's answer) will do that.

提交回复
热议问题