lundi 22 mai 2017

How to disable a try/except block during testing?

I wrote a cronjob that iterates through a list of accounts and performs some web call for them (shown below):

for account in self.ActiveAccountFactory():
  try:
    self.logger.debug('Updating %s', account.login)
    self.update_account_from_fb(account)
    self.check_balances()
    self.check_rois()
  except Exception,e:
    self.logger.exception(traceback.format_exc())

Because this job is run by heroku one every 10 minutes, I do not want the entire job to fail just because one account is running into issues (it happens). I placed a try catch clause here so that this task is "fault-tolerant".

However, I noticed that when I am testing, this try/catch block is giving me cryptic problems because of the task is allowed to continue executing even though there is some serious error.

What is the best way to disable a try/except block during testing?

I've though about implementing the code directly like this:

for account in self.ActiveAccountFactory():
    self.logger.debug('Updating %s', account.login)
    self.update_account_from_fb(account)
    self.check_balances()
    self.check_rois()
    self.logger.exception(traceback.format_exc())

in my test cases but then this makes my tests very clumsy as I am copying large amounts of code over.

What should I do?

Aucun commentaire:

Enregistrer un commentaire