mercredi 30 décembre 2015

Test if super() was called during initialization gives AttributeError

I would like to test if in the following scenario the init routine of the Base class is called during the initialization of the Derived class:

class Base(object):
    def __init__(self, a):
        self.a = a

class Derived(Base):
    def __init__(self, a):
        super(Derived, self).__init__(a)

I used this post here and this test works fine:

@mock.patch("mocktest.Base.__init__")
def test_calls_init_routine_of_base(mock_super_init):
    Derived(1)
    assert mock_super_init.called

However, if the Derived class does something with the attribute a after super(Derived, self).__init(a), e.g. if I put a print(self.a) right after the call to super(), then the test throws an AttributeError:

AttributeError: 'Derived' object has no attribute 'a'

How can I prevent this? Are there other/better ways to realize this test in this situation?

Aucun commentaire:

Enregistrer un commentaire