How to fail a python unittest if the setUpClass throws exception

前端 未结 4 2182
我在风中等你
我在风中等你 2021-02-13 21:49

I am having little trouble using the python setUpClass.

For example consider the following case

class MyTest(unittest.case.TestCase):

    @classmethod
          


        
4条回答
  •  刺人心
    刺人心 (楼主)
    2021-02-13 22:41

    import contextlib
    
    class MyTestCase(unitest.TestCase):
    
        @classmethod
        def setUpClass(cls):
            with contextlib.ExitStack() as stack:
                # ensure teardown is called if error occurs
                stack.callback(cls.tearDownClass)
    
                # Do the things here!
    
                # remove callback at the end if no error found
                stack.pop_all()
    

提交回复
热议问题