Run setUp only once for a set of automated tests

前端 未结 8 1232
逝去的感伤
逝去的感伤 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:11

    setup_done is a class variable, not an instance variable.

    You are referencing it as an instance variable:

    self.setup_done

    But you need to reference it as a class variable:

    mySelTest.setup_done

    Here's the corrected code:

    class mySelTest(unittest.TestCase):
        setup_done = False
    
        def setUp(self):
            print str(mySelTest.setup_done)
    
            if mySelTest.setup_done:
                return
            mySelTest.setup_done = True
            print str(mySelTest.setup_done)
    

提交回复
热议问题