سؤال

in unittest python library, I can use the setUp method for set variables prior to each test and setUpclass prior to all test.

How I can pass data from setUpClass to setUp ?

I try this :

class TestSequenceFunctions(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        cls.a = 0

    def setUp(self):
        self.a = cls.a

    def test_hello01(self):
        self.assertEquals(self.a, 0)

if __name__ == '__main__':
    unittest.main(verbosity=2)

but I get

NameError: global name 'cls' is not defined
هل كانت مفيدة؟

المحلول 2

From within setUp(), you can directly access 'a' without the 'cls' notation, ie:

def setUp(self):
    print self.a   # should print 0

نصائح أخرى

You can access the class of the object with the __class__ attribute.

self.a = self.__class__.a
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top