jeudi 19 février 2015

How to mock up a class for several tests in Django

I have a class that calls a remote service through HTTP. Right now, this class detects if it is running in "TESTING" mode and acts accordingly: while "TESTING" it does not send the actual request to the remote service, it simply returns without executing anything at all.



class PushService(object):

def trigger_event(self, channel_name, event_name, data):
if satnet_cfg.TESTING:
logger.warning('[push] Service is in testing mode')
return
self._service.trigger(channel_name, event_name, data)


Several tests invoke parts of the code that end up by invoking this method. My questions are the following:



1. Do I have to patch this method/class for every test that, for some reason, also invoke that method?
2. Is it a good practice to try to patch it in the TestRunner?

Aucun commentaire:

Enregistrer un commentaire