vendredi 22 janvier 2016

How should a Python (unittest) superclass method reference a variable in its calling subclass?

I am simply not experienced enough in Python OO programming to know how this is done: If I have several classes that are subclasses of a unittest.TestCase subclass. How should the superclass' methods reference variables of the subclasses when the latter call these methods? Let me try to illustrate it with this, probably wrong, example:

import unittest

class TestSuper(unittest.TestCase):

    def test_method(self):
        # do something, e.g.
        pass

class TestSub1(TestSuper):

    def setUp(self):
        self.some_parameter = 1

class TestSub2(TestSuper):

    def setUp(self):
        self.some_parameter = 2

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

Now, I cannot figure out how to correcty reference TestSub1.parameter or TestSub2.parameter, respectively, when TestSuper.test_method is called from the subclasses.

I am inspired by http://ift.tt/1Jo2D95, but here I am trying achieve having multiple test cases that do the same but only differ in their set-up. I can of course achieve all this by just copy-pasting my test case definitions, but I find that bad coding practice.

Aucun commentaire:

Enregistrer un commentaire