[TIP] patching and subclasses

andrea crotti andrea.crotti.0 at gmail.com
Tue Nov 19 08:17:32 PST 2013

I have my nice base test class which extends from the django test case
and another class

class MyTestCase(TestCase, TestFreshProvisionedEachTest):

Now it all works nicely, except that to make it work we have to patch
a couple of functions in the middleware.

My idea is that this would have worked fine:

@patch('spotlight.middleware.extract_host_name', new=lambda host_name:
@patch('spotlight.middleware.extract_host_key', new=lambda host_key:
class MyTestCase(TestCase, TestFreshProvisionedEachTest):

However it doesn't seem to work, the methods of the subclass are not mocked.
I thought then that there could be a way to do something like

MyTestCase = patch(MyTestCase,
'spotlight.middleware.extract_host_name', new=lambda host_name:

But that also is not possible.
Is there a way to avoid this repetition and do a patch on a superclass
which patches all the subclasses methods as well?

More information about the testing-in-python mailing list