Search code examples
pythoninheritancenose

Nose test superclass calls and inherited classes


Question
How may I test a class and its methods that has inherited from another Superclass through a super() call?

Example

class A():
    def __init__(self, x, y):
        # do something with x and y

    def aMethod(self, param1):
        # do stuff with param1

class B(A):
    def bMethod1(self, param2):
        # do stuff with self and param2
        super(B, self).aMethod(param2)

    def bMethod2(self, myDict):
        # do stuff with myDict
        return myDict

And would it be the same if I had for example, class A(Z) (inheritance from class Z()) and then class B(A) ?

This answer is very similar to what I'm looking for, but it's for Mocking!

Edit
So I would have something like this for my Nose test:

class Test_B(Object):
    # All of the setup and teardown defs go here...
    def test_bMethod2(self):
        b = B()
        dict = { 'a' : '1', 'b' : '2' } # and so on...
        assert_equal(b.bMethod2(dict), dict)

Solution

  • A way of doing so is:

    class Test_B(Object):
    # All of the setup and teardown defs go here...
    def test_bMethod2(self):
        a, b, param = "",
        dict = { 'a' : '1', 'b' : '2' }
        # Instantiating B with its super params
        klass = B(a, b)
        # Calling class B's bMethod1() with required params 
        # to pass onto its super class' method (here: aMethod())
        klass.bMethod1(param)
        assert_equal(klass.bMethod2(dict), dict)
    

    Hope this helps someone else.