Python - test that succeeds when exception is not raised

前端 未结 3 1492
时光取名叫无心
时光取名叫无心 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:53

    def runTest(self):
        try:
            doStuff()
        except:
            self.fail("Encountered an unexpected exception.")
    

    UPDATE: As liw.fi mentions, the default result is a success, so the example above is something of an antipattern. You should probably only use it if you want to do something special before failing. You should also catch the most specific exceptions possible.

提交回复
热议问题