Python - test that succeeds when exception is not raised

前端 未结 3 1491
时光取名叫无心
时光取名叫无心 2021-02-19 04:20

I know about unittest Python module.

I know about assertRaises() method of TestCase class.

I would like to write a test th

3条回答
  •  悲&欢浪女
    2021-02-19 04:44

    I use this pattern for the kind of assertion you've asked:

    with self.assertRaises(Exception):
        try:
            doStuff()
        except:
            pass
        else:
            raise Exception
    

    It will fail exactly when exception is raised by doStuff().

提交回复
热议问题