28 septembre 2010

Using multiple inheritance for Python mocks

Today, I needed to mock just one function from a class instance.

I used multiple inheritance to achieve that like so:

class ProductDefinitionMock(object):
def function_to_mock(self, product_dict):
return None
class ProdEngineMock(ProductDefinitionMock, ProdEngine):
pass
class MyTests(TestCase):
def setUp(self):
self.prod_engine = ProdEngineMock(...)
view raw mock_multi.py hosted with ❤ by GitHub


The ProdEngineMock class inherits everything from the ProdEngine class, except the mocked function.

Nice and elegant!

Aucun commentaire:

Enregistrer un commentaire