Skip to content

Instantly share code, notes, and snippets.

@edgabaldi
Created December 2, 2014 16:29
Show Gist options
  • Save edgabaldi/1e4c4751365df6275320 to your computer and use it in GitHub Desktop.
Save edgabaldi/1e4c4751365df6275320 to your computer and use it in GitHub Desktop.
unit test for logging and traceback
import logging
import traceback
logger = logging.getLogger('django')
class PDFParserManager(object):
def run(self):
veiculo_list = list()
for key, value in enumerate(self.pesquisas):
try:
veiculo_list.append(self.parse(value))
except Exception, e:
logger.error('module=plugins.prepara_leilao.tasks.'
'PDFParserManager exception="%s" key=%d value="%s"' % (e, key, value))
logger.error(str(traceback.format_exc()))
self.salva_veiculos(veiculo_list)
# test_tasks.py
from mock import patch, call
from unittest import TestCase
class PDFParserManagerTest(TestCase):
def setUp(self):
arquivo = codecs.open('plugins/prepara_leilao/tests/data/exemplo.pdf', 'r')
self.manager = PDFParserManager(arquivo)
@patch('plugins.prepara_leilao.tasks.traceback.format_exc')
@patch('plugins.prepara_leilao.tasks.logger')
@patch('plugins.prepara_leilao.tasks.PDFParserManager.parse')
def test_run_log(self, _parse, _logger, _format_exc):
_format_exc.return_value = 'foo'
_parse.side_effect= Exception('BOOM!')
self.manager.pesquisas = ['bar']
self.manager.run()
call_list = [
call('module=plugins.prepara_leilao.tasks.PDFParserManager'
' exception="BOOM!" key=0 value="bar"'),
call('foo'),
]
_logger.error.assert_has_calls(call_list)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment