Run setUp only once for a set of automated tests

前端 未结 8 1262
逝去的感伤
逝去的感伤 2020-12-13 05:15

My Python version is 2.6.

I would like to execute the test setUp method only once since I do things there which are needed for all tests.

My idea was to crea

8条回答
  •  渐次进展
    2020-12-13 06:06

    I'm using Python 3 and found that the cls reference is also available in the setup method and so the following works:

    class TestThing(unittest.TestCase):
    
      @classmethod
      def setUpClass(cls):
        cls.thing = Thing() # the `thing` is only instantiated once
    
      def setup(self):
        self.thing = cls.thing # ...but set on each test case instance
    
      def test_the_thing(self):
        self.assertTrue(self.thing is not None)
    

提交回复
热议问题